-
Notifications
You must be signed in to change notification settings - Fork 12.6k
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
String template literals as object keys don't get validated correctly #58673
Comments
Duplicate of #13948. |
Note that |
I mean I guess strictly from the perspective of something receiving the type but TS still checks that you're putting in the correct things when you're initially creating the object const aa: Record<number, boolean> = {
[2]: true,
salami: "sandwich",
}; |
Only when assigning the object literal to the type directly: const bb = {
[2]: true,
salami: "sandwich",
};
const aa: Record<number, boolean> = bb; // ok, no error The excess property check isn't comprehensive, you shouldn't rely on it to constrain the allowed values of a type. That's why issue #12936 exists. |
This issue has been marked as "Duplicate" and has seen no recent activity. It has been automatically closed for house-keeping purposes. |
π Search Terms
String template literal object keys return type function
π Version & Regression Information
β― Playground Link
https://www.typescriptlang.org/play/?ts=5.4.5#code/MYewdgzgLgBBCGBbADgGwKYEYYF4YAoAPALjAFdEAjdAJwEpiYAldUGgEwB4ADBFDAPoASAN7kqtAL7cANNBoBLMAHMAfLnX4RAKBh6YAbV5I06YSMLSAuowBEACXSpUIWzO2S627aEiw+pgBMuAQk4tT0jCxsXMb8ZqLhUrLySmoaBDr6MAD0OTBKbDSsUKgAnjAAbvCoCuwwANboZbr6RgBC8PW1ULQ1MKKW3DYwtpRdMOPsbh5ePuDQcCYYAMwhRKQUEQzMrCAcPAGCiVvJclCKKuo4mln6eTBFJeUF7OhgUAoAZgro9VONZqtPS2ZTwGjjZToWx2YoxWyzIA
π» Code
π Actual behavior
Typescript does not error on incorrect string template literals
π Expected behavior
Typescript should show sample2 as having a bad object key like it does in sample3
Additional information about the issue
Bonus feature request/question: Is this the only way to get this sort of return type from a function. Putting "as const" in the key or after the object doesn't seem to correctly infer the type. It stays wide {[x:string]: string}
The text was updated successfully, but these errors were encountered: