Options
All
  • Public
  • Public/Protected
  • All
Menu

External module "types/type-description"

Index

Type aliases

TypeDescrObject

TypeDescrObject: object

Makes an alias for the type that maps properties of TTargetObject to TypeDescription's

remarks

Use it like so:

import * as Vts from 'vee-type-safe';
export interface Human {
    name: string;
    age: number;
}

export const HumanTD: Vts.TypeDescrObject<Human> = {
    name: 'string',
    age: Vts.isPositiveInteger // you will get better intellisense here
};

Type declaration

TypeDescription

TypeDescription: TypePredicate<TTarget> | TypeDescrSet<TTarget> | TTarget extends number ? "number" : TTarget extends string ? "string" | RegExp : TTarget extends boolean ? "boolean" : TTarget extends bigint ? "bigint" : TTarget extends undefined ? "undefined" : TTarget extends null ? "object" : TTarget extends symbol ? "symbol" : TTarget extends Function ? "function" : TTarget ex...

Defines a type that describes another type's shape. It is accepted by type matching functions like mismatch(), conforms(), duckMismatch(), exactlyConforms(). This type is a meta-type, as it describes limitations over other types.

remarks

There exists a bunch of ways you may describe your type with TypeDescription. E.g. you can describe number type the following way:

type TypeDescription<number> =
 | 'number'
 | (suspect: unknown) => suspect is number
 | Set<TypeDescription<number>>

So if you need to create a type description, use more specific subtype of it, rather than using default union type.

There is a handy function td() that preserves literally all types you used to define your TypeDescription, thus you may further retrieve the type, that is described by your TypeDescription without having to write it yourself. If you want to get the described type from the given TypeDescription, see TypeDescriptionTarget.

Example:

import * as Vts from 'vee-type-safe';

const NameTD = Vts.td({
    first: 'string',
    last:  'string'
});

type Name = Vts.TypeDescriptionTarget<typeof NameTD>;

// statically generated TypeScript type:
// type Name === {
//     first: string;
//     last:  string;
// }

const JsonUserTD = Vts.td({
    name:     NameTD,
    password: /[a-zA-Z]{3,32}/,
    email:    (suspect): suspect is string => {
        // instert custom logic to check that suspect is an email string here
        return true;
    },
    cash:       Vts.isInteger,
    isDisabled: 'boolean'
});

type JsonUser = Vts.TypeDescriptionTarget<typeof JsonUserTD>;

// statically generated TypeScript
// type JsonUser === {
//     name:       Name;
//     password:   string;
//     email:      string;
//     cash:       number;
//     isDisabled: boolean;
// }

TypeDescriptionTarget

TypeDescriptionTarget: TTypeDescr extends TypeDescrSet<any> ? TTypeDescr extends Set<infer TItems> ? TItems extends TypeDescription<any> ? TItems extends TypeDescrSet<any> ? TItems extends Set<infer TItems> ? TItems extends TypeDescription<any> ? TItems extends TypeDescrSet<...> ? TItems extends Set<...> ? TItems extends TypeDescription<....

Defines a type that is described by the given TTypeDescr type description.

remarks

This type is basically inverse to TypeDescription, but not exactly. Because of TypeScript circular types limitation enough nested TypeDescrSet type will cause to return never type.

This type aspires to support to the following equality: TypeDescriptionTarget<TypeDescription<T>> === T But it doesn't, because TypeDesescription<T> must be a more specific subtype of TypeDescription. You should make it using td() function.

See TypeDescription for more explanation.

TypePredicate

TypePredicate: TTarget extends unknown ? (suspect: unknown) => boolean : (suspect: unknown) => suspect is TTarget

Generated using TypeDoc