javascript - Extending builtin objects in TypeScript on Netbeans with plugin -
i trying transcript existing javascript code typescript , ran problem extending builtin objects object.defineproperty
, e.g. string.prototype
.
object.defineproperty(string.prototype, 'testfunc', { value: function():string {return 'test';} }); var s1:string = 'abc'.testfunc(); // property 'testfunc' not exist on type 'string' var s2:string = string.prototype.testfunc(); // property 'testfunc' not exist on type 'string' object.defineproperty(object, 'testfunc', { value: function():string {return 'test';} }); var s:string = object.testfunc(); // property 'testfunc' not exist on type 'objectconstructor'
it gets translated javascript, however, netbeans 8.1 typescript plugin claims errors listed comments above.
all of confused experiments declare
, interface
did not match proper syntax. have no idea how work.
how can extend builtin objects in typescript , make ide accepting it?
after 1001 try'n'errors i've figured out working solution. seems want.
interface string { strfunc:function; } object.defineproperty(string.prototype, 'strfunc', { value: function():string {return 'test';} }); var s1:string = 'abc'.strfunc(); var s2:string = string.prototype.strfunc(); interface objectconstructor { objfunc:function; } object.defineproperty(object, 'objfunc', { value: function():string {return 'test';} }); var s:string = object.objfunc(); // objfunc should not work on strings: var s:string = 'a string instance'.objfunc(); // property 'testfunc' not exist on type 'string'
Comments
Post a Comment