//#region src/types/index.d.ts /** * Type utilities * * @example * ```ts * import type { Awaitable } from '@kazupon/jts-utils/types' * ``` * * @module types */ /** * @license MIT * @author kazuya kawaguchi (a.k.a. kazupon) */ /** * Define a promise type that can be await from T * * @typeParam T - Type to be awaited * * @example * ```ts * import type { Awaitable } from '@kazupon/jts-utils' * * // Example with a synchronous value * const syncValue: Awaitable = 42 * // Example with a Promise * const asyncValue: Awaitable = Promise.resolve(42) * ``` */ type Awaitable = T | Promise; /** * Extract module type with interoperability for CJS `module.exports` * * @typeParam T - Module type * * @example * ```ts * import type { InteropModuleDefault } from '@kazupon/jts-utils' * * // Example for `default` export in ESM Module * type ESMModule = { * default: { * foo: string * } * } * type ESMResult = InteropModuleDefault * // Resulting type: * // { * // foo: string * // } * * // Example for CJS Module * type CJSModule = { * foo: string * } * type CJSResult = InteropModuleDefault * // Resulting type: * // { * // foo: string * // } * ``` */ type InteropModuleDefault = T extends { default: infer U; } ? U : T; /** * Convert a union to intersection * * @typeParam U - Union type * * @example * ```ts * import type { UnionToIntersection } from '@kazupon/jts-utils' * * type Union = { a: string } | { b: number } * type Intersection = UnionToIntersection * // Resulting type: * // { * // a: string * // } & { * // b: number * // } * ``` */ type UnionToIntersection = (U extends unknown ? (argument: U) => 0 : never) extends ((argument: infer I) => 0) ? I : never; /** * Extract the last element in a union * * @typeParam U - Union type * * @example * ```ts * import type { LastInUnion } from '@kazupon/jts-utils' * * type MyUnion = { a: string } | { b: number } | { c: boolean } * type LastElement = LastInUnion * // Resulting type: * // { * // c: boolean * // } * ``` */ type LastInUnion = UnionToIntersection 0 : never> extends ((x: infer L) => 0) ? L : never; /** * Convert a union to tuple * * @typeParam U - Union type * * @example * ```ts * import type { UnionToTuple } from '@kazupon/jts-utils' * * type MyUnion = { a: string } | { b: number } | { c: boolean } * type MyTuple = UnionToTuple * // Resulting type: * // [ * // { a: string }, * // { b: number }, * // { c: boolean } * // ] * ``` */ type UnionToTuple> = [U] extends [never] ? [] : [...UnionToTuple>, Last]; /** * Merge two types * * @typeParam F - First type * @typeParam S - Second type * * @example * ```ts * import type { Merge } from '@kazupon/jts-utils' * * type First = { * a: number * b: string * } * type Second = { * b: boolean * c: string * } * type Merged = Merge * // Resulting type: * // { * // a: number * // b: boolean * // c: string * // } * ``` */ type Merge = { [K in keyof F | keyof S]: K extends keyof S ? S[K] : K extends keyof F ? F[K] : never }; /** * Check never type * * @typeParam T - Type to check * * @example * ```ts * import type { IsNever } from '@kazupon/jts-utils' * * type A = IsNever // true * type B = IsNever // false * ``` */ type IsNever = [T] extends [never] ? true : false; /** * whether the type is null * * @typeParam T - Type to check * * @example * ```ts * import type { IsNull } from '@kazupon/jts-utils' * * type A = IsNull // true * type B = IsNull // false * ``` */ type IsNull = T extends null ? true : false; /** * whether the type is object * * @typeParam T - Type to check * * @example * ```ts * import type { IsObject } from '@kazupon/jts-utils' * * type A = IsObject<{ a: number }> // true * type B = IsObject // false * ``` */ type IsObject = T extends object ? (T extends Function ? false : true) : false; /** * whether the type is a plain object * * @typeParam T - Type to check * * @example * ```ts * import type { IsPlainObject } from '@kazupon/jts-utils' * * type A = IsPlainObject<{ a: number }> // true * type B = IsPlainObject // false * type C = IsPlainObject // false * ``` */ type IsPlainObject = T extends object ? T extends Function | Array | null ? false : true : false; /** * Overwrite properties * * @typeParam T - Original type * @typeParam U - Type with properties to overwrite * * @example * ```ts * import type { Overwrite } from '@kazupon/jts-utils' * * type Original = { * a: number * b: string * } * type Overwritten = Overwrite * // Resulting type: * // { * // a: string * // b: boolean * // } * ``` */ type Overwrite = Pick> & Pick>; /** * Prettify a type by flattening its structure. * * @typeParam T - The type to be prettified. * * @example * ```ts * import type { Prettify, Overwrite } from '@kazupon/jts-utils' * * type Original = { * a: number * b: string * } * type Update = { * a: string * b: boolean * } * type Overwritten = Overwrite * type Prettified = Prettify * // Resulting type: * // { * // a: string * // b: boolean * // } * ``` */ type Prettify = { [K in keyof T]: T[K] } & {}; //#endregion export { Awaitable, InteropModuleDefault, IsNever, IsNull, IsObject, IsPlainObject, LastInUnion, Merge, Overwrite, Prettify, UnionToIntersection, UnionToTuple };