You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Inference of arguments and the return type of a function
π Motivating Example
π» Use Cases
If you need static variables in func then using the func object as a bag is the only good solution in javascript.
I am not sure if this typescript error is a bug or a feature since assigning properties to func in the outer scope does not cause errors
functionfunc(a,b){}func.counter=1// ok
I found the best way to get rid of the error is to add // @ts-ignore in front of the property assignment. However doing this everywhere just to deal with a typescript shortcoming isn't ideal.
My failed attempts to work around this error by working with typescript:
// trying an interface// One shouldn't create an outside interface for properties that are only ever used inside of funcinterfaceanObject{(...args:any):any;[x:string]:any}// Currently there is no syntax for assigning a type or interface onto func// function func(a, b) { func.counter = 1 } // Hence Suggestion 2.// One has to break the function statement and turn it into an expression// Typescript should not force one to change the structure of existing code to accommodate it. it should be simply additive.varfunc : anObject=function(a,b){func.counter=1}// ok// But now the info on arguments and return type of func is lost (...args:any)=>any// Attempt to fix this:// failsvarfunc: (typeoffunc)&{counter:number}=functionfunc__(params){func.counter=1}// 'func' is referenced directly or indirectly in its own type annotation. ts(2502)// failsvarfunc: {<Textends(...args: any)=>any>(...args:Parameters<T>):ReturnType<T>;counter:number}=function(a,b){func.counter=1}// failsvarfunc: {(a,b):void;counter:number}=function(a,b){func.counter=1}// Property 'counter' is missing ... ts(2741)// Hence suggestion 3.
Suggestion
π Search Terms
Function Statement , static variables
β Suggestion
Problem case:
My 3 suggestions related to this problem.
π Motivating Example
π» Use Cases
If you need static variables in func then using the func object as a bag is the only good solution in javascript.
I am not sure if this typescript error is a bug or a feature since assigning properties to func in the outer scope does not cause errors
I found the best way to get rid of the error is to add
// @ts-ignorein front of the property assignment. However doing this everywhere just to deal with a typescript shortcoming isn't ideal.My failed attempts to work around this error by working with typescript: