TypeScript icon indicating copy to clipboard operation
TypeScript copied to clipboard

Typescript allows assigning anything to `Record<number, ...>`

Open rChaoz opened this issue 1 year ago • 2 comments

🔎 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

rChaoz avatar Oct 19 '24 22:10 rChaoz

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.

MartinJohns avatar Oct 19 '24 23:10 MartinJohns

Duplicate of #45727.

MartinJohns avatar Oct 19 '24 23:10 MartinJohns

This issue has been marked as "Duplicate" and has seen no recent activity. It has been automatically closed for house-keeping purposes.

typescript-bot avatar Nov 02 '24 01:11 typescript-bot