-
Notifications
You must be signed in to change notification settings - Fork 0
runtype
A runtype determines at runtime whether a value conforms to a type specification.
Name | Default |
---|---|
A |
unknown |
-
RuntypeBase<A>
↳ Runtype
↳↳ Literal
↳↳ InstanceOf
↳↳ Unknown
↳↳ Never
↳↳ Boolean
↳↳ Number
↳↳ BigInt
↳↳ String
↳↳ Symbol
↳↳ SymbolFor
↳↳ Array
↳↳ Tuple
↳↳ Dictionary
↳↳ Union
↳↳ Intersect
↳↳ Optional
↳↳ Function
↳↳ Constraint
↳↳ Brand
• Readonly
_falseWitness: A
Inherited from: void
Defined in: src/runtype.ts:39
• Readonly
reflect: Reflect
Convert this to a Reflect, capable of introspecting the structure of the type.
Inherited from: void
Defined in: src/runtype.ts:37
▸ And<B>(B
: B): Intersect<[Runtype<A>, B]>
Intersect this Runtype with another.
Name | Type |
---|---|
B |
Runtype<unknown, B> |
Name | Type |
---|---|
B |
B |
Returns: Intersect<[Runtype<A>, B]>
Defined in: src/runtype.ts:54
▸ Or<B>(B
: B): Union<[Runtype<A>, B]>
Union this Runtype with another.
Name | Type |
---|---|
B |
Runtype<unknown, B> |
Name | Type |
---|---|
B |
B |
Returns: Union<[Runtype<A>, B]>
Defined in: src/runtype.ts:49
▸ assert(x
: any): asserts x is A
Verifies that a value conforms to this runtype. When given a value that does not conform to the runtype, throws an exception.
Name | Type |
---|---|
x |
any |
Returns: asserts x is A
Inherited from: void
Defined in: src/runtype.ts:15
▸ check(x
: any): A
Verifies that a value conforms to this runtype. If so, returns the same value, statically typed. Otherwise throws an exception.
Name | Type |
---|---|
x |
any |
Returns: A
Inherited from: void
Defined in: src/runtype.ts:21
▸ guard(x
: any): x is A
A type guard for this runtype.
Name | Type |
---|---|
x |
any |
Returns: x is A
Inherited from: void
Defined in: src/runtype.ts:32
▸ optional(): Optional<Runtype<A>>
Optionalize this Runtype.
Defined in: src/runtype.ts:59
▸ validate(x
: any): Result<A>
Validates that a value conforms to this type, and returns a result indicating success or failure (does not throw).
Name | Type |
---|---|
x |
any |
Returns: Result<A>
Inherited from: void
Defined in: src/runtype.ts:27
▸ withBrand<B>(brand
: B): Brand<B, Runtype<A>>
Adds a brand to the type.
Name | Type |
---|---|
B |
string |
Name | Type |
---|---|
brand |
B |
Defined in: src/runtype.ts:102
▸ withConstraint<T, K>(constraint
: ConstraintCheck<Runtype<A>>, options?
: { args?
: K ; name?
: string }): Constraint<Runtype<A>, T, K>
Use an arbitrary constraint function to validate a runtype, and optionally to change its name and/or its static type.
Name | Default | Description |
---|---|---|
T |
- | Optionally override the static type of the resulting runtype |
K |
unknown | - |
Name | Type | Description |
---|---|---|
constraint |
ConstraintCheck<Runtype<A>> | Custom function that returns true if the constraint is satisfied, false or a custom error message if not. |
options? |
object | - |
options.args? |
K | - |
options.name? |
string | - |
Returns: Constraint<Runtype<A>, T, K>
Defined in: src/runtype.ts:74
▸ withGuard<T, K>(guard
: (x
: A) => x is T, options?
: { args?
: K ; name?
: string }): Constraint<Runtype<A>, T, K>
Helper function to convert an underlying Runtype into another static type via a type guard function. The static type of the runtype is inferred from the type of the guard function.
Name | Default | Description |
---|---|---|
T |
- | Typically inferred from the return type of the type guard function, so usually not needed to specify manually. |
K |
unknown | - |
Name | Type | Description |
---|---|---|
guard |
(x : A) => x is T |
Type guard function (see https://www.typescriptlang.org/docs/handbook/advanced-types.html#user-defined-type-guards) |
options? |
object | - |
options.args? |
K | - |
options.name? |
string | - |
Returns: Constraint<Runtype<A>, T, K>
Defined in: src/runtype.ts:94