/** * Filters out all members of {@link T} that are not {@link P} * * @param T - Items to filter * @param P - Type to filter out * @returns Filtered items * * @example * type Result = Filter<['a', 'b', 'c'], 'b'> * // ^? type Result = ['a', 'c'] */ export type Filter = T extends readonly [infer F, ...infer Rest extends readonly unknown[]] ? [F] extends [P] ? Filter : Filter : readonly [...Acc]; /** * @description Checks if {@link T} can be narrowed further than {@link U} * @param T - Type to check * @param U - Type to against * @example * type Result = IsNarrowable<'foo', string> * // ^? true */ export type IsNarrowable = IsNever<(T extends U ? true : false) & (U extends T ? false : true)> extends true ? false : true; /** * @description Checks if {@link T} is `never` * @param T - Type to check * @example * type Result = IsNever * // ^? type Result = true */ export type IsNever = [T] extends [never] ? true : false; /** * @description Evaluates boolean "or" condition for {@link T} properties. * @param T - Type to check * * * @example * type Result = Or<[false, true, false]> * // ^? type Result = true * * @example * type Result = Or<[false, false, false]> * // ^? type Result = false */ export type Or = T extends readonly [ infer Head, ...infer Tail ] ? Head extends true ? true : Or : false; /** * @description Checks if {@link T} is `undefined` * @param T - Type to check * @example * type Result = IsUndefined * // ^? type Result = true */ export type IsUndefined = [undefined] extends [T] ? true : false; /** * Excludes empty attributes from T if TMaybeExclude is true. * * @example * type Result = MaybeExcludeEmpty<{ a: string, b: number, c: [] }, true> * // ^? type Result = { a: string, b: number } * @example * type Result = MaybeExcludeEmpty<{ a: string, b: number, c: [] }, false> * // ^? type Result = { a: string, b: number, c: [] } * @example * type Result = MaybeExcludeEmpty<{ a: string, b: number, c: undefined }, true> * // ^? type Result = { a: string, b: number } */ export type MaybeExcludeEmpty = TMaybeExclude extends true ? Exclude : T; export type MaybePromise = T | Promise; /** * @description Makes attributes on the type T required if TRequired is true. * * @example * MaybeRequired<{ a: string, b?: number }, true> * => { a: string, b: number } * * MaybeRequired<{ a: string, b?: number }, false> * => { a: string, b?: number } */ export type MaybeRequired = TRequired extends true ? Required : T; /** * @description Makes the attribute on the type T allow undefined if TUndefinedish is true. * * @example * MaybeUndefined * => string | undefined * * MaybeUndefined * => string */ export type MaybeUndefined = TUndefinedish extends true ? T | undefined : T; /** * @private Helper for `Assign`. This is a workaround for tsc generating errorneous type definitions. */ export type Assign_ = { [K in keyof T as K extends keyof U ? U[K] extends void ? never : K : K]: K extends keyof U ? U[K] : T[K]; }; /** * @description Assigns the properties of U onto T. * * @example * Assign<{ a: string, b: number }, { a: undefined, c: boolean }> * => { a: undefined, b: number, c: boolean } */ export type Assign = Assign_ & U; /** * @description Makes nullable properties from T optional. * * @example * OptionalNullable<{ a: string | undefined, c: number }> * => { a?: string | undefined, c: number } */ export type OptionalNullable = { [K in keyof T as T[K] extends NonNullable ? K : never]: T[K]; } & { [K in keyof T as T[K] extends NonNullable ? never : K]?: T[K]; }; /** * @description Make properties K of type T never. * * @example * NeverBy<{ a: string, b: boolean, c: number }, 'a' | 'c'> * => { a: never, b: boolean, c: never } */ export type NeverBy = { [U in keyof T]: U extends K ? never : T[U]; }; /** * @description Constructs a type by excluding `undefined` from `T`. * * @example * NoUndefined * => string */ export type NoUndefined = T extends undefined ? never : T; /** * @description Construct a type with the properties of union type T except for those in type K. * @example * type Result = UnionOmit<{ a: string, b: number } | { a: string, b: undefined, c: number }, 'a'> * => { b: number } | { b: undefined, c: number } */ export type UnionOmit = T extends any ? Omit : never; /** * @description Creates a type that is a partial of T, but with the required keys K. * * @example * PartialBy<{ a: string, b: number }, 'a'> * => { a?: string, b: number } */ export type PartialBy = Omit & Partial>; /** * @description Combines members of an intersection into a readable type. * * @see {@link https://twitter.com/mattpocockuk/status/1622730173446557697?s=20&t=NdpAcmEFXY01xkqU3KO0Mg} * @example * Prettify<{ a: string } & { b: string } & { c: number, d: bigint }> * => { a: string, b: string, c: number, d: bigint } */ export type Prettify = { [K in keyof T]: T[K]; } & {}; type TrimLeft = T extends `${Chars}${infer R}` ? TrimLeft : T; type TrimRight = T extends `${infer R}${Chars}` ? TrimRight : T; /** * @description Creates a type with required keys K from T. * * @example * type Result = RequiredBy<{ a?: string, b?: number, c: number }, 'a' | 'c'> * // ^? { a: string, b?: number, c: number } */ export type RequiredBy = Omit & Required>; /** * @description Trims empty space from type T. * * @example * Trim<' lol '> * => 'lol' */ export type Trim = TrimLeft, Chars>; /** * @description Creates a type that extracts the values of T. * * @example * ValueOf<{ a: string, b: number }> * => string | number */ export type ValueOf = T[keyof T]; export {}; //# sourceMappingURL=utils.d.ts.map