Re:All I Want for Christmas Is These Seven TypeScript Improv...
by TheCleric on Monday December 26, 2022 @07:07PM
(#34143334)
> Deducing types from Object.entries would be pretty awesome. That would feel like black magic.
Not really. Typescript already has the
keyof
keyword. For some reason though Object.entries(T)
returns string[]
when it could easily return keyof typeof T
I've had to write a helper function that essentially does this for you with a generic and a cast, but it feels unnecessary.
Still, it feels dangerous to be able to feed a generic object method in and expect the return to be typed based on a previous typing of the composite instances. Or maybe it feels like something that should always pass a compile check - because Object.entries can always be any - and only fail at runtime. Even if it was a feature, it's something I wouldn't implicitly trust a compiler to catch all edge cases of.