Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

infer function generic signature from actual function's return type

See original GitHub issue


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:open
  • Created a year ago
  • Comments:14 (7 by maintainers)

github_iconTop GitHub Comments

RyanCavanaughcommented, Jun 21, 2022

I believe what actually happens is that we collect candidates for T, find out there are none, so default it to its constraint, which is unknown, and then process the call as if you had written test<unknown>(.

It’s weird since if you had written test<unknown>(, that definitely shouldn’t be an implicit any error. Maybe we need to make a marker unknown to use in zero-candidate inference that isn’t allowed to contextually type a parameter - worth experimenting with, probably.

RyanCavanaughcommented, Aug 26, 2022

@trusktr did you mean to use prev in these examples? Again we have the problem of “you can just omit the parameter”.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Post

No results found

github_iconTop Related Hashnode Post

No results found