-
Notifications
You must be signed in to change notification settings - Fork 13.1k
Closed
Description
π Search Terms
union, generic, assignable, not assignable to parameter of type
π Version & Regression Information
- This is the behavior in every version I tried, and I reviewed the FAQ for entries about union and generics
β― Playground Link
π» Code
const takeString = (param: string) => undefined;
const takeNumber = (param: number) => undefined;
type Func = typeof takeString | typeof takeNumber;
const metaFunc = <T extends Func>(func: T, param: Parameters<T>[0]) => {
// Argument of type 'string | number' is not assignable to parameter of type 'never'.
// Type 'string' is not assignable to type 'never'
func(param);
}
// Type inference works as expected when calling the function
// Argument of type 'number' is not assignable to parameter of type 'string'
metaFunc(takeString, 2)π Actual behavior
The func invocation is invalid. With the error:
Argument of type 'string | number' is not assignable to parameter of type 'never'.
Type 'string' is not assignable to type 'never'
π Expected behavior
TypeScript should recognize that since the same T is used for both func and param, the invocation of func(param) is valid. Similar to how it recognizes that metaFunc(takeString, XXX) should be called with a string rather than a string | number
Additional information about the issue
Potentially related: #51587, #33912, #30581
Note that this is different than https://github.com/microsoft/TypeScript/issues/30581#issuecomment-476417974 since the same T extends Func is used for both func and param, rather than using Func for both types
aandrejevas
Metadata
Metadata
Assignees
Labels
No labels