in

Iterates over a union of keys in a mapped type and narrows via the in operator.

Since TS 2.1 Spec ↗

Syntax

{ [K in Keys]: ValueType }

Examples

type Flags = { [K in 'a' | 'b']: boolean };
// { a: boolean; b: boolean }
function area(shape: { kind: string }) {
  if ('radius' in shape) {
    // shape narrowed to the variant with radius
  }
}

Notes

In a mapped type, `in` binds a key variable to each member of a key union. As a runtime operator inside type narrowing, `in` checks for property presence and narrows discriminated unions. Combine with `keyof` to map over an existing type's keys.

See also