UNPKG

@freik/typechk

Version:

Core set of TS/JS type checking helpers

496 lines (495 loc) 21.2 kB
import { SimpleObject, boolcheck, typecheck } from './Types.js'; /** * Type check for undefined * * @param {unknown} obj - The value being checked * @returns {boolean} true iff `obj` is undefined */ export declare function isUndefined(obj: unknown): obj is undefined; /** * Type check for a defined value * * @param {unknown} obj - The value being checked * @returns true if the `obj` is NOT undefined */ export declare function isDefined(obj: unknown): obj is NonNullable<unknown> | null; /** * Type check for null * * @param {unknown} obj - The value being checked * @returns true if the object is null */ export declare function isNull(obj: unknown): obj is null; /** * Type check for null or undefined * * @param {unknown} obj - The value being checked * @returns true if the object is null or undefined */ export declare function isEmpty(obj: unknown): obj is null | undefined; /** * Type check for NOT empty (nonnullable) * * @param {unknown} obj - The value being checked * @returns true if the object is a NonNullable */ export declare function isNonNullable(obj: unknown): obj is NonNullable<unknown>; /** * Type check for one of two types * * @param {unknown} obj - The value being checked * @param chk1 - Type Checker #1 * @param chk2 - Type Checker #2 * @returns true if the object is either type T or type U * * @deprecated Use {@link isAnyOf} instead. */ export declare function isOneOf<T, U>(obj: unknown, chk1: typecheck<T>, chk2: typecheck<U>): obj is T | U; /** * Generate a type check function for one of two types * * @param chk1 - Type Checker #1 * @param chk2 - Type Checker #2 * @returns A type check function for {@link isOneOf} * * @deprecated Use {@link chkAnyOf} instead. */ export declare function chkOneOf<T, U>(chk1: typecheck<T>, chk2: typecheck<U>): typecheck<T | U>; /** * Type check for meeting both of two types * * @param {unknown} obj - The value being checked * @param chk1 - Type Checker #1 * @param chk2 - Type Checker #2 * @returns true if the object is both type T and type U * * @deprecated Use {@link isAllOf} instead. */ export declare function isBothOf<T, U>(obj: unknown, chk1: typecheck<T>, chk2: typecheck<U>): obj is T & U; /** * Generate a type check function for two types * * @param chk1 - Type Checker #1 * @param chk2 - Type Checker #2 * @returns A type check function for {@link isOneOf} * * @deprecated Use {@link chkAllOf} instead. */ export declare function chkBothOf<T, U>(chk1: typecheck<T>, chk2: typecheck<U>): typecheck<T & U>; type InferType<C> = C extends typecheck<infer T> ? T : never; export declare function isAnyOf<Checkers extends readonly [typecheck<any>, ...typecheck<any>[]]>(obj: unknown, ...checks: Checkers): obj is InferType<Checkers[number]>; export declare function chkAnyOf<Checkers extends readonly [typecheck<any>, ...typecheck<any>[]]>(...checks: Checkers): typecheck<InferType<Checkers[number]>>; type UnionToIntersection<U> = (U extends any ? (k: U) => void : never) extends (k: infer I) => void ? I : never; export declare function isAllOf<Checkers extends readonly [typecheck<any>, ...typecheck<any>[]]>(obj: unknown, ...checks: Checkers): obj is UnionToIntersection<InferType<Checkers[number]>>; export declare function chkAllOf<Checkers extends readonly [typecheck<any>, ...typecheck<any>[]]>(...checks: Checkers): typecheck<UnionToIntersection<InferType<Checkers[number]>>>; /** * Type check for a non-null object * * @param {unknown} obj - The value being checked * @returns true if the value is an object, and is not null */ export declare function isObjectNonNull(obj: unknown): obj is NonNullable<object>; /** * Type check for object (or null) * * @param {unknown} obj - The value being checked * @returns True if obj is null, or an object (of any type) */ export declare function isObject(obj: unknown): obj is object | null; /** * Type check for an array (of any type) * * @param {unknown} obj - The value being checked * @returns True if obj is an array (of any type) */ export declare function isArray(obj: unknown): obj is unknown[]; /** * Type check for 1 element tuple. * * @param {unknown} obj - the value being checked * @returns True if obj is a 1 element tuple (of any type!) * * @deprecated use {@link isTupleOf} instead. */ export declare function is1Tuple(obj: unknown): obj is [unknown]; /** * Type check for 2 element tuples * * @param {unknown} obj - The value being checked * @returns True of obj is a 2 element tuple (of any type!) * * @deprecated use {@link isTupleOf} instead. */ export declare function is2Tuple(obj: unknown): obj is [unknown, unknown]; /** * Type check for 3 element tuples * * @param {unknown} obj - The value being checked * @returns True of obj is a 3 element tuple (of any type!) * * @deprecated use {@link isTupleOf} instead. */ export declare function is3Tuple(obj: unknown): obj is [unknown, unknown, unknown]; /** * Generate a 'coercion' function that will return a value of type T, * either the object provided to the function, or `defVal` if it's not that type * * @param chk - The typecheck<T> function to validate the type * @param defVal - The value to return of the object isn't the correct type * @returns A function that takes an object and coerces it to type T */ export declare function as<T>(chk: typecheck<T>, defVal: T): (o: unknown) => T; /** * Type check for a string * * @param {unknown} obj - The value being checked * @returns True if `obj` is a string */ export declare function isString(obj: unknown): obj is string; /** * Type filtering for strings. Will NOT coerce the thing to a string.. * * @param {unknown} obj - The value being coerced * @param notStr = the value to return if obj is not a string * @returns Either obj or notStr (whichever is a string) */ export declare function asString(obj: unknown, notStr?: string): string; /** * Type coercion to a string. Will try to *lightly* coerce the thing to a string if possible. * * @param {unknown} obj - The value being coerced * @param notStr = the value to return if obj is not a string * @returns Either obj, obj.toString() or notStr (in that preferred order) */ export declare function toString(obj: unknown, notStr?: string): string; /** * Type check for number (and not NaN) * * @param {unknown} obj - The value being checked * @returns True if obj is a number and NOT a NaN */ export declare function isNumber(obj: unknown): obj is number; /** * If obj is a number (and not a NaN!) return that value, otherwise, return notNum * * @param {unknown} obj - The value being coerced * @param notNum - The value to return if obj is not a number (or a NaN) * @returns obj, if it's a number, otherwise returns nonNum */ export declare function asNumber(obj: unknown, notNum: number): number; /** * Type check for number (and not NaN) or a string * * @param {unknown} obj - The value being checked * @returns True if obj is a number and NOT a NaN, or a string */ export declare const isNumberOrString: typecheck<number | string>; /** * If obj is a number (and not NaN) or a string, return that values, otherwise return notNumOrStr * @param {unknown} obj * @param {number|string} notNumOrStr * @returns {number|string} obj, if it's a number and NOT a NaN, or a string, otherwise notNumOrStr */ export declare function asNumberOrString(obj: unknown, notNumOrStr: number | string): number | string; /** * Type check for boolean * @param {unknown} obj * @returns {boolean} true iff obj is a boolean expression */ export declare function isBoolean(obj: unknown): obj is boolean; /** * Type check for Date * @param {unknown} obj * @returns {boolean} true iff obj is a Dat */ export declare function isDate(obj: unknown): obj is Date; /** * Type check for BigInt * @param {unknown} obj * @returns {boolean} true iff obj is a BigInt */ export declare function isBigInt(obj: unknown): obj is bigint; /** * Type check for Function * @param {unknown} obj * @returns {boolean} true iff obj is a function */ export declare function isFunction(obj: unknown): obj is Function; /** * Type check for RegExp * @param {unknown} obj * @returns {boolean} true iff obj is a Regular Expression */ export declare function isRegex(obj: unknown): obj is RegExp; /** * Type check for Map * @param {unknown} obj * @returns {boolean} true iff obj is a Map (of any key and value type) */ export declare function isMap(obj: unknown): obj is Map<unknown, unknown>; /** * Type check for Set * @param {unknown} obj * @returns {boolean} true iff obj is a Set (of any key type) */ export declare function isSet(obj: unknown): obj is Set<unknown>; /** * Type check for T[] (Array<T>) * @param {unknown} obj * @param {typecheck<T>} chk - TypeCheck function for Type T * @returns {boolean} true iff obj is an Array of type T (or an empty array!) */ export declare function isArrayOf<T>(obj: unknown, chk: typecheck<T>): obj is T[]; /** * Generate a type check function for T[] (Array<T>) * @param {typecheck<T>} chk - TypeCheck function for Type T * @returns {typecheck<T[]>} return a typechk function for {@link isArrayOf} */ export declare function chkArrayOf<T>(chk: typecheck<T>): typecheck<T[]>; /** * Type check for Tuple of [T] * @param {unknown} obj * @param {typecheck<T>} t - Typecheck function for Type T * @returns {boolean} true iff obj is a Tuple of type [T] * * @deprecated Use {@link isTupleOf} instead. */ export declare function is1TupleOf<T>(obj: unknown, t: typecheck<T>): obj is [T]; /** * Generate a type check function for Tuple of [T] * @param t - TypeCheck function for Type T * @returns {boolean} true iff obj is a Tuple of type [T] * * @deprecated Use {@link chkTupleOf} instead. */ export declare function chk1TupleOf<T>(t: typecheck<T>): typecheck<[T]>; /** * Type check for Tuple of [T, U] * @param {unknown} obj * @param {typecheck<T>} t - TypeCheck function for Type T * @param {typecheck<U>} u - TypeCheck function for Type U * @returns {boolean} true iff obj is a Tuple of type [T, U] * * @deprecated Use {@link isTupleOf} instead. */ export declare function is2TupleOf<T, U>(obj: unknown, t: typecheck<T>, u: typecheck<U>): obj is [T, U]; /** * Generate a type check function for Tuple of [T, U] * @param {typecheck<T>} t - TypeCheck function for Type T * @param {typecheck<U>} u - TypeCheck function for Type U * @returns {typecheck<[T, U]>} * * @deprecated Use {@link chkTupleOf} instead. */ export declare function chk2TupleOf<T, U>(t: typecheck<T>, u: typecheck<U>): typecheck<[T, U]>; /** * Type check for Tuple of [T, U, V] * @param {unknown} obj * @param {typecheck<T>} t - TypeCheck function for Type T * @param {typecheck<U>} u - TypeCheck function for Type U * @param {typecheck<V>} v - TypeCheck function for Type V * @returns {obj_is<Tuple<T, U, V>>} * * @deprecated Use {@link isTupleOf} instead. */ export declare function is3TupleOf<T, U, V>(obj: unknown, t: typecheck<T>, u: typecheck<U>, v: typecheck<V>): obj is [T, U, V]; /** * Generate a type check function for Tuple of [T, U, V] * @param {typecheck<T>} t - TypeCheck function for Type T * @param {typecheck<U>} u - TypeCheck function for Type U * @param {typecheck<V>} v - TypeCheck function for Type V * @returns {typecheck<[T, U, V]>} * * @deprecated Use {@link chkTupleOf} instead. */ export declare function chk3TupleOf<T, U, V>(t: typecheck<T>, u: typecheck<U>, v: typecheck<V>): typecheck<[T, U, V]>; /** * Type check for [tuples, of, different, types] * @param value {unknown} obj * @param checkers {...typecheck<T[...K]>} TypeCheck functions for each item of the tuples * @returns {typecheck<T[...K]>} true iff obj is a tuple of [...K] type */ export declare function isTupleOf<T extends unknown[]>(obj: unknown, ...checkers: { [K in keyof T]: typecheck<T[K]>; }): obj is T; /** * Generate a typechecking function for tuples * @param checkers {...typecheck<T[...K]>} TypeCheck functions for each item of the tuples * @returns {typecheck<T>} A typechecking function for tuples */ export declare function chkTupleOf<T extends unknown[]>(...checkers: { [K in keyof T]: typecheck<T[K]>; }): typecheck<T>; /** * Type check for string[] * @param {unknown} obj * @returns {obj_is<string[]>} */ export declare function isArrayOfString(obj: unknown): obj is string[]; /** * Filter obj to an array of strings. If defVal is an array of strings, even if * a single element of obj is not a string, defVal will be used instead. If * defVal is a string, it will be used to replace any values in obj that are not * strings. If defVal isn't provided, only strings will be left in obj. * @param {unknown} obj * @param {string[]|string} defVal? * @returns {string[]} */ export declare function asArrayOfString(obj: unknown, defVal?: string[] | string): string[]; /** * Coerce obj to an array of strings. If defVal is an array of strings, even if * a single element of obj is not a string, defVal will be used instead. If * defVal is a string, it will be used to replace any values in obj that cannot * be coerced to strings. If defVal isn't provided, only strings, or items that * can be coerced to strings, will be left in obj. * @param obj - The value being coerced to `string[]` * @param defVal? - The default value used for coercion */ export declare function toArrayOfString(obj: unknown, defVal?: string[] | string): string[]; /** * Type check for Map<K, V> * @param {unknown} obj * @param {typecheck<K>} key - A K type checking function (obj:unknown) => obj is K * @param {typecheck<V>} val - A V type checking function (obj:unknown) => obj is V * @returns {obj_is<Map<K, V>>} */ export declare function isMapOf<K, V>(obj: unknown, key: typecheck<K>, val: typecheck<V>): obj is Map<K, V>; /** * Generate a type check function for Map<K, V> * @param {typecheck<K>} key - A K type checking function (obj:unknown) => obj is K * @param {typecheck<V>} val - A V type checking function (obj:unknown) => obj is V * @returns {typecheck<Map<K, V>>} */ export declare function chkMapOf<K, V>(key: typecheck<K>, val: typecheck<V>): typecheck<Map<K, V>>; /** * Type check for Map<string, string> * @param {unknown} obj * @returns {obj_is<Map<string, string>>} */ export declare function isMapOfStrings(obj: unknown): obj is Map<string, string>; /** * Type check for Set<T> * @param {unknown} obj * @param {typecheck<T>} chk - A T type checking function (obj:unknow) => obj is T * @returns {obj_is<Set<T>>} */ export declare function isSetOf<T>(obj: unknown, chk: typecheck<T>): obj is Set<T>; /** * Generate a type check function for Set<T> * @param {typecheck<T>} chk - A T type checking function (obj:unknow) => obj is T * @returns {typecheck<Set<T>>} */ export declare function chkSetOf<T>(chk: typecheck<T>): typecheck<Set<T>>; /** * Type check for Set<string> * @param {unknown} obj * @returns {obj_is<Set<string>>} */ export declare function isSetOfString(obj: unknown): obj is Set<string>; /** * Type check of \{ [key: string | symbol]: T \} types * @param {unknown} obj * @param {typecheck<T>} chk - a T type-checking function (obj: unknown) => obj is T * @returns {obj_is<{any:T}>} */ export declare function isObjectOf<T>(obj: unknown, chk: typecheck<T>): obj is NonNullable<{ [key: string | number | symbol]: T; }>; /** * Type check of \{ [key: string | symbol]: T \} types * @param {unknown} obj * @param {typecheck<T>} chk - a T type-checking function (obj: unknown) => obj is T * @returns {obj_is<{any:T}>} */ export declare function chkObjectOf<T>(chk: typecheck<T>): typecheck<{ [key: string | number | symbol]: T; }>; /** * Type checking function for \{ [key: string | symbol]: string \} types * @param {unknown} obj * @returns {obj_is<{any:string}>} */ export declare function isObjectOfString(obj: unknown): obj is { [key: string]: string; }; /** * Type check function for a Promise<T>, though T is not (and can't be...) validated. * This is a simple check for a "then-able" object type. * @param {unknown} obj * @returns {obj_is<Promise<T>>} */ export declare function isPromise(obj: unknown): obj is Promise<unknown>; /** * Pay no attention to the man behind the curtain!!! * @param obj * @returns */ export declare function isPromiseOf<T>(obj: unknown): obj is Promise<T>; /** * Type check for a Javascript symbol type * @param {unknown} obj * @returns {obj_is<symbol>} */ export declare function isSymbol(obj: unknown): obj is symbol; /** * Type check for a particular key in obj. * After a conditional, you can use obj[key] or obj.key safely. * @param {unknown} obj * @param {K} key * @returns {obj_is<{key: unknown}>} */ export declare function hasField<Obj extends object, K extends PropertyKey>(obj: Obj, key: K): obj is Obj & Record<K, unknown>; export declare function hasField<K extends PropertyKey>(obj: unknown, key: K): obj is Record<K, unknown>; export declare function chkField<Obj extends object, K extends PropertyKey>(key: K): typecheck<Obj & Record<K, unknown>>; export declare function chkField<K extends PropertyKey>(key: K): typecheck<Record<K, unknown>>; /** * Type check for a key of type T in obj. * After a conditional, you can use obj[key] or obj.key with the type T for * key safely. * @param {unknown} obj * @param {K} key * @param {typecheck<T>} checker - A Type checking function for T * @returns {obj_is<{key: T}>} */ export declare function hasFieldOf<T, Obj extends object, K extends PropertyKey>(obj: Obj, key: K, checker: typecheck<T>): obj is Obj & Record<K, T>; export declare function hasFieldOf<T, K extends PropertyKey>(obj: unknown, key: K, checker: typecheck<T>): obj is Record<K, T>; export declare function chkFieldOf<T, Obj extends object, K extends PropertyKey>(key: K, checker: typecheck<T>): typecheck<Obj & Record<K, T>>; export declare function chkFieldOf<T, K extends PropertyKey>(key: K, checker: typecheck<T>): typecheck<Record<K, T>>; export declare const hasFieldType: typeof hasFieldOf; export declare const chkFieldType: typeof chkFieldOf; /** * Type check for a string typed key in obj. * After a conditional, you can use obj[key] or obj.key as a string safely. * @param {unknown} obj * @param {K} key * @returns {obj_is<{key: string}>} */ export declare function hasStrField<K extends PropertyKey>(obj: unknown, key: K): obj is Record<K, string>; export declare function chkStrField<K extends PropertyKey>(key: K): typecheck<Record<K, string>>; export declare function isIterable(obj: unknown): obj is NonNullable<{ [Symbol.iterator]: () => IterableIterator<unknown>; }>; export declare function isIterableOf<T>(obj: unknown): obj is NonNullable<{ [Symbol.iterator]: () => IterableIterator<T>; }>; export declare function isSimpleObject(x: unknown): x is SimpleObject; export declare function asSimpleObject(x: unknown, def?: SimpleObject): SimpleObject; export declare function isCustomType<T>(obj: unknown, sym: symbol): obj is T; export declare function chkCustomType<T>(sym: symbol): typecheck<T>; export type OptionalKeysOf<BaseType extends object> = Exclude<{ [Key in keyof BaseType]: BaseType extends Record<Key, BaseType[Key]> ? never : Key; }[keyof BaseType], undefined>; export type RequiredKeysOf<BaseType extends object> = Exclude<{ [Key in keyof BaseType]: BaseType extends Record<Key, BaseType[Key]> ? Key : never; }[keyof BaseType], undefined>; export declare function isObjectOfExactType<T extends NonNullable<object>>(obj: unknown, requiredFields: Record<RequiredKeysOf<T>, boolcheck>, optionalFields?: Record<OptionalKeysOf<T>, boolcheck> | Record<string, never>): obj is T; export declare function isObjectOfType<T extends NonNullable<object>>(obj: unknown, requiredFields: Record<RequiredKeysOf<T>, boolcheck>, optionalFields?: Record<OptionalKeysOf<T>, boolcheck> | Record<string, never>): obj is T; export declare function chkObjectOfType<T extends object>(requiredFields: Record<RequiredKeysOf<T>, boolcheck>, optionalFields?: Record<OptionalKeysOf<T>, boolcheck> | Record<string, never>): typecheck<T>; export declare function chkObjectOfExactType<T extends object>(requiredFields: Record<RequiredKeysOf<T>, boolcheck>, optionalFields?: Record<OptionalKeysOf<T>, boolcheck> | Record<string, never>): typecheck<T>; export declare function isPartialOf<T extends object>(obj: unknown, fields: Record<keyof T, boolcheck>): obj is Partial<T>; export declare function chkPartialOf<T extends object>(fields: Record<keyof T, boolcheck>): typecheck<Partial<T>>; export declare function isRecord<K extends string | number | symbol>(obj: unknown, keyChk: typecheck<K>): obj is Record<K, unknown>; export declare function chkRecord<K extends string | number | symbol>(keyChk: typecheck<K>): typecheck<Record<K, unknown>>; export declare function isRecordOf<K extends string | number | symbol, V>(obj: unknown, keyChk: typecheck<K>, valChk: typecheck<V>): obj is Record<K, V>; export declare function chkRecordOf<K extends string | number | symbol, V>(keyChk: typecheck<K>, valChk: typecheck<V>): typecheck<Record<K, V>>; export {};