-
Notifications
You must be signed in to change notification settings - Fork 12.4k
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 parmater with union type not working as expected (not assignable to parameter of type) #57299
Comments
As defined, this is a valid invocation of metaFunc<Func>(takeString, 3);
// Maybe something you'd be more likely to see in the wild
const myFunc = ((arg: string) => undefined) as Func;
metaFunc(myFunc, 3); It might not be practical in your actual use case but in your example, this workaround is available: const metaFunc = <T extends string | number>(func: (arg: T)=>undefined, param: T) => {
// ...
} |
I see the issue. type Func<T extends string | number> = (arg: T) => undefined
const metaFunc = <T extends string | number>(func: Func<T>, param: T) => {
func(param);
} Of course in real life Thanks! |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
π 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: