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
{{ message }}
Repository navigation
generate function declaration from function type in TypeScript #22395
typeMyCallback=(a: number,b: string)=>voidfunctionfoo(callback: MyCallback){// ...}// case 1foo((a,b)=>{ ... })// case 2foo(function(a,b){ ... })// case 3functioncb(a: number,b: string){ ... }foo(cb)
Above are 3 common ways to write a callback.
TypeScript is great at inferring the argument types in the implemented callbacks. a, and b are typed accordingly in the callbacks above.
However, the tooltip is not useful when we are implementing the callbacks:
If I change foo() to not using type alias, it helps in case 1 and 2:
But that doesn't help in case 3 and actually demotes type alias.
One solution I can think of is using [ctrl+.] to fill in the callback:
foo(/* caret */)// [ctrl+.] and choose "implement callback as arrow function":foo((a: number,b: string)=>{/* caret */})// [ctrl+.] and choose "implement callback as function declaration":foo(function(a: number,b: string){/* caret */})// caret anywhere on the type alias belowMyCallback// [ctrl+.] and choose "implement function declaration" will expand to snippet:function${1:functionName}(a: number,b: string): void{${0}}
This is really great! It's sometimes hard to follow the hints, having TS write a new arrow function in there for me with all the parameters leaving me only to write the function body is just great 🤩
Activity
ackvf commentedon Apr 10, 2022
This is really great! It's sometimes hard to follow the hints, having TS write a new arrow function in there for me with all the parameters leaving me only to write the function body is just great 🤩