DEV Community

Discussion on: Indexing objects in TypeScript

Collapse
 
methodbox profile image
Chris Shaffer

I appreciate the solution as I have a similar issue driving me nuts.

However, this is one aspect of TS I'm not really on board with yet and may never be: you had to write an entirely separate function just to handle the key indexing of a simple object.

This seems like a massive waste of time with little benefit in most cases.

I feel like there is a separation with what our goals are with TypeScript (as developers) and what TS is actually doing in this instance. TS is checking the type of the index value first, rather than resolving that index value and determining if it's valid for our use case.

Basically, I'm forced to check each of the index values whether I want or need to, when really I just need to make sure that value is valid for the argument I'm passing in - it seems the argument's expected type should be the source of truth, not the object's index value's type.

I understand that value still needs to be known to check it against the argument's type, but it would seem identifying that type should be able to be inferred in most cases.

Thanks for the clear explanation here, though!

Collapse
 
pierreyveslebrun profile image
Pierre Lebrun

I quite agree with you.
For this edge case the lack of type inference is a bit disappointing.