I try to learn typescript - make some first steps :). Here's my problem.
I created File1.ts as follows:
export namespace ex {
export var mapper = ( ob : Object ) : NewClass {
return ob as NewClass;
};
export class NewClass extends Object {
public getSomething() : number {
return 2;
}
};
}
Then, I created File2.ts, which uses the "mapper" defined in first file:
import { ex } from './Ex';
var a : Object = myAlreadyExistingObject;
console.log( ex.mapper( a ).getSomething() );
What I get:
ERROR: TypeError: File1_1.ex.mapper(a).getSomething is not a function.
The "Object" class is just an example - in reality I'm using an already defined, complex class. What I'm trying to achieve is to have "extension methods" like in C#, using it like this: ex.mapper( instance ).newFunction()
Why doesn't it work?
return ob as NewClass;
Type assertions in TypeScript don't cause runtime changes. If ob
didn't have a getSomething
method before, then the return value of this function won't either, and you'll get the error described.