Skip to content
webtype.orgwebtype.org

    ↑↓ move · ⏎ open · esc close

    Object shapes

    Record

    Builds an object type from a key set and one value type.

    What it is

    The constructor to Pick’s selector. Given a union of keys and a value type it produces the object with those keys, and given string it produces an index signature instead. Those two behaviours look similar and are not: one is a fixed shape and the other is a dictionary.

    Examples

    • Record<'a' | 'b', number>
      →{ a: number; b: number; }
    • Record<string, number>
      →{ [x: string]: number; }

      An index signature, not a fixed shape — every key is allowed and none is guaranteed.

    • Record<'id', string>
      →{ id: string; }

    Each resolved type above was printed by TypeScript 6.0.3, not written by hand.

    What it does not do

    • It does not promise the keys exist at run time. Record<string, number> will happily let you read a key that was never set, and hand you number for it.
    • It does not vary the value per key. When each key needs its own value type, you want a mapped type with as, not Record.

    Takeaway

    A literal union of keys gives you a shape the compiler will check. string gives you a dictionary it cannot. Choose deliberately.