Files
typebox/docs/types/indexed.md

1.1 KiB

Home | Installation | Usage | Types | Values | Syntax | TypeRegistry | TypeCheck

Indexed Access Types

TypeBox supports indexed access types with the Index function. This function enables uniform access to interior property and element types without having to extract them from the underlying schema representation. Index types are supported for Object, Array, Tuple, Union and Intersect types.

const T = Type.Object({
  // type T = {
  x: Type.Number(), //   x: number,
  y: Type.String(), //   y: string,
  z: Type.Boolean(), //   z: boolean
}); // }

const A = Type.Index(T, ["x"]); // type A = T['x']
//
// ... evaluated as
//
// const A: TNumber

const B = Type.Index(T, ["x", "y"]); // type B = T['x' | 'y']
//
// ... evaluated as
//
// const B: TUnion<[
//   TNumber,
//   TString,
// ]>

const C = Type.Index(T, Type.KeyOf(T)); // type C = T[keyof T]
//
// ... evaluated as
//
// const C: TUnion<[
//   TNumber,
//   TString,
//   TBoolean
// ]>

Back to Home