/* eslint-disable @typescript-eslint/no-empty-interface */ /* eslint-disable @typescript-eslint/ban-types */ /** * @see https://devblogs.microsoft.com/typescript/announcing-typescript-2-9-2/#support-for-symbols-and-numeric-literals-in-keyof-and-mapped-object-types */ export type KeyofBase = keyof any /** * @see {@link https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Data_types} */ export type Primative = string | number | boolean | bigint | symbol | null | undefined /** * @see {@link https://www.typescriptlang.org/docs/handbook/release-notes/typescript-2-2.html#object-type} */ export type NonPrimative = object export interface EmptyInterface {} export type Cast = T extends SafeT ? T : SafeT export type Try = T extends SafeT ? T : Catch /** * Predicate whether the target is `undefined`. */ export type IsUndefined = Exclude extends never ? true : false export type IfUndefined = IsUndefined extends true ? TRUE : FALSE export type CastUndefined = T extends undefined ? T : undefined /** * Predicate whether the target is `null`. */ export type IsNull = Exclude extends never ? true : false export type IfNull = IsNull extends true ? TRUE : FALSE export type CastNull = T extends null ? T : null /** * Predicate whether the target is `null | undefined`. */ export type IsNil = Exclude extends never ? true : false export type IfNil = IsNil extends true ? TRUE : FALSE export type CastNil = T extends undefined | null ? T : undefined | null export type IsUndefinedable = T extends Exclude ? false : true export type IsNullable = T extends Exclude ? false : true export type IsNilable = T extends Exclude ? false : true export type IfUndefinedable = IsUndefinedable extends true ? TRUE : FALSE export type IfNullable = IsNullable extends true ? TRUE : FALSE export type IfNilable = IsNilable extends true ? TRUE : FALSE export type Undefinedable = T | undefined export type Nullable = T | null export type Nilable = T | null | undefined export type NonUndefinedable = Exclude export type NonNullable = Exclude export type NonNilable = Exclude /** * Predicate whether the target is `never`. */ export type IsNever = [T] extends [never] ? true : false export type IfNever = IsNever extends true ? TRUE : FALSE export type CastNever = T extends never ? T : never /** * Predicate whether the target is `any`. * @see Copyright - The idea is get from {@link https://stackoverflow.com/questions/49927523/disallow-call-with-any/49928360#49928360} */ export type IsAny = 0 extends (1 & T) ? true : false export type IfAny = 0 extends (1 & T) ? TRUE : FALSE export type CastAny = T extends any ? T : any /** * @see {@link https://www.typescriptlang.org/docs/handbook/type-compatibility.html#any-unknown-object-void-undefined-null-and-never-assignability} */ export type IsUnknown = IsNever extends true ? false : ( T extends unknown ? ( unknown extends T ? (IsAny extends true ? false : true) : false ) : false ) export type IfUnknown = IsUnknown extends true ? TRUE : FALSE export type CastUnknown = T extends unknown ? T : unknown /** * Predicate whether the target is `boolean`. */ export type IsBoolean = T extends boolean ? true : false export type IfBoolean = IsBoolean extends true ? TRUE : FALSE export type CastBoolean = T extends boolean ? T : boolean // FIXME: there is a bug export type IsBooleanLiteral = T extends boolean ? (boolean extends T ? false : true) : false /** * Predicate whether the target is `string`. */ export type IsString = T extends string ? true : false export type IfString = IsString extends true ? TRUE : FALSE export type CastString = T extends string ? T : string export type IsStringLiteral = T extends string ? (string extends T ? false : true) : false /** * Predicate whether the target is `number`. */ export type IsNumber = T extends number ? true : false export type IfNumber = IsNumber extends true ? TRUE : FALSE export type CastNumber = T extends number ? T : number export type IsNumberLiteral = T extends number ? (number extends T ? false : true) : false /** * Predicate whether the target is `symbol`. */ export type IsSymbol = T extends symbol ? true : false export type IfSymbol = IsSymbol extends true ? TRUE : FALSE export type CastSymbol = T extends symbol ? T : symbol /** * Predicate whether the target is of type function. */ export type IsFunction = T extends AnyFunction ? true : false export type IfFunction = IsFunction extends true ? TRUE : FALSE export type CastFunction = T extends AnyFunction ? T : AnyFunction type ExpandedDistribution = T extends any ? T[] : never type ClosedDistribution = [T] extends [any] ? T[] : never /** * Predicate whether the target is `Union` type. * * @see https://www.typescriptlang.org/docs/handbook/2/conditional-types.html#distributive-conditional-types */ export type IsUnion = ClosedDistribution extends ExpandedDistribution ? false : true export type IfUnion = IsUnion extends true ? TRUE : FALSE /** * Predicate whether the target is `Array`. */ export type IsArray = T extends readonly any[] ? (number extends T['length'] ? true : false) : false export type IfArray = IsArray extends true ? TRUE : FALSE // ReadonlyArray is subset of Array. export type IsReadonlyArray = T extends readonly any[] ? (T extends any[] ? false : true) : false export type IfReadonlyArray = IsReadonlyArray extends true ? TRUE : FALSE export type IsWritableArray = T extends any[] ? (number extends T['length'] ? true : false) : false export type IfWritableArray = IsWritableArray extends true ? TRUE : FALSE /** * Predicate whether the target is `Tuple`. * * @see https://github.com/type-challenges/type-challenges/issues/4491#issuecomment-975808109 */ export type IsTuple = T extends readonly any[] ? (number extends T['length'] ? false : true) : false export type IfTuple = IsTuple extends true ? TRUE : FALSE export type IsReadonlyTuple = T extends readonly any[] ? (T extends any[] ? false : true) : false export type IfReadonlyTuple = IsReadonlyTuple extends true ? TRUE : FALSE export type IsWritableTuple = T extends any[] ? (number extends T['length'] ? false : true) : false export type IfWritableTuple = IsWritableTuple extends true ? TRUE : FALSE /** * Predicate whether the target is `Array` or `Tuple`. */ export type IsArrayOrTuple = T extends readonly any[] ? true : false export type IfArrayOrTuple = IsArrayOrTuple extends true ? TRUE : FALSE export type IsRecord = T extends Record ? true : false export type IfRecord = IsRecord extends true ? TRUE : FALSE export type IsStringRecord = T extends Record ? true : false export type IfStringRecord = IsStringRecord extends true ? TRUE : FALSE export type IsNumberRecord = T extends Record ? true : false export type IfNumberRecord = IsNumberRecord extends true ? TRUE : FALSE export type IsSymbolRecord = T extends Record ? true : false export type IfSymbolRecord = IsSymbolRecord extends true ? TRUE : FALSE /** * Predicate whether the target is `Record`. */ export type IsEmptyObject> = keyof Target extends never ? true : false export type IfEmptyObject, TRUE = true, FALSE = never> = IsEmptyObject extends true ? TRUE : FALSE export type IsPrimative = T extends string ? true : T extends number ? true : T extends boolean ? true : T extends symbol ? true : T extends bigint ? true : T extends undefined ? true : T extends null ? true :false export type IfPrimative = IsPrimative extends true ? TRUE : FALSE export type IsPrimativeUnion = T extends Primative ? true : false export type IfPrimativeUnion = IsPrimativeUnion extends true ? TRUE : FALSE /** * @see Copyright - The code is copy from {@Link https://github.com/millsp/ts-toolbelt/blob/319e55123b9571d49f34eca3e5926e41ca73e0f3/sources/Any/Keys.ts#L11} */ export type Keys = A extends readonly any[] ? Exclude | number : keyof A