infer function generic signature from actual function's return type
See original GitHub issueSuggestion
function test<T>(fn: (prev: T) => T) { }
test((prev) => ({ a: 1 })); // T is inferred as "unknown"
đ Search Terms
Maybe an issue exists, but I wasnât sure what to search for. âInfer generic function type from return valueâ didnât really help.
â Viability Checklist
My suggestion meets these guidelines:
- This wouldnât be a breaking change in existing TypeScript/JavaScript code. not sure
- This wouldnât change the runtime behavior of existing JavaScript code
- This could be implemented without emitting different JS based on the types of the expressions
- This isnât a runtime feature (e.g. library functionality, non-ECMAScript syntax with JavaScript output, new syntax sugar for JS, etc.)
- This feature would agree with the rest of TypeScriptâs Design Goals.
â Suggestion
T
should be inferred as the type of the returned object, {a: number}
đ Motivating Example
đť Use Cases
make life easier
Issue Analytics
- State:
- Created a year ago
- Comments:14 (7 by maintainers)
Top Results From Across the Web
Typescript Function with Generic Return Type - Stack Overflow
To reiterate: when you have a generic function, the generic type parameters are specified by the caller, not by the implementer. It's true...
Read more >Type Inference - Learning the Java Language
The inference algorithm determines the types of the arguments and, if available, the type that the result is being assigned, or returned. Finally,...
Read more >4. Functions - Programming TypeScript [Book] - O'Reilly
This is TypeScript's syntax for a function's type, or call signature (also called a type signature). You'll notice it looks remarkably similar to...
Read more >Typescript: Type Inference on function arguments (huge update)
It works, but sometimes, if not always, you are not allowed to use immutable values. Second option is much better. You can add...
Read more >How To Use Generics in TypeScript - DigitalOcean
Generics can appear in functions, types, classes, and interfaces. ... This generic type is used in the return type of your function:Â ...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
I believe what actually happens is that we collect candidates for
T
, find out there are none, so default it to its constraint, which isunknown
, and then process the call as if you had writtentest<unknown>(
.Itâs weird since if you had written
test<unknown>(
, that definitely shouldnât be an implicitany
error. Maybe we need to make a markerunknown
to use in zero-candidate inference that isnât allowed to contextually type a parameter - worth experimenting with, probably.@trusktr did you mean to use
prev
in these examples? Again we have the problem of âyou can just omit the parameterâ.