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

Generic function passed to generic function inferred correctly only with spread #60552

Open
webstrand opened this issue Nov 21, 2024 · 1 comment

Comments

@webstrand
Copy link
Contributor

🔎 Search Terms

generic function spread regression inference

🕗 Version & Regression Information

  • This is the behavior in every version I tried, and I reviewed the FAQ for entries about generics

⏯ Playground Link

https://www.typescriptlang.org/play/?ts=5.8.0-dev.20241121#code/KYDwDg9gTgLgBDAnmYcByEAmqC8cDecAvgNwBQZ2AxgDYCGUwAkAGYCuAdlTAJYQdwWEYSIgAeACoAaOACVgAZzY14oGMA6YFcAIJQodRABkeAa2BjOpjhADuHAHwyAogDceNOGo1a4jOpj8NIhwVjb2ANoAug4AFGRMPBxgbDAAXHDSCVR0NDQARnRUphmxSSnpmTJgjK4Z8koqMgB0rcDuNBluHgCUcDgOcorKMFJkPfXDKhTU9IysnNx8AoV0a2tQkjINI14g6praegbGZhZhdo5xCeWpGVlMOXmFxaW3ldJwNe2TjTB9AyGfzGEyBIxmwFoDFQ7C4vH4cCeBSKpkk1zgGLg73uY0xX1qGQUMCgSQA5tEQYTiWTouQyKBILBBIt4QJQHQALZgGgWCRxbGZPr4Ch4gD0orgAEkOCxgFBtEgUF8GJzgOooGCVIjoIxuMEEkJRMIyslUjIkS9TD1yGKJQA9AD8Isx4rgADE6B4FRAsTK5QhkKgwCqOWr-TstVQdZCYMFfUTgAFfbL5QgABaoKMcIkGJIwBKrdYME0Vc25ZHFa1kW1wR3OjGugBCyDoCgUZOTco0VGAMhgGeZcOWiPLWO0Uf0MermML602nxzNJiJbNI+eKKr04bEulKYVgeVBlD6s18AnutjiC3cFnGxXozXFdRfKrNbrRDIQA

💻 Code

export type Node = { };
declare function fooooooo<T, Result extends ArrayLike<unknown>, Evil extends readonly unknown[]>(
    input: T,
    callback: (input: T, prev: Result, ...evil: Evil) => Result,
): Result

declare function baaaaaar<T, Result extends ArrayLike<unknown>>(
    input: T,
    callback: (input: T, prev: Result) => Result,
): Result

declare function callback<T>(
    input: T,
    prev: string[],
): string[];

export function example<T>(input: T) {
    // Infers type parameter Result correctly
    fooooooo(input, callback);
    // ^? function fooooooo<T, string[], []>(input: T…

    // Fails to infer type parameter Result correctly instead infers the constraint
    baaaaaar(input, callback);
    // ^? function baaaaaar<T, ArrayLike<unknown>>(in…



    // Bypassing inference, the function call is correct
    baaaaaar<T, string[]>(input, callback);

    // Infers type parameter Result correctly
    baaaaaar(input, callback<T>);
    // ^? function baaaaaar<T, string[]>(input: T, ca…
}

🙁 Actual behavior

baaaaaar(input, callback) infers the constraint of Result = ArrayLike<unknown>

🙂 Expected behavior

baaaaaar(input, callback) should infer Result = string[] from the return type or parameter of callback.

Additional information about the issue

No response

@Andarist
Copy link
Contributor

I believe that at the implementation level the difference boils down to this line of code:
https://github.dev/microsoft/TypeScript/blob/d85767abfd83880cea17cea70f9913e9c4496dcc/src/compiler/checker.ts#L35096

In the case that doesn't work a fixing mapper is used and that prevents inference candidates that are discovered later from being chosen.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants