import type { Undefinedable, Nullable, Nilable, NonUndefinedable, NonNullable, NonNilable } from './base' import type { SetIntersection, SetDifference, SetComplement } from './union' /** * Strict version of `Omit`, `K` must be subset of `typeof T`. */ export type StrictOmit = Omit export type StringRecord = Record export type NumberRecord = Record export type SymbolRecord = Record export type HybridRecord = Record export type AnyStringRecord = StringRecord export type AnyNumberRecord = NumberRecord export type AnySymbolRecord = SymbolRecord export type AnyHybridRecord = Record export type AnyRecordUnion = AnyStringRecord | AnyNumberRecord | AnySymbolRecord // export type ObjectPrettify = Target extends infer O ? { [K in keyof O]: O[K] } : never export type ObjectPrettify = Pick /** * @example * ``` * interface Temp { a: string, b: number, c?: boolean } * // Expect to be: { a: string | undefined, b: number, c?: boolean } * type test1 = UndefinedableByKeys * ``` */ export type UndefinedableByKeys = { [Key in keyof Target]: Key extends Keys ? Undefinedable : Target[Key] } export type UndefinedableAllKeys = UndefinedableByKeys /** * @example * ``` * interface Temp { a: string, b: number, c?: boolean } * // Expect to be: { a: string | null, b: number, c?: boolean } * type test1 = NullableByKeys * ``` */ export type NullableByKeys = { [Key in keyof Target]: Key extends Keys ? Nullable : Target[Key] } export type NullableAllKeys = NullableByKeys /** * @example * ``` * interface Temp { a: string, b: number, c?: boolean } * // Expect to be: { a: string | null | undefined, b: number, c?: boolean } * type test1 = NilableByKeys * ``` */ export type NilableByKeys = { [Key in keyof Target]: Key extends Keys ? Nilable : Target[Key] } export type NilableAllKeys = NilableByKeys /** * @example * ``` * interface Temp { a: string | undefined, b: number, c?: boolean | undefined } * // Expect to be: { a: string, b: number, c?: boolean } * type test1 = NonUndefinedableAllKeys * ``` */ export type NonUndefinedableAllKeys = { [Key in keyof Target]: NonUndefinedable } /** * @example * ``` * interface Temp { a: string | undefined, b: number, c?: boolean } * // Expect to be: { a: string, b: number, c?: boolean } * type test1 = NonUndefinedableByKeys * type test2 = NonUndefinedableByKeys * * interface Temp { a: string | undefined, b: number, c?: boolean | undefined } * // Expect to be: { a: string, b: number, c?: boolean } * type test3 = NonUndefinedableByKeys * // Expect to be: { a: string | undefined, b: number, c?: boolean } * type test4 = NonUndefinedableByKeys * ``` */ export type NonUndefinedableByKeys = ObjectPrettify< NonUndefinedableAllKeys, Keys>>> & Pick, Keys>> & Partial, Keys>>>> & Pick, Keys>> > /** * @example * ``` * interface Temp { a: string | null, b: number, c?: boolean | null } * // Expect to be: { a: string, b: number, c?: boolean } * type test1 = NonNullableAllKeys * ``` */ export type NonNullableAllKeys = { [Key in keyof Target]: NonNullable } /** * @example * ``` * interface Temp { a: string | null, b: number, c?: boolean } * // Expect to be: { a: string, b: number, c?: boolean } * type test1 = NonNullableByKeys * type test2 = NonNullableByKeys * * interface Temp { a: string | null, b: number, c?: boolean | null } * // Expect to be: { a: string, b: number, c?: boolean } * type test3 = NonNullableByKeys * // Expect to be: { a: string | null, b: number, c?: boolean } * type test4 = NonNullableByKeys * ``` */ export type NonNullableByKeys = ObjectPrettify< NonNullableAllKeys, Keys>>> & Pick, Keys>> & Partial, Keys>>>> & Pick, Keys>> > /** * @example * ``` * interface Temp { a: string | null | undefined, b: number, c?: boolean | null } * // Expect to be: { a: string, b: number, c?: boolean } * type test1 = NonNilableAllKeys * ``` */ export type NonNilableAllKeys = { [Key in keyof Target]: NonNilable } /** * @example * ``` * interface Temp { a: string | null | undefined, b: number, c?: boolean } * // Expect to be: { a: string, b: number, c?: boolean } * type test1 = NonNilableByKeys * type test2 = NonNilableByKeys * * interface Temp { a: string | null | undefined, b: number, c?: boolean | null | undefined } * // Expect to be: { a: string, b: number, c?: boolean } * type test3 = NonNilableByKeys * // Expect to be: { a: string | null | undefined, b: number, c?: boolean } * type test4 = NonNilableByKeys * ``` */ export type NonNilableByKeys = ObjectPrettify< NonNilableAllKeys, Keys>>> & Pick, Keys>> & Partial, Keys>>>> & Pick, Keys>> > /** * @example * ``` * interface Temp { a?: string | undefined, b?: number, c?: boolean } * // Expect to be: { readonly a: string | undefined, readonly b: number, readonly c: boolean } * type test1 = ReadonlyAllKeys * ``` */ export type ReadonlyAllKeys = { +readonly [Key in keyof Target]: Target[Key] } /** * @example * ``` * interface Temp { a?: string | undefined, b?: number, c?: boolean } * // Expect to be: { readonly a: string | undefined, readonly b: number, readonly c: boolean } * type test1 = ReadonlyByKeys * ``` */ export type ReadonlyByKeys< Target, Keys> = ObjectPrettify< ReadonlyAllKeys>> & Pick> > /** * @example * ``` * interface Temp { readonly a?: string | undefined, readonly b?: number, readonly c?: boolean } * // Expect to be: { a: string | undefined, b: number, c: boolean } * type test1 = WritableAllKeys * ``` */ export type WritableAllKeys = { -readonly [Key in keyof Target]: Target[Key] } /** * @example * ``` * interface Temp { readonly a?: string | undefined, readonly b?: number, readonly c?: boolean } * // Expect to be: { a: string | undefined, b: number, c: boolean } * type test1 = WritableByKeys * ``` */ export type WritableByKeys< Target, Keys> = ObjectPrettify< WritableAllKeys>> & Pick> > /** * @example * ``` * interface Temp { a?: string | undefined, b?: number, c?: boolean } * // Expect to be: { a: string | undefined, b: number, c: boolean } * type test1 = RequiredAllKeys * ``` */ export type RequiredAllKeys = { [Key in keyof Target]-?: Target[Key] } /** * @example * ``` * interface Temp { a?: string | undefined, b?: number, c?: boolean } * // Expect to be: { a: string | undefined, b: number, c: boolean } * type test1 = RequiredByKeys * ``` */ export type RequiredByKeys< Target, Keys> = ObjectPrettify< RequiredAllKeys>> & Pick> > /** * @example * ``` * interface Temp { a: string | undefined, b: number, c?: boolean | undefined } * // Expect to be: { a?: string | undefined, b?: number, c?: boolean | undefined } * type test1 = OptionalAllKeys * ``` */ export type OptionalAllKeys = { [Key in keyof Target]+?: Target[Key] } /** * @example * ``` * interface Temp { a: string | undefined, b: number, c?: boolean | undefined } * // Expect to be: { a?: string | undefined, b?: number, c?: boolean } * type test1 = OptionalByKeys * ``` */ export type OptionalByKeys = ObjectPrettify< OptionalAllKeys>> & Pick> > /** * Given a type `{ a?: string, b: number }`, return `{ a: string, b?: number }`. * * @see {@link ReverseRequired} * @see Reference - The solution is refer to https://stackoverflow.com/questions/57593022/reverse-required-and-optional-properties */ export type ReverseOptional = ObjectPrettify< Required>> & OptionalAllKeys>> > /** * @see {@link ReverseOptional} */ export type ReverseRequired = ReverseOptional /** * @example * ``` * interface Temp { a: string, b?: number, c?: boolean } * * // Expect: { a: string, b: number, c?: boolean } * type test1 = ReverseOptionalByKeys * ``` */ export type ReverseOptionalByKeys = ObjectPrettify< Omit & ReverseOptional> > /** * @see {@link ReverseOptionalByKeys} */ export type ReverseRequiredByKeys = ReverseOptionalByKeys /** * ObjectValues * @description Get the union type of all the values in target object. * @example * ``` * interface Props { name: string, age: number, visible: boolean } * // Expect: 'name' | 'age' | 'visible' * type Keys = ObjectKeys * ``` */ export type ObjectKeys = Target extends AnyHybridRecord ? keyof Target : never /** * ObjectValues * @description Get the union type of all the values in target object. * @example * ``` * interface Props { name: string, age: number, visible: boolean } * // Expect: string | number | boolean * type PropsValues = ObjectValues * ``` */ export type ObjectValues = Target extends AnyHybridRecord ? Target[keyof Target] : never /** * FunctionKeys * @description Get union type of keys that are functions in object type `Target` * @example * ``` * type MixedProps = {name: string; setName: (name: string) => void; someKeys?: string; someFn?: (...args: any) => any;}; * * // Expect: "setName | someFn" * type Keys = FunctionKeys * ``` * @see Copyright - The code is copy from {@link https://github.com/piotrwitek/utility-types/blob/master/src/mapped-types.ts} */ export type FunctionKeys = { // eslint-disable-next-line @typescript-eslint/ban-types [Key in keyof Target]-?: NonUndefinedable extends Function ? Key : never; }[keyof Target] /** * NonFunctionKeys * @description Get union type of keys that are non-functions in object type `Target` * @example * ``` * type MixedProps = {name: string; setName: (name: string) => void; someKeys?: string; someFn?: (...args: any) => any;}; * * // Expect: "name | someKey" * type Keys = NonFunctionKeys * ``` * @see Copyright - The code is copy from {@link https://github.com/piotrwitek/utility-types/blob/master/src/mapped-types.ts} */ export type NonFunctionKeys = { // eslint-disable-next-line @typescript-eslint/ban-types [Key in keyof Target]-?: NonUndefinedable extends Function ? never : Key; }[keyof Target] /** * WARNING: https://github.com/ts-essentials/ts-essentials/blob/5aa1f264e77fb36bb3f673c49f00927c7c181a7f/lib/types.ts#L440 * @see https://stackoverflow.com/a/52473108/1815209 with comments */ type IfEquals = (() => T extends X ? 1 : 2) extends (() => T extends Y ? 1 : 2) ? A : B /** * ReadonlyKeys * @description Get union type of keys that are readonly in object type `Target` * Credit: Matt McCutchen * https://stackoverflow.com/questions/52443276/how-to-exclude-getter-only-properties-from-type-in-typescript * @example * ``` * type Props = { readonly foo: string; bar: number }; * * // Expect: "foo" * type Keys = ReadonlyKeys * ``` * @see Copyright - The code is copy from {@link https://github.com/piotrwitek/utility-types/blob/master/src/mapped-types.ts} */ export type ReadonlyKeys = { [Key in keyof Target]-?: IfEquals<{ [Q in Key]: Target[Key] }, { -readonly [Q in Key]: Target[Key] }, never, Key> }[keyof Target] /** * WritableKeys * @description Get union type of keys that are mutable in object type `Target` * Credit: Matt McCutchen * https://stackoverflow.com/questions/52443276/how-to-exclude-getter-only-properties-from-type-in-typescript * @example * ``` * interface Props { readonly foo: string, bar: number } * * // Expect: "bar" * type Keys = WritableKeys * ``` * @see Copyright - The code is copy from {@link https://github.com/piotrwitek/utility-types/blob/master/src/mapped-types.ts} */ export type WritableKeys = { [Key in keyof Target]-?: IfEquals<{ [Q in Key]: Target[Key] }, { -readonly [Q in Key]: Target[Key] }, Key, never> }[keyof Target] /** * RequiredKeys * @description Get union type of keys that are required in object type `Target` * @see https://stackoverflow.com/questions/52984808/is-there-a-way-to-get-all-required-properties-of-a-typescript-object * @example * ``` * type Props = { req: number; reqUndef: number | undefined; opt?: string; optUndef?: number | undefined; }; * * // Expect: "req" | "reqUndef" * type Keys = RequiredKeys * ``` * @see Copyright - The code is copy from {@link https://github.com/piotrwitek/utility-types/blob/master/src/mapped-types.ts} */ export type RequiredKeys = { [Key in keyof Target]-?: Record extends Pick ? never : Key; }[keyof Target] /** * OptionalKeys * @description Get union type of keys that are optional in object type `Target` * @see https://stackoverflow.com/questions/52984808/is-there-a-way-to-get-all-required-properties-of-a-typescript-object * @example * ``` * type Props = { req: number; reqUndef: number | undefined; opt?: string; optUndef?: number | undefined; }; * * // Expect: "opt" | "optUndef" * type Keys = OptionalKeys * ``` * @see Copyright - The code is copy from {@link https://github.com/piotrwitek/utility-types/blob/master/src/mapped-types.ts} */ export type OptionalKeys = { [Key in keyof Target]-?: Record extends Pick ? Key : never; }[keyof Target] /** * PickKeysByValue * @example * ``` * interface Temp { req: number, reqUndef: number | undefined, opt?: string } * // Expect: 'req' * type test1 = PickKeysByValue * // Expect: 'req' | 'reqUndef' * type test2 = PickKeysByValue * ``` * @attention (For ALL `ByValue` utilities) When we want to get 'opt' as result in above example, `PickKeysByValue` * will not work. We should use `PickKeysByValue` instead. It's so weird that we don't always * know whether the target key is optional or not in actual most of situations so we have to include `undefined` * in the target matching `ValueType`'s union. It's unnecessary and redundant. * * On the other hand, normal keys and optional keys are quite different. We indeed need an approach to * distinguish them. Seems like add `undefined` to the union of optional keys' value type is a reasonable way. * * Here our final choice is give `undefined` a chance until we get a more fantastic solution. Meanwhile, turn the * `exactOptionalPropertyTypes: true` typescript's compiler option on. It will make the use of `undefined` on optional * properties more reasonable. For more information on it: see {@link https://www.typescriptlang.org/tsconfig#exactOptionalPropertyTypes}, * there's also a use guide on that information, see {@link https://tkdodo.eu/blog/optional-vs-undefined}. */ export type PickKeysByValue = { [Key in keyof Target]-?: Target[Key] extends ValueType ? Key : never }[keyof Target] /** * PickByValue * @description From `Target` pick a set of properties by value matching `ValueType`. * Credit: [Piotr Lewandowski](https://medium.com/dailyjs/typescript-create-a-condition-based-subset-types-9d902cea5b8c) * @example * ``` * interface Temp { req: number, reqUndef: number | undefined, opt?: string } * * // Expect: { req: number } * type test1 = PickByValue * // Expect: { req: number; reqUndef: number | undefined; } * type test2 = PickByValue * ``` * @see */ export type PickByValue = Pick> /** * PickKeysByValue * @example * ``` * interface Temp { req: number, reqUndef: number | undefined, opt?: string } * // Expect: 'req' * type test1 = PickKeysByValue * // Expect: 'reqUndef' * type test2 = PickKeysByValue * ``` */ export type PickKeysByValueExact = { [Key in keyof Target]-?: [ValueType] extends [Target[Key]] ? ([Target[Key]] extends [ValueType] ? Key : never) : never; }[keyof Target] /** * PickByValueExact * @description From `Target` pick a set of properties by value matching exact `ValueType`. * @example * ``` * interface Temp { req: number, reqUndef: number | undefined, opt?: string } * * // Expect: { req: number } * type test1 = PickByValueExact * // Expect: { reqUndef: number | undefined; } * type test2 = PickByValueExact * ``` * @see Copyright - The code is copy from {@link https://github.com/piotrwitek/utility-types/blob/master/src/mapped-types.ts} */ export type PickByValueExact = Pick> /** * OmitByValue * @description From `Target` remove a set of properties by value matching `ValueType`. * Credit: [Piotr Lewandowski](https://medium.com/dailyjs/typescript-create-a-condition-based-subset-types-9d902cea5b8c) * @example * ``` * interface Temp { req: number, reqUndef: number | undefined, opt?: string } * * // Expect: { reqUndef: number | undefined; opt?: string; } * type test1 = OmitByValue * // Expect: { opt?: string; } * type test2 = OmitByValue * ``` * @see */ export type OmitByValue = Omit> /** * OmitByValueExact * @description From `Target` remove a set of properties by value matching exact `ValueType`. * @example * ``` * interface Temp { req: number, reqUndef: number | undefined, opt?: string } * * // Expect: { reqUndef: number | undefined; opt?: string; } * type test1 = OmitByValueExact * // Expect: { req: number; opt?: string } * type test2 = OmitByValueExact * ``` * @see Copyright - The code is copy from {@link https://github.com/piotrwitek/utility-types/blob/master/src/mapped-types.ts} */ export type OmitByValueExact = Omit> /** * ObjectIntersection * @description From `T` pick properties that exist in `U`. * @example * ``` * type Props = { name: string; age: number; visible: boolean }; * type DefaultProps = { age: number }; * * // Expect: { age: number; } * type DuplicateProps = ObjectIntersection * ``` * @see Copyright - The code is copy from {@link https://github.com/piotrwitek/utility-types/blob/master/src/mapped-types.ts} */ export type ObjectIntersection = Pick> /** * ObjectDifference * @description From `T` remove properties that exist in `U`. * @example * ``` * type Props = { name: string; age: number; visible: boolean }; * type DefaultProps = { age: number }; * * // Expect: { name: string; visible: boolean; } * type DiffProps = ObjectDifference * ``` * @see Copyright - The code is copy from {@link https://github.com/piotrwitek/utility-types/blob/master/src/mapped-types.ts} */ export type ObjectDifference = Pick> /** * ObjectComplement * @description From `T` remove properties that exist in `SuperT` (`SuperT` has a subset of the properties of `T`) * @example * ``` * type Props = { name: string; age: number; visible: boolean }; * type DefaultProps = { age: number }; * * // Expect: { name: string; visible: boolean; } * type RestProps = ObjectComplement * ``` * @see Copyright - The code is copy from {@link https://github.com/piotrwitek/utility-types/blob/master/src/mapped-types.ts} */ export type ObjectComplement = Pick> /** * ObjectSymmetricDifference * @example * ``` * interface PropsA { name: string, age: number, visible: boolean } * interface PropsB { age: number, sex: string } * * // Expect: { name: string; visible: boolean; sex: string }; * type ResultProps = ObjectSymmetricDifference * // Expect: 'name' | 'visible' | 'sex' * type ResultProps = keyof ObjectSymmetricDifference * ``` */ export type ObjectSymmetricDifference = ObjectPrettify< ObjectDifference & ObjectDifference > /** * ObjectOverwrite * @description From `U` overwrite properties to `T`. * @example * ``` * interface Props { name: string, age: number, visible: boolean } * interface NewProps { age: string, other: string } * * // Expect: { name: string; age: string; visible: boolean; } * type ReplacedProps = ObjectOverwrite * ``` * @see Copyright - The code is copy from {@link https://github.com/piotrwitek/utility-types/blob/master/src/mapped-types.ts} */ export type ObjectOverwrite = ObjectPrettify< ObjectDifference & ObjectIntersection > /** * ObjectAssign * @description From `U` assign properties to `T` (just like object assign). * @example * ``` * interface Props { name: string, age: number, visible: boolean } * interface NewProps { age: string, other: string } * * // Expect: { name: string; age: string; visible: boolean; other: string; } * type ExtendedProps = ObjectAssign * ``` * @see Copyright - The code is copy from {@link https://github.com/piotrwitek/utility-types/blob/master/src/mapped-types.ts} */ export type ObjectAssign = ObjectPrettify< // ObjectDifference & ObjectIntersection & ObjectDifference Omit & Patch > /** * Alias for `ObjectAssign`, defined for semantic of `ObjectMergeN`. * @see {@link ObjectAssign} */ export type ObjectMerge = ObjectAssign /** * __internal type__, defined for `ObjectMergeN`. * @see {@link ObjectMergeN} */ type _ObjectMergeN = Targets extends [infer Head, ...infer Tail] ? _ObjectMergeN> : Result /** * @example * ``` * interface a { a: string } * interface b { readonly b: string } * interface c { c?: string } * // Expect: { a: string; readonly b: string; c?: string; } * type Result = ObjectMergeN<[a, b, c]> * ``` */ // eslint-disable-next-line @typescript-eslint/ban-types export type ObjectMergeN = ObjectPrettify<_ObjectMergeN> /** * ObjectUnionize * @desc Disjoin object to form union of objects, each with single property. * @example * ``` * interface Props { name: string, age: number, visible: boolean } * * // Expect: { name: string; } | { age: number; } | { visible: boolean; } * type UnionizedType = ObjectUnionize * ``` * @see Copyright - The code is copy from {@link https://github.com/piotrwitek/utility-types/blob/master/src/mapped-types.ts} */ export type ObjectUnionize = { [Key in keyof Target]: { [Q in Key]: Target[Key] }; }[keyof Target] /** * @example * ``` * interface Temp { * readonly grandpa: { * readonly name: string * father?: { * readonly name?: string * son: { * readonly name?: string * } * } * } * } * // Expect: { grandpa?: { name?: string, father?: { name?: string, son?: { name?: string } } } } * type test1 = ObjectDeepWritable * ``` */ export type ObjectDeepWritable = { -readonly [Key in keyof Target]: Target[Key] extends (AnyStringRecord | undefined) ? ObjectDeepWritable> : Target[Key]; } /** * @example * ``` * interface Temp { * grandpa: { * name: string * father?: { * name?: string * son: { * name?: string * } * } * } * } * // Expect: { readonly grandpa?: { * // readonly name?: string, readonly father?: { readonly name?: string, readonly son?: { readonly name?: string } } * // } } * type test1 = ObjectDeepReadonly * ``` */ export type ObjectDeepReadonly = { +readonly [Key in keyof Target]: Target[Key] extends (AnyStringRecord | undefined) ? ObjectDeepReadonly> : Target[Key]; } /** * @example * ``` * interface Temp { * grandpa: { * name: string * father?: { * name?: string * son: { * name?: string * } * } * } * } * // Expect: { grandpa: { name: string, father: { name: string, son: { name: string } } } } * type test1 = ObjectDeepRequired * ``` */ export type ObjectDeepRequired = { [Key in keyof Target]-?: Target[Key] extends (AnyStringRecord | undefined) ? ObjectDeepRequired> : Target[Key]; } /** * @example * ``` * interface Temp { * grandpa: { * name: string * father?: { * name?: string * son: { * name?: string * } * } * } * } * // Expect: { grandpa?: { name?: string, father?: { name?: string, son?: { name?: string } } } } * type test1 = ObjectDeepOptional * ``` */ export type ObjectDeepOptional = { [Key in keyof Target]+?: Target[Key] extends (AnyStringRecord | undefined) ? ObjectDeepOptional> : Target[Key]; } /** * If the `Target` is an empty Record/Object, then return `never`. * @see example - For usecases, see {@link https://github.com/ts-essentials/ts-essentials#nonemptyobject} */ export type ObjectNonEmpty = keyof Target extends never ? never : Target /** * ObjectNamedKeys * @example * ``` * interface Temp { * name: string * age?: number * [key: string]: string | number * } * // Expect: 'name' | 'age' * type test1 = ObjectNamedKeys * ``` * @see Copyright - The code is copy from {@link https://github.com/millsp/ts-toolbelt/blob/319e55123b9571d49f34eca3e5926e41ca73e0f3/sources/Any/KnownKeys.ts#L11} * @see Issue - https://github.com/millsp/ts-toolbelt/issues/164 * @see Reference - https://stackoverflow.com/questions/51465182/how-to-remove-index-signature-using-mapped-types/51956054#51956054 */ export type ObjectNamedKeys = keyof { [Key in keyof Target as ( string extends Key ? never : number extends Key ? never : symbol extends Key ? never : Key )]: Target[Key] } export type ObjectNamedParts = ObjectPrettify>> export type ObjectIndexedParts = ObjectPrettify>> export type ObjectHasNamedKeys = ObjectNamedKeys extends never ? false : true export type ObjectHasIndexedKeys = keyof ObjectIndexedParts extends never ? false : true