UNPKG

ts-guardian

Version:
95 lines (94 loc) 6.15 kB
declare type BasicTypeMap = { any: any; boolean: boolean; bigint: bigint; function: Function; null: null; number: number; object: object; string: string; symbol: symbol; undefined: undefined; unknown: unknown; }; declare type BasicTypeDef = keyof BasicTypeMap; declare type BasicArrayTypeDef = `${BasicTypeDef}[]`; declare type BasicOptionalTypeDef = `${Exclude<BasicTypeDef | BasicArrayTypeDef, 'undefined' | 'unknown' | 'any'>}?`; declare type Literal = string | number | boolean; declare type Instance = new (...args: any[]) => any; interface ObjectTypeDef extends Record<PropertyKey, TypeDef> { } declare type TupleTypeDef = [] | (BasicTypeDef | BasicArrayTypeDef | BasicOptionalTypeDef | ObjectTypeDef | Guard<any>)[]; declare type TypeDef = BasicTypeDef | BasicArrayTypeDef | BasicOptionalTypeDef | ObjectTypeDef | TupleTypeDef | Guard<any>; declare type BasicTypeDefType<T extends BasicTypeDef> = BasicTypeMap[T]; declare type StripArrayBrackets<T extends string> = T extends `${infer U}[]` ? U : never; declare type StripOptionalMark<T extends string> = T extends `${infer U}?` ? U : never; declare type TypeDefType<TTypeDef extends unknown> = TTypeDef extends Guard<infer V> ? V : TTypeDef extends BasicTypeDef ? BasicTypeDefType<TTypeDef> : TTypeDef extends BasicArrayTypeDef ? BasicTypeDefType<StripArrayBrackets<TTypeDef>>[] : TTypeDef extends BasicOptionalTypeDef ? (StripOptionalMark<TTypeDef> extends infer Stripped ? Stripped extends BasicArrayTypeDef ? BasicTypeDefType<StripArrayBrackets<Stripped>>[] : Stripped extends BasicTypeDef ? BasicTypeDefType<Stripped> : never : never) | undefined : TTypeDef extends ObjectTypeDef | TupleTypeDef ? { [key in keyof TTypeDef]: TypeDefType<TTypeDef[key]>; } : never; export declare type Guard<T extends unknown> = { (value: unknown): value is T; or: <U extends TypeDef>(t: U) => Guard<T | TypeDefType<U>>; and: <U extends TypeDef>(t: U) => Guard<T & TypeDefType<U>>; orArrayOf: <U extends TypeDef>(t: U) => Guard<T | TypeDefType<U>[]>; orRecordOf: <U extends TypeDef>(t: U) => Guard<T | Record<PropertyKey, TypeDefType<U>>>; orLiterally: <U extends Literal[]>(...t: U) => Guard<T | U[number]>; orInstanceOf: <U extends Instance>(t: U) => Guard<T | (U extends new (...args: any[]) => infer V ? V : never)>; }; export declare type GuardType<T extends Guard<any>> = T extends (value: unknown) => value is infer U ? U : never; export declare const is: <T extends TypeDef>(t: T) => Guard<TypeDefType<T>>; export declare const isArrayOf: <T extends TypeDef>(t: T) => Guard<TypeDefType<T>[]>; export declare const isRecordOf: <T extends TypeDef>(t: T) => Guard<Record<PropertyKey, TypeDefType<T>>>; export declare const isLiterally: <T extends Literal[]>(...t: T) => Guard<T[number]>; export declare const isInstanceOf: <T extends Instance>(t: T) => Guard<T extends new (...args: any[]) => infer V ? V : never>; export declare const isOptional: <T extends TypeDef>(t: T) => Guard<TypeDefType<T> | undefined>; export declare const isNullable: <T extends TypeDef>(t: T) => Guard<TypeDefType<T> | null>; export declare const isNullish: <T extends TypeDef>(t: T) => Guard<TypeDefType<T> | null | undefined>; /** @deprecated Use `is('boolean')` instead. */ export declare const isBoolean: Guard<boolean>; /** @deprecated Use `is('bigint')` instead. */ export declare const isBigint: Guard<bigint>; /** @deprecated Use `is('function')` instead. */ export declare const isFunction: Guard<Function>; /** @deprecated Use `is('null')` instead. */ export declare const isNull: Guard<null>; /** @deprecated Use `is('number')` instead. */ export declare const isNumber: Guard<number>; /** @deprecated Use `is('string')` instead. */ export declare const isString: Guard<string>; /** @deprecated Use `is('symbol')` instead. */ export declare const isSymbol: Guard<symbol>; /** @deprecated Use `is('undefined')` instead. */ export declare const isUndefined: Guard<undefined>; /** @deprecated Use `isNullable('boolean')` instead. */ export declare const isBooleanOrNull: Guard<boolean | null>; /** @deprecated Use `isNullable('bigint')` instead. */ export declare const isBigintOrNull: Guard<bigint | null>; /** @deprecated Use `isNullable('function')` instead. */ export declare const isFunctionOrNull: Guard<Function | null>; /** @deprecated Use `isNullable('number')` instead. */ export declare const isNumberOrNull: Guard<number | null>; /** @deprecated Use `isNullable('string')` instead. */ export declare const isStringOrNull: Guard<string | null>; /** @deprecated Use `isNullable('symbol')` instead. */ export declare const isSymbolOrNull: Guard<symbol | null>; /** @deprecated Use `is('boolean?')` instead. */ export declare const isBooleanOrUndefined: Guard<boolean | undefined>; /** @deprecated Use `is('bigint?')` instead. */ export declare const isBigintOrUndefined: Guard<bigint | undefined>; /** @deprecated Use `is('function?')` instead. */ export declare const isFunctionOrUndefined: Guard<Function | undefined>; /** @deprecated Use `is('null?')` instead. */ export declare const isNullOrUndefined: Guard<null | undefined>; /** @deprecated Use `is('number?')` instead. */ export declare const isNumberOrUndefined: Guard<number | undefined>; /** @deprecated Use `is('string?')` instead. */ export declare const isStringOrUndefined: Guard<string | undefined>; /** @deprecated Use `is('symbol?')` instead. */ export declare const isSymbolOrUndefined: Guard<symbol | undefined>; declare type ParserReturn<T, TGuard extends Guard<any>> = T extends undefined ? GuardType<TGuard> | undefined : GuardType<TGuard> extends T ? T | undefined : never; export declare const parserFor: <T extends unknown = undefined, TGuard extends Guard<any> = Guard<T>>(guard: TGuard) => (value: any) => ParserReturn<T, TGuard>; export declare const requireThat: <T extends any>(value: any, guard: Guard<T>, errorMessage?: string) => asserts value is T; /** @deprecated Use requireThat instead */ export declare const assertThat: <T extends unknown>(value: any, guard: Guard<T>, errorMessage?: string | undefined) => asserts value is T; export {};