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.
Extracts the Type stored at a single key of a record type built by
recordorstrictRecord.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.maxread the same value the schema validates against. An optional member widens toT | undefined, asR[K]does, and still carries the member's constraints.