Hacker News new | ask | show | jobs
by presentation 1709 days ago
I actually don't really like Record types in the way people/library maintainers often use them - the type-checker asserts that values are actually present for all the specified keys, which is fine if the objects with the Record type really were exhaustive; but instead I often see them used where the reality of the data is a Partial<Record> - some keys are missing. Something about the abstraction causes people to misuse it frequently.
2 comments

Yep, and in your own code you can tell TS to verify your property access with the setting `noPropertyAccessFromIndexSignature` (https://devblogs.microsoft.com/typescript/announcing-typescr...)

The TS crew did discuss having a "pedantic" mode, which is stricter than "strict", but I don't think it's on the roadmap any more.

That one as well as the noUncheckedIndexedAccess ought to be defaults when in strict mode.
You pretty much always have to define your record type with `| undefined` tacked on to the value type parameter. With that, the problem mostly goes away.
What's the point of typing things if you have to constantly typecheck them anyway?
Yeah, but I use libraries that don’t do that, and have teammates that don’t care about this