TypeScript
TypeScript copied to clipboard
Typescript allows assigning anything to `Record<number, ...>`
🔎 Search Terms
assigning to record number
🕗 Version & Regression Information
- This is the behavior in every version I tried, and I reviewed the FAQ for entries
⏯ Playground Link
https://www.typescriptlang.org/play/?ts=5.7.0-beta#code/MYewdgzgLgBCBGArGBeGBvGEQFsCmUAFgJZgDmAXDAETQBO1MAvgLABQoksdewVASrxB0AJgB4wAVxzw8dADRYodUmQB8qOEiA
💻 Code
const obj = { something: "str" }
const rec: Record<number, string> = obj
🙁 Actual behavior
No error
🙂 Expected behavior
It should error as "something" is not of type number
This is working as intended. The type Record<number, string> means that all number keys are string, not that only number keys exists. You can't limit the existence of additional properties.
Duplicate of #45727.
This issue has been marked as "Duplicate" and has seen no recent activity. It has been automatically closed for house-keeping purposes.