In TypeScript, generics parameter before function -


i've defined interface a this:

interface {   (): any;   b: any; }; 

however, it's not easy define variable compatible interface.

i've tried with:

var a: = function () { console.log('this function'); } a.b = 'this field'; 

this snippet failed due property 'b' missing in type '() => void'. think it's reasonable, there's no b field when defining a.

then tried one:

var a: = <a> function () { console.log('this function'); } a.b = 'this field'; 

this worked, have no idea what's <a> function ... syntax is.

it'll great if help. i've searched every corner in official document.

thanks.

var a: = <a> function () ... 

here, <a> not generic, it's cast (you instead write as a @ end). problem original statement (which missing cast) assigning had no property b variable a: a (a function, on own, satisfy (): any declaration). casting, asserting function (or will) have property.

you could, alternatively, make b optional defining as:

b?: any; 

in case not need cast, mild abuse of optional, think better suited options objects (used parameters).

note bad practice assign properties functions in typescript (which why running difficulties), though language allows (for sake of converting old js).


Comments

Popular posts from this blog

php - Wordpress website dashboard page or post editor content is not showing but front end data is showing properly -

javascript - Get parameter of GET request -

javascript - Twitter Bootstrap - how to add some more margin between tooltip popup and element -