You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
constarray=[1,2,3,4]a[i]// this should be of type numbertypeKey="a"|"b"|"c"constrecord: Record<Key,number>={"a": 0,"b": 1,"c": 2}record[i]// this should be of type numberconstrecord: Record<string,number>={"a": 0,"b": 1,"c": 2}record[i]// this should be of type number | undefined
Why isn't there a way to configure TypeScript to have these most sane type assumptions? Either they're all a number, which is very unsafe for the third case, or with noUncheckedIndexedAccess they're all number | undefined which makes zero sense for the first two, as basically means to use ! for every array access.
EDIT: Note also that if Map is used in place of Record for the third case, it will correctly be number | undefined which also makes zero sense given that Record and Map are used interchangeably and should have consistent interfaces.
The text was updated successfully, but these errors were encountered:
alamothe
changed the title
TypeScript is terrible at type-checking array/Record index access
TypeScript is broken at type-checking array/Record index access
Jun 24, 2023
alamothe
changed the title
TypeScript is broken at type-checking array/Record index access
TypeScript is broken at type-checking array/Record access
Jun 24, 2023
The statement here that numeric access should be assumed in-bounds and string access should be assumed possibly-out-of-bounds is actually the reverse from what I hear most often.
Uh oh!
There was an error while loading. Please reload this page.
Why isn't there a way to configure TypeScript to have these most sane type assumptions? Either they're all a
number
, which is very unsafe for the third case, or withnoUncheckedIndexedAccess
they're allnumber | undefined
which makes zero sense for the first two, as basically means to use!
for every array access.EDIT: Note also that if
Map
is used in place ofRecord
for the third case, it will correctly benumber | undefined
which also makes zero sense given thatRecord
andMap
are used interchangeably and should have consistent interfaces.The text was updated successfully, but these errors were encountered: