Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Higher order type inference from generic functions lost when result type is wrapped #51850

Closed
maclockard opened this issue Dec 10, 2022 · 2 comments
Labels
Duplicate An existing issue was already created

Comments

@maclockard
Copy link

maclockard commented Dec 10, 2022

Bug Report

🔎 Search Terms

Higher order type, Higher kinded type, Inference generic lost, Wrapped

🕗 Version & Regression Information

and fill in the line that best applies:

  • This is the behavior in every version I tried, and I reviewed the FAQ for entries about higher order type inference

⏯ Playground Link

Playground link with relevant code

💻 Code

declare function wrapFuncResult<A extends any[], R>(func: (...args: A) => R): (...args: A) => { wrapped: R };

declare function wrapFuncResult2<A extends any[], R>(func: (...args: A) => R): { wrappedFunc: (...args: A) => { inner: R } };

declare const stringToNum: (s: string) => number;
const stringToNumWrapped = wrapFuncResult(stringToNum); // (s: string) => { wrapped: number }
const stringToNumWrapped2 = wrapFuncResult2(stringToNum); // { wrappedFunc: (s: string) => { wrapped: number } }

declare const toArray: <T>(t: T) => T[];
const toArrayWrapped = wrapFuncResult(toArray);  // <T>(t: T) => { wrapped: T[] }
const toArrayWrapped2 = wrapFuncResult2(toArray); // { wrappedFunc: (t: any) => { wrapped: any[] } } - :(

🙁 Actual behavior

The function toArrayWrapped2 is of type { wrappedFunc: (t: any) => { wrapped: any[] } }. The inference of the type parameter T from toArray was lost when the return type was wrapped.

🙂 Expected behavior

The function toArrayWrapped2 is of type { wrappedFunc: <T>(t: T) => { wrapped: T[] } }. This would preserve the type parameter T from toArray just like the unwrapped result type of toArrayWrapped.

@maclockard maclockard changed the title Higher order type inference from generic functions lost when result in wrapped Higher order type inference from generic functions lost when result type is wrapped Dec 10, 2022
@jcalz
Copy link
Contributor

jcalz commented Dec 11, 2022

Duplicate of (or strongly related to) #49505

@RyanCavanaugh RyanCavanaugh added the Duplicate An existing issue was already created label Dec 12, 2022
@typescript-bot
Copy link
Collaborator

This issue has been marked as a 'Duplicate' and has seen no recent activity. It has been automatically closed for house-keeping purposes.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Duplicate An existing issue was already created
Projects
None yet
Development

No branches or pull requests

4 participants