// oxlint-disable no-explicit-any /** * @description 表示 JavaScript 与 TypeScript 中的原始类型联合。 * * @see {@link https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Data_types} */ export type Primitive = string | number | boolean | bigint | symbol | null | undefined /** * @description 表示非原始类型。 * * @see {@link https://www.typescriptlang.org/docs/handbook/release-notes/typescript-2-2.html#object-type} */ export type NonPrimitive = object /** * @description 判断目标类型是否为原始类型。 */ export type IsPrimitive = T extends string ? true : T extends number ? true : T extends boolean ? true : T extends bigint ? true : T extends symbol ? true : T extends null ? true : T extends undefined ? true : false /** * @description 当目标类型是原始类型时返回 `TRUE`,否则返回 `FALSE`。 */ export type IfPrimitive = IsPrimitive extends true ? TRUE : FALSE /** * @description 判断联合类型的每个成员是否都属于原始类型集合。 */ export type IsPrimitiveUnion = T extends Primitive ? true : false /** * @description 当联合类型成员都属于原始类型集合时返回 `TRUE`,否则返回 `FALSE`。 */ export type IfPrimitiveUnion = IsPrimitiveUnion extends true ? TRUE : FALSE /** * @description Predicate whether the target is `string`. */ export type IsString = T extends string ? true : false /** * @description 当目标类型是 `string` 时返回 `TRUE`,否则返回 `FALSE`。 */ export type IfString = IsString extends true ? TRUE : FALSE /** * @description 将目标类型收窄为 `string`,否则回退到 `string`。 */ export type CastString = T extends string ? T : string /** * @description 判断目标类型是否为字符串字面量类型。 */ export type IsStringLiteral = T extends string ? (string extends T ? false : true) : false /** * @description Predicate whether the target is `number`. */ export type IsNumber = T extends number ? true : false /** * @description 当目标类型是 `number` 时返回 `TRUE`,否则返回 `FALSE`。 */ export type IfNumber = IsNumber extends true ? TRUE : FALSE /** * @description 将目标类型收窄为 `number`,否则回退到 `number`。 */ export type CastNumber = T extends number ? T : number /** * @description 判断目标类型是否为数字字面量类型。 */ export type IsNumberLiteral = T extends number ? (number extends T ? false : true) : false /** * @description Predicate whether the target is `boolean`. */ export type IsBoolean = T extends boolean ? true : false /** * @description 当目标类型是 `boolean` 时返回 `TRUE`,否则返回 `FALSE`。 */ export type IfBoolean = IsBoolean extends true ? TRUE : FALSE /** * @description 将目标类型收窄为 `boolean`,否则回退到 `boolean`。 */ export type CastBoolean = T extends boolean ? T : boolean /** * @description 判断目标类型是否为字面量 `true`。 */ export type IsTrue = T extends true ? true : false /** * @description 判断目标类型是否为字面量 `false`。 */ export type IsFalse = T extends false ? true : false /** * @description 判断目标类型是否为布尔字面量类型。 */ export type IsBooleanLiteral = [T] extends [boolean] // Check if T is assignable to boolean (true, false, or boolean) ? [boolean] extends [T] // Check if boolean is assignable to T (only true for 'boolean' itself) ? false // If both are true, it's the general 'boolean' type : true // Otherwise, it's a specific literal (true or false) : false // Not a boolean type at all /** * @description Predicate whether the target is `bigint`. */ export type IsBigInt = T extends bigint ? true : false /** * @description 当目标类型是 `bigint` 时返回 `TRUE`,否则返回 `FALSE`。 */ export type IfBigInt = IsBigInt extends true ? TRUE : FALSE /** * @description 将目标类型收窄为 `bigint`,否则回退到 `bigint`。 */ export type CastBigInt = T extends bigint ? T : bigint /** * @description Predicate whether the target is `symbol`. */ export type IsSymbol = T extends symbol ? true : false /** * @description 当目标类型是 `symbol` 时返回 `TRUE`,否则返回 `FALSE`。 */ export type IfSymbol = IsSymbol extends true ? TRUE : FALSE /** * @description 将目标类型收窄为 `symbol`,否则回退到 `symbol`。 */ export type CastSymbol = T extends symbol ? T : symbol /** * @description Predicate whether the target is `null`. */ export type IsNull = Exclude extends never ? true : false /** * @description 当目标类型是 `null` 时返回 `TRUE`,否则返回 `FALSE`。 */ export type IfNull = IsNull extends true ? TRUE : FALSE /** * @description 将目标类型收窄为 `null`,否则回退到 `null`。 */ export type CastNull = T extends null ? T : null /** * @description 判断目标类型是否包含 `null`。 */ export type IsNullable = T extends Exclude ? false : true /** * @description 当目标类型可空时返回 `TRUE`,否则返回 `FALSE`。 */ export type IfNullable = IsNullable extends true ? TRUE : FALSE /** * @description 为目标类型附加 `null`。 */ export type Nullable = T | null /** * @description 从目标类型中移除 `null`。 */ export type NonNullable = Exclude /** * @description Predicate whether the target is `undefined`. */ export type IsUndefined = Exclude extends never ? true : false /** * @description 当目标类型是 `undefined` 时返回 `TRUE`,否则返回 `FALSE`。 */ export type IfUndefined = IsUndefined extends true ? TRUE : FALSE /** * @description 将目标类型收窄为 `undefined`,否则回退到 `undefined`。 */ export type CastUndefined = T extends undefined ? T : undefined /** * @description 判断目标类型是否包含 `undefined`。 */ export type IsUndefinedable = T extends Exclude ? false : true /** * @description 当目标类型可为 `undefined` 时返回 `TRUE`,否则返回 `FALSE`。 */ export type IfUndefinedable = IsUndefinedable extends true ? TRUE : FALSE /** * @description 为目标类型附加 `undefined`。 */ export type Undefinedable = T | undefined /** * @description 从目标类型中移除 `undefined`。 */ export type NonUndefinedable = Exclude /** * @description Predicate whether the target is `null | undefined`. */ export type IsNil = Exclude extends never ? true : false /** * @description 当目标类型是 `null | undefined` 时返回 `TRUE`,否则返回 `FALSE`。 */ export type IfNil = IsNil extends true ? TRUE : FALSE /** * @description 将目标类型收窄为 `null | undefined`,否则回退到该并集。 */ export type CastNil = T extends undefined | null ? T : undefined | null /** * @description 判断目标类型是否包含 `null` 或 `undefined`。 */ export type IsNilable = T extends Exclude ? false : true /** * @description 当目标类型可为 `null` 或 `undefined` 时返回 `TRUE`,否则返回 `FALSE`。 */ export type IfNilable = IsNilable extends true ? TRUE : FALSE /** * @description 为目标类型附加 `null | undefined`。 */ export type Nilable = T | null | undefined /** * @description 从目标类型中移除 `null | undefined`。 */ export type NonNilable = Exclude /** * @description Predicate whether the target is `Record`. */ export type IsRecord = T extends Record ? true : false /** * @description 当目标类型是记录类型时返回 `TRUE`,否则返回 `FALSE`。 */ export type IfRecord = IsRecord extends true ? TRUE : FALSE /** * @description 判断目标类型是否为字符串键记录类型。 */ export type IsStringRecord = T extends Record ? true : false /** * @description 当目标类型是字符串键记录类型时返回 `TRUE`,否则返回 `FALSE`。 */ export type IfStringRecord = IsStringRecord extends true ? TRUE : FALSE /** * @description 判断目标类型是否为数字键记录类型。 */ export type IsNumberRecord = T extends Record ? true : false /** * @description 当目标类型是数字键记录类型时返回 `TRUE`,否则返回 `FALSE`。 */ export type IfNumberRecord = IsNumberRecord extends true ? TRUE : FALSE /** * @description 判断目标类型是否为符号键记录类型。 */ export type IsSymbolRecord = T extends Record ? true : false /** * @description 当目标类型是符号键记录类型时返回 `TRUE`,否则返回 `FALSE`。 */ export type IfSymbolRecord = IsSymbolRecord extends true ? TRUE : FALSE /** * @description 判断目标类型是否为对象类型。 */ // oxlint-disable-next-line no-wrapper-object-types export type IsObject = Target extends Object ? true : false /** * @description 当目标类型是对象类型时返回 `TRUE`,否则返回 `FALSE`。 */ export type IfObject = IsObject extends true ? TRUE : FALSE /** * @description 表示普通对象结构。 */ export type PlainObject = Record /** * @description Predicate whether the target is `EmptyObject`(`{}`). */ export type IsEmptyObject> = keyof Target extends never ? true : false /** * @description 当目标类型是空对象时返回 `TRUE`,否则返回 `FALSE`。 */ export type IfEmptyObject, TRUE = true, FALSE = never> = IsEmptyObject extends true ? TRUE : FALSE /** * @description Predicate whether the target is `Array`. */ export type IsArray = T extends readonly unknown[] ? number extends T["length"] ? true : false : false /** * @description 当目标类型是数组时返回 `TRUE`,否则返回 `FALSE`。 */ export type IfArray = IsArray extends true ? TRUE : FALSE // ReadonlyArray is subset of Array. /** * @description 判断目标类型是否为只读数组。 */ export type IsReadonlyArray = T extends readonly unknown[] ? T extends unknown[] ? false : true : false /** * @description 当目标类型是只读数组时返回 `TRUE`,否则返回 `FALSE`。 */ export type IfReadonlyArray = IsReadonlyArray extends true ? TRUE : FALSE /** * @description 判断目标类型是否为可写数组。 */ export type IsWritableArray = T extends unknown[] ? number extends T["length"] ? true : false : false /** * @description 当目标类型是可写数组时返回 `TRUE`,否则返回 `FALSE`。 */ export type IfWritableArray = IsWritableArray extends true ? TRUE : FALSE /** * @description Predicate whether the target is `ArrayLike`. */ export type IsArrayLike = T extends ArrayLike ? true : false /** * @description 当目标类型满足 `ArrayLike` 结构时返回 `TRUE`,否则返回 `FALSE`。 */ export type IfArrayLike = IsArrayLike extends true ? TRUE : FALSE /** * @description Predicate whether the target is `Tuple`. * * @see {@link https://github.com/type-challenges/type-challenges/issues/4491#issuecomment-975808109} */ export type IsTuple = T extends readonly unknown[] ? number extends T["length"] ? false : true : false /** * @description 当目标类型是元组时返回 `TRUE`,否则返回 `FALSE`。 */ export type IfTuple = IsTuple extends true ? TRUE : FALSE /** * @description 判断目标类型是否为只读元组。 */ export type IsReadonlyTuple = T extends readonly unknown[] ? T extends unknown[] ? false : true : false /** * @description 当目标类型是只读元组时返回 `TRUE`,否则返回 `FALSE`。 */ export type IfReadonlyTuple = IsReadonlyTuple extends true ? TRUE : FALSE /** * @description 判断目标类型是否为可写元组。 */ export type IsWritableTuple = T extends unknown[] ? number extends T["length"] ? false : true : false /** * @description 当目标类型是可写元组时返回 `TRUE`,否则返回 `FALSE`。 */ export type IfWritableTuple = IsWritableTuple extends true ? TRUE : FALSE /** * @description Predicate whether the target is `Array` or `Tuple`. */ export type IsArrayOrTuple = T extends readonly unknown[] ? true : false /** * @description 当目标类型是数组或元组时返回 `TRUE`,否则返回 `FALSE`。 */ export type IfArrayOrTuple = IsArrayOrTuple extends true ? TRUE : FALSE /** * @description Predicate whether the target is of type function. */ export type AnyFunction = (...args: any[]) => any /** * @description 根据输入类型构造对应参数列表的任意函数类型。 */ export type AnyFunctionOfTake = A extends readonly [...infer R] ? (...args: R) => any : (arg: A) => any /** * @description 表示返回值固定为指定类型的任意函数。 */ export type AnyFunctionOfReturn = (...args: any[]) => T /** * @description 判断目标类型是否为函数类型。 */ export type IsFunction = T extends AnyFunction ? true : false /** * @description 当目标类型是函数类型时返回 `TRUE`,否则返回 `FALSE`。 */ export type IfFunction = IsFunction extends true ? TRUE : FALSE /** * @description 将目标类型收窄为任意函数类型,否则回退到 `AnyFunction`。 */ export type CastFunction = T extends AnyFunction ? T : AnyFunction /** * @description 表示返回非 `Promise` 值的任意同步函数。 */ export type AnySyncFunction = (...args: any[]) => Exclude> /** * @description Predicate whether the target is of type sync function. */ export type IsSyncFunction = T extends (...args: any[]) => infer R ? R extends Promise ? false : true : false /** * @description 当目标类型是同步函数时返回 `TRUE`,否则返回 `FALSE`。 */ export type IfSyncFunction = IsSyncFunction extends true ? TRUE : FALSE /** * @description 将目标类型收窄为同步函数,否则回退到通用函数类型。 */ export type CastSyncFunction = T extends (...args: unknown[]) => infer R ? R extends Promise ? (...args: Parameters) => Awaited : T : AnyFunction /** * @description 表示返回 `Promise` 的任意异步函数。 */ export type AnyAsyncFunction = (...args: any[]) => Promise /** * @description Predicate whether the target is of type async function. */ export type IsAsyncFunction = T extends AnyAsyncFunction ? true : false /** * @description 当目标类型是异步函数时返回 `TRUE`,否则返回 `FALSE`。 */ export type IfAsyncFunction = IsAsyncFunction extends true ? TRUE : FALSE /** * @description 将目标类型收窄为异步函数,否则回退到 `AnyAsyncFunction`。 */ export type CastAsyncFunction = T extends AnyAsyncFunction ? T : AnyAsyncFunction /** * @description 表示任意生成器函数。 */ export type AnyGeneratorFunction = (...args: any[]) => Generator /** * @description Predicate whether the target is of type generator function. */ export type IsGeneratorFunction = T extends AnyGeneratorFunction ? true : false /** * @description 当目标类型是生成器函数时返回 `TRUE`,否则返回 `FALSE`。 */ export type IfGeneratorFunction = IsGeneratorFunction extends true ? TRUE : FALSE /** * @description 将目标类型收窄为生成器函数,否则回退到 `AnyGeneratorFunction`。 */ export type CastGeneratorFunction = T extends AnyGeneratorFunction ? T : AnyGeneratorFunction /** * @description 表示任意异步生成器函数。 */ export type AnyAsyncGeneratorFunction = (...args: any[]) => AsyncGenerator /** * @description Predicate whether the target is of type async generator function. */ export type IsAsyncGeneratorFunction = T extends AnyAsyncGeneratorFunction ? true : false /** * @description 当目标类型是异步生成器函数时返回 `TRUE`,否则返回 `FALSE`。 */ export type IfAsyncGeneratorFunction = IsAsyncGeneratorFunction extends true ? TRUE : FALSE /** * @description 将目标类型收窄为异步生成器函数,否则回退到 `AnyAsyncGeneratorFunction`。 */ export type CastAsyncGeneratorFunction = T extends AnyAsyncGeneratorFunction ? T : AnyAsyncGeneratorFunction /** * @description Predicate whether the target is `never`. */ export type IsNever = [T] extends [never] ? true : false /** * @description 当目标类型是 `never` 时返回 `TRUE`,否则返回 `FALSE`。 */ export type IfNever = IsNever extends true ? TRUE : FALSE /** * @description 将目标类型收窄为 `never`,否则回退到 `never`。 */ export type CastNever = T extends never ? T : never /** * @description Predicate whether the target is `any`. * * @see {@link https://stackoverflow.com/questions/49927523/disallow-call-with-any/49928360#49928360} */ export type IsAny = 0 extends 1 & T ? true : false /** * @description 当目标类型是 `any` 时返回 `TRUE`,否则返回 `FALSE`。 */ export type IfAny = IsAny extends true ? TRUE : FALSE /** * @description 将目标类型收窄为 `any`。 */ export type CastAny = T extends any ? T : any /** * @description Predicate whether the target is `unknown`. * * @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 /** * @description 当目标类型是 `unknown` 时返回 `TRUE`,否则返回 `FALSE`。 */ export type IfUnknown = IsUnknown extends true ? TRUE : FALSE /** * @description 将目标类型收窄为 `unknown`,否则回退到 `unknown`。 */ export type CastUnknown = T extends unknown ? T : unknown type InternalIsUnion = [T] extends [never] ? false : T extends any ? [U] extends [T] ? false : true : false /** * @description Predicate whether the target is `Union` type. * * 判断目标类型是否为联合类型。 * * @see {@link https://www.typescriptlang.org/docs/handbook/2/conditional-types.html#distributive-conditional-types} */ export type IsUnion = InternalIsUnion /** * @description 当目标类型是联合类型时返回 `TRUE`,否则返回 `FALSE`。 */ export type IfUnion = IsUnion extends true ? TRUE : FALSE /** * @description 表示一个空接口。 */ export interface EmptyInterface {}