-
Notifications
You must be signed in to change notification settings - Fork 12.5k
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
Incorrect Type Inference When Using Dynamic Key with Union Value Types #60549
Comments
This is working as intended. The type of As a workaround you can do this: (credit to y-nk) type Kinded<T> = { [K in keyof T]: [key: K, val: T[K]] }[keyof T]
declare function getVideoGame(...args: Kinded<VideoGames>): string; This forces you do provide matching literal types. |
Thank you for the workaround, we'll try it. But it seems very strange that this should be intended behavior when it enables code that is not typesafe. In our getVideoGame function if we were to make a check like this if (game === "mario-kart") {
// console is now inferred to "switch"
} TypeScript now infers the type as expected, but it might be incorrect Intended or not, this is a flaw in the type system that allows for unsafe code |
That's not the case. The type will be |
Oh you are absolutely right, I assumed it would infer it. |
π Search Terms
Type inference with union types, Generics and union types, Keyof with union types
π Version & Regression Information
Not really sure, started implementation of the feature in 5.6.3.
β― Playground Link
https://www.typescriptlang.org/play/?#code/KYDwDg9gTgLgBDAnmYcBqBLAJsCBxAQwFtgBnOAXjgG8AoOOAIiIwDtgBjKAgMxkYBcTMB0ZwAPk1IB3DDA4ALRgG56TIgSgYIAWgDWm-kMYy5ilWsZYMBAEYAbCIOGjVAX1W0cHe5tQ8AV1YOGG1WOABzYBhMHHxiYAAeAGk4UBhgVixyPWBECB50bFxCElIAPgAKNVYEoWSAGjUOCFZSCHtgIViShNIAbWSAXVoASiFSGC1WCM8Aejm4AFFwTgysIQA5CDSoKGg4SpZ2Ll54DHICADcCDF8HVFa4EVHaKJji+JJK5jZObj4jAaLkYo3mixWKBCwA2yz2ByOmm0+kMcAucFYEHg11u906cCeL1oCzh+ygcAI5FAUPWQgAglAIgESKx4AUEMhUAByRgiRhctHkTHY0ikDARWoPBA7MCaBIZcnspAoOA80zyJRct7RHpfYA-DRaXQGWBAkFg2heTi+KCoFpteBYRC1FgcUpdOC5fKFXXu0jguAASVFAQ9212ZMFz32WACHBhcAUwFtxIhq2hsKW8PJtk4BACpG5TpdGDdCQFLQC9iwcFz6iRxsMZukClLCijwopNzudnxT15om1Hzi7sqxeIpfdwIHoOUQA
π» Code
π Actual behavior
No error is reported on the last call. TypeScript allows
getVideoGame(dynamicGame, "pc")
without any type errors, even though"pc"
is not a valid console for all possible games inVideoGames
.π Expected behavior
An error should be reported on the last call to
getVideoGame
becausedynamicGame
can be any key ofVideoGames
, including"mario-kart"
, which does not support the"pc"
console. The type system should enforce that theconsole
argument is valid for all possible values ofdynamicGame
.Additional information about the issue
This issue affects the type safety of functions that rely on generic keys with associated value types. It seems that when using a dynamic key of a union type, the compiler does not enforce that the provided value is valid for all possible keys, potentially allowing invalid combinations without type errors.
The text was updated successfully, but these errors were encountered: