3 ms·
I think the example type is useful for unvalidated input data e.g. from an API call. Or an update function for a DB abstraction. Internally in the backend, you
by 3np 5y ago
I think the example type is useful for unvalidated input data e.g. from an API call. Or an update function for a DB abstraction.
Internally in the backend, you’d still use the type you just posited.
- throwanem 5y agoEh. A good ORM provides type definitions from model definitions, which is one way I've found ORMs more useful in TS than JS, and I'd more likely use a runtype or a decoder to both validate and type inbound data than roll my own interface for it. On review of documentation, I was actually pretty off base in grandparent comment. The real use case for Record appears to be when you need a map type whose keys are both explicitly enumerated and defined elsewhere, ie in a union, enum, or otherwise unrelated object type. Rather than duplicating the keys, you can use Record<someUnion, V> or Record<keyof typeof someEnum, V> and only have to make one change to update both. For the "arbitrary keys, known value types" case I mentioned earlier, an object type with an index signature works fine and may be more legible.
- chana_masala 5y ago> an object type with an index signature works fine and may be more legible. If I understand you correctly, that's exactly what a Record is underneath
- throwanem 5y agoMore or less. There are extra steps involved with a Record, but they work the same iirc.