Collection of utility types, complementing TypeScript built-in mapped types and aliases (think "lodash" for static types).
Found it useful? Want more updates?
Show your support by giving a :star:
:tada: Now updated to support TypeScript v3.7 :tada:
TypeScript.dts-jest# NPM
npm install utility-types
# YARN
yarn add utility-types
TypeScript support
* v3.x.x - TypeScript v3.1+
* v2.x.x - TypeScript v2.8.1+
* v1.x.x - TypeScript v2.7.2+
Utility-Types is an open-source project created by people investing their time for the benefit of our community.
Issues like bug fixes or feature requests can be very quickly resolved when funded through the IssueHunt platform.
I highly recommend adding a bounty to the issue that you're waiting for to attract some contributors willing to work on it.
We are open for contributions. If you're planning to contribute please make sure to read the contributing guide as it can save you from wasting your time: CONTRIBUTING.md
SetIntersection<A, B>SetDifference<A, B>SetComplement<A, A1>SymmetricDifference<A, B>Exclude<A, B> (built-in)Extract<A, B> (built-in)NonNullable<T> (built-in)NonUndefined<T>FunctionKeys<T>NonFunctionKeys<T>MutableKeys<T>ReadonlyKeys<T>RequiredKeys<T>OptionalKeys<T>Optional<T, K>Partial<T> (built-in)DeepPartial<T>Required<T, K>DeepRequired<T>Readonly<T> (built-in)DeepReadonly<T>Mutable<T>Pick<T, K> (built-in)Omit<T, K> (built-in)PickByValue<T, ValueType>PickByValueExact<T, ValueType>OmitByValue<T, ValueType>OmitByValueExact<T, ValueType>Intersection<T, U>Diff<T, U>Subtract<T, T1>Overwrite<T, U>Assign<T, U>ValuesType<T>ReturnType<T> (built-in)InstanceType<T> (built-in)PromiseType<T>Unionize<T>Brand<T, U>UnionToIntersection<U>$Keys<T>$Values<T>$ReadOnly<T>$Diff<T, U>$PropertyType<T, K>$ElementType<T, K>$Call<T>$Shape<T>$NonMaybeType<T>Class<T>mixedgetReturnOfExpression() - from TS v2.0 it's better to use type-level ReturnType insteadPrimitiveType representing primitive types in JavaScript, and thus TypeScript: string | number | bigint | boolean | symbol | null | undefined
You can test for singular of these types with typeof
isPrimitiveThis is a TypeScript Typeguard for the Primitive type.
This can be useful to control the type of a parameter as the program flows. Example:
const consumer = (param: Primitive[] | Primitive): string => {
if (isPrimitive(param)) {
// typeof param === Primitive
return String(param) + ' was Primitive';
}
// typeof param === Primitive[]
const resultArray = param
.map(consumer)
.map(rootString => '\n\t' + rootString);
return resultArray.reduce((comm, newV) => comm + newV, 'this was nested:');
};
FalsyType representing falsy values in TypeScript: false | "" | 0 | null | undefined
Except
NaNwhich cannot be represented as a type literal
isFalsyconst consumer = (param: Falsy | string): string => {
if (isFalsy(param)) {
// typeof param === Falsy
return String(param) + ' was Falsy';
}
// typeof param === string
return param.toString();
};
NullishType representing nullish values in TypeScript: null | undefined
isNullishconst consumer = (param: Nullish | string): string => {
if (isNullish(param)) {
// typeof param === Nullish
return String(param) + ' was Nullish';
}
// typeof param === string
return param.toString();
};
SetIntersection<A, B> (same as Extract)Set intersection of given union types A and B
Usage:
import { SetIntersection } from 'utility-types';
// Expect: "2" | "3"
type ResultSet = SetIntersection<'1' | '2' | '3', '2' | '3' | '4'>;
// Expect: () => void
type ResultSetMixed = SetIntersection<string | number | (() => void), Function>;
SetDifference<A, B> (same as Exclude)Set difference of given union types A and B
Usage:
import { SetDifference } from 'utility-types';
// Expect: "1"
type ResultSet = SetDifference<'1' | '2' | '3', '2' | '3' | '4'>;
// Expect: string | number
type ResultSetMixed = SetDifference<string | number | (() => void), Function>;
SetComplement<A, A1>Set complement of given union types A and (it's subset) A1
Usage:
import { SetComplement } from 'utility-types';
// Expect: "1"
type ResultSet = SetComplement<'1' | '2' | '3', '2' | '3'>;
SymmetricDifference<A, B>Set difference of union and intersection of given union types A and B
Usage:
import { SymmetricDifference } from 'utility-types';
// Expect: "1" | "4"
type ResultSet = SymmetricDifference<'1' | '2' | '3', '2' | '3' | '4'>;
NonNullable<A>Exclude null and undefined from set A
NonUndefined<A>Exclude undefined from set A
Exclude<A, B>Exclude subset B from set A
Extract<A, B>Extract subset B from set A
FunctionKeys<T>Get union type of keys that are functions in object type T
Usage:
import { FunctionKeys } from 'utility-types';
type MixedProps = { name: string; setName: (name: string) => void };
// Expect: "setName"
type Keys = FunctionKeys<MixedProps>;
NonFunctionKeys<T>Get union type of keys that are non-functions in object type T
Usage:
import { NonFunctionKeys } from 'utility-types';
type MixedProps = { name: string; setName: (name: string) => void };
// Expect: "name"
type Keys = NonFunctionKeys<MixedProps>;
MutableKeys<T>Get union type of keys that are mutable (not readonly) in object type T
Alias: WritableKeys<T>
Usage:
import { MutableKeys } from 'utility-types';
type Props = { readonly foo: string; bar: number };
// Expect: "bar"
type Keys = MutableKeys<Props>;
ReadonlyKeys<T>Get union type of keys that are readonly in object type T
Usage:
import { ReadonlyKeys } from 'utility-types';
type Props = { readonly foo: string; bar: number };
// Expect: "foo"
type Keys = ReadonlyKeys<Props>;
RequiredKeys<T>Get union type of keys that are required in object type T
Usage:
import { RequiredKeys } from 'utility-types';
type Props = { req: number; reqUndef: number | undefined; opt?: string; optUndef?: number | undefined; };
// Expect: "req" | "reqUndef"
type Keys = RequiredKeys<Props>;
OptionalKeys<T>Get union type of keys that are optional in object type T
Usage:
import { OptionalKeys } from 'utility-types';
type Props = { req: number; reqUndef: number | undefined; opt?: string; optUndef?: number | undefined; };
// Expect: "opt" | "optUndef"
type Keys = OptionalKeys<Props>;
Optional<T, K>From T make a set of properties by key K become optional
Usage:
import { Optional } from 'utility-types';
type Props = { name: string; age: number; visible: boolean; };
// Expect: { name?: string; age?: number; visible?: boolean; }
type Props = Optional<Props>
// Expect: { name: string; age?: number; visible?: boolean; }
type Props = Optional<Props, 'age' | 'visible'>;
Pick<T, K> (built-in)From T pick a set of properties by key K
Usage:
type Props = { name: string; age: number; visible: boolean };
// Expect: { age: number; }
type Props = Pick<Props, 'age'>;
PickByValue<T, ValueType>From T pick a set of properties by value matching ValueType.
(Credit: Piotr Lewandowski)
Usage:
import { PickByValue } from 'utility-types';
type Props = { req: number; reqUndef: number | undefined; opt?: string; };
// Expect: { req: number }
type Props = PickByValue<Props, number>;
// Expect: { req: number; reqUndef: number | undefined; }
type Props = PickByValue<Props, number | undefined>;
PickByValueExact<T, ValueType>From T pick a set of properties by value matching exact ValueType.
Usage:
import { PickByValueExact } from 'utility-types';
type Props = { req: number; reqUndef: number | undefined; opt?: string; };
// Expect: { req: number }
type Props = PickByValueExact<Props, number>;
// Expect: { reqUndef: number | undefined; }
type Props = PickByValueExact<Props, number | undefined>;
Omit<T, K>From T remove a set of properties by key K
Usage:
import { Omit } from 'utility-types';
type Props = { name: string; age: number; visible: boolean };
// Expect: { name: string; visible: boolean; }
type Props = Omit<Props, 'age'>;
OmitByValue<T, ValueType>From T remove a set of properties by value matching ValueType.
_(Credit: [Piotr Lewandowski](https://medium.com/dailyjs/typescript-create-a-condition-based-subset-type
$ claude mcp add utility-types \
-- python -m otcore.mcp_server <graph>