Microsoft/TypeScript
GitHub ã§èŠãGeneric function passed to generic function inferred correctly only with spread
Open
#60,552 opened on 2024幎11æ21æ¥
Domain: check: Type InferenceHelp WantedPossible Improvement
Repository metrics
- Stars
- Â (48,455 stars)
- PR merge metrics
-  (å¹³åããŒãž 6d 17h) (30d ã§ 9 merged PRs)
説æ
ð 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
ð» 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