Method Chainability and Inheritance

Topics: Language Specification
Oct 5, 2012 at 8:51 PM

Hello, I've a class which I'll call "Base" and a subclass ("SubBase").


Base has some methods where it returns "this". The class is designed to help method chainability so that I can:


new SubBase().methodA().methodB().methodC() => SubBase.


The problem is that base and subBase are defined as follow: 


declare module test {
	export class Base {
		setAnchorPoint(x:number, y:number):Base;
		setPosition(x:number, y:number): Base;
	export class SubBase extends test.Base{

		setSomethingElse(x:number, y:number): SubBase;

in this snippet, when I do something like:

new SubBase().setAnchorPoint(0,0).setPosition(0,0).setSomethingElse() ;

since setPosition() returns Base, the compiler tells me that Base does not have method "setSomethingElse".

So i'm trying to understand if there's some keyword I can use like "self" so that all the subclasses will also inherit the method and "override" the return value.


Oct 8, 2012 at 4:58 PM

There is discussion of something like this in one of the other forum posts: