You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
consttakeString=(param: string)=>undefined;consttakeNumber=(param: number)=>undefined;typeFunc=typeoftakeString|typeoftakeNumber;constmetaFunc=<TextendsFunc>(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
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
The text was updated successfully, but these errors were encountered:
As defined, this is a valid invocation of metaFunc
metaFunc<Func>(takeString,3);// Maybe something you'd be more likely to see in the wildconstmyFunc=((arg: string)=>undefined)asFunc;metaFunc(myFunc,3);
It might not be practical in your actual use case but in your example, this workaround is available:
π Search Terms
union, generic, assignable, not assignable to parameter of type
π Version & Regression Information
β― Playground Link
https://www.typescriptlang.org/play?#code/MYewdgzgLgBFCGBrApgZSgJwJZgOYwF4YAKAB3g3gFsAuGabPASkID4YBXMAE2QDMcybgG4AUKEiwEKAHIcqAI2QZCJcpVoww8pRhYF2XXgLBCxoqAE9SyGADEuwVVZsg+cJGkw58AHzjWyG4esjrK5hLQMFTICA5gTkQAPAAqMMgAHlDIPBD2jqzEfI50KQA0MOrUdAAKFNSxyhCprADaAAwAuvrsAN6iMDAA9EMwAIIYuPI5sMEutgDkDD4w-tqKygswWHlgILDwEBBYuGDwCgA2tlAglfUx2SpzgTALpgBumwB0A8OjKS8lt48Fsdlp9jBDsdTucrnBbvNXh9Nr9igkyPcmGIAL6iUQjGAAmzbMB8ZQ5YC2ADuIAwiDyh3SGRswGy3BgVIAFjkYMB4BcLisoNyYGjWVhwPjRhMpjEwLN3Ii3mEMKDdhCoSczpdrrcqg9lDBnsSgYxcAtRA94PFgMRpF4zRUAExMURAA
π» Code
π Actual behavior
The
func
invocation is invalid. With the error:π Expected behavior
TypeScript should recognize that since the same
T
is used for bothfunc
andparam
, the invocation offunc(param)
is valid. Similar to how it recognizes thatmetaFunc(takeString, XXX)
should be called with a string rather than astring | 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 sameT extends Func
is used for bothfunc
andparam
, rather than usingFunc
for both typesThe text was updated successfully, but these errors were encountered: