ts-fortress
    Preparing search index...

    Function at

    • Extracts the Type stored at a single key of a record type built by record or strictRecord.

      Those carry the shape they were built from, so what comes back is the very member type the shape holds — constraints included, which is what makes at(User, 'age').constraints.max read the same value the schema validates against. An optional member widens to T | undefined, as R[K] does, and still carries the member's constraints.

      Type Parameters

      Parameters

      • recordType: Readonly<
            {
                assertIs: (a: unknown) => asserts a is UnknownRecord;
                cast: (a: unknown) => A;
                defaultValue: A;
                fill: (a: unknown) => A;
                is: (a: unknown) => a is UnknownRecord;
                prune: <B extends UnknownRecord>(a: B) => A;
                typeName: string;
                validate: (
                    a: unknown,
                ) => Result<
                    UnknownRecord,
                    readonly Readonly<
                        {
                            actualValue: unknown;
                            details?: ValidationErrorDetails;
                            expectedType: string;
                            path: readonly string[];
                            typeName: string;
                        },
                    >[],
                >;
            },
        > & Readonly<{ shape: S }>
      • key: K

      Returns ShapeMemberType<S, K>

      import * as t from 'ts-fortress';

      const User = t.record({
      age: t.number(0, { int: true, min: 0, max: 120 }),
      });

      const max: 120 = t.at(User, 'age').constraints.max;

      // equivalently, straight off the record type
      const sameMax: 120 = User.shape.age.constraints.max;

      assert.strictEqual(max, 120);

      assert.strictEqual(sameMax, 120);
    • Extracts the Type stored at a single key of a record type.

      The first argument must be a record type (i.e. it must extend Type<UnknownRecord>, like pick and keyof), and the second argument is constrained to the keys declared on that record.

      The record type may be a simple record, an intersection (e.g. the result of mergeRecords), a union, a recursive type, or any combination of these — the underlying shape is expanded and, when a key resolves to multiple member types (as in a union), the resulting types are combined with union.

      Type Parameters

      • const R extends UnknownRecord
      • const K extends string

      Parameters

      Returns Type<R[K]>

      import * as t from 'ts-fortress';

      const ymd = t.record({
      year: t.number(1900),
      month: t.number(1),
      date: t.number(1),
      });

      const year = t.at(ymd, 'year'); // Type<number>

      assert.isTrue(year.is(2000));
    • Extracts the Type stored at a single index of a tuple type.

      Type Parameters

      • const T extends readonly unknown[]
      • const N extends number

      Parameters

      Returns Type<T[N]>

      import * as t from 'ts-fortress';

      const tup = t.tuple([t.number(), t.string(), t.boolean()]);

      const second = t.at(tup, 1); // Type<string>

      assert.isTrue(second.is('hello'));