How do I leverage type inference for nested function parameters?
I'm having trouble with type inference on this function. It seems to work intermittently. Sometimes it infers the type {} and other times it will infer the correct type. Here is an example:
function foo<T>(a: (b: () => T) => T) {
const b = () => value;
const value = a(b);
return value;
}
const v = foo(b => 'a'); // 'v: {}' but should be 'v: string'
I could declare the type for b when I call foo() but foo is sometimes called from JS and I wanted to leverage IntelliSense while working in JS. Is there another way I can do this?
Additional info:
- TypeScript v3.4.3
- Using VSCode to inspect types
Screenshot of it working intermittently: https://i.imgur.com/dRC8ab7.png
Posted Apr 17, 2019, 5:19 PM UTC · edited
Try this one:
Thank you!