Gathering detailed insights and metrics for utility-types
Gathering detailed insights and metrics for utility-types
Gathering detailed insights and metrics for utility-types
Gathering detailed insights and metrics for utility-types
Collection of utility types, complementing TypeScript built-in mapped types and aliases (think "lodash" for static types).
npm install utility-types
Module System
Min. Node Version
Typescript Support
Node Version
NPM Version
5,544 Stars
250 Commits
230 Forks
38 Watching
1 Branches
30 Contributors
Updated on 27 Nov 2024
Minified
Minified + Gzipped
TypeScript (100%)
Cumulative downloads
Total Downloads
Last day
-1.2%
521,368
Compared to previous day
Last week
5.2%
2,754,484
Compared to previous week
Last month
14.2%
11,305,048
Compared to previous month
Last year
35.4%
115,769,709
Compared to previous year
8
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: Added new utilities :tada:
TypeScript
.dts-jest
1# NPM 2npm install utility-types 3 4# YARN 5yarn 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>
UnionKeys<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>
mixed
getReturnOfExpression()
- from TS v2.0 it's better to use type-level ReturnType
insteadPrimitive
Type 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
isPrimitive
This is a TypeScript Typeguard for the Primitive
type.
This can be useful to control the type of a parameter as the program flows. Example:
1const consumer = (param: Primitive[] | Primitive): string => { 2 if (isPrimitive(param)) { 3 // typeof param === Primitive 4 return String(param) + ' was Primitive'; 5 } 6 // typeof param === Primitive[] 7 const resultArray = param 8 .map(consumer) 9 .map(rootString => '\n\t' + rootString); 10 return resultArray.reduce((comm, newV) => comm + newV, 'this was nested:'); 11};
Falsy
Type representing falsy values in TypeScript: false | "" | 0 | null | undefined
Except
NaN
which cannot be represented as a type literal
isFalsy
1const consumer = (param: Falsy | string): string => { 2 if (isFalsy(param)) { 3 // typeof param === Falsy 4 return String(param) + ' was Falsy'; 5 } 6 // typeof param === string 7 return param.toString(); 8};
Nullish
Type representing nullish values in TypeScript: null | undefined
isNullish
1const consumer = (param: Nullish | string): string => { 2 if (isNullish(param)) { 3 // typeof param === Nullish 4 return String(param) + ' was Nullish'; 5 } 6 // typeof param === string 7 return param.toString(); 8};
SetIntersection<A, B>
(same as Extract)Set intersection of given union types A
and B
Usage:
1import { SetIntersection } from 'utility-types'; 2 3// Expect: "2" | "3" 4type ResultSet = SetIntersection<'1' | '2' | '3', '2' | '3' | '4'>; 5// Expect: () => void 6type ResultSetMixed = SetIntersection<string | number | (() => void), Function>;
SetDifference<A, B>
(same as Exclude)Set difference of given union types A
and B
Usage:
1import { SetDifference } from 'utility-types'; 2 3// Expect: "1" 4type ResultSet = SetDifference<'1' | '2' | '3', '2' | '3' | '4'>; 5// Expect: string | number 6type ResultSetMixed = SetDifference<string | number | (() => void), Function>;
SetComplement<A, A1>
Set complement of given union types A
and (it's subset) A1
Usage:
1import { SetComplement } from 'utility-types'; 2 3// Expect: "1" 4type ResultSet = SetComplement<'1' | '2' | '3', '2' | '3'>;
SymmetricDifference<A, B>
Set difference of union and intersection of given union types A
and B
Usage:
1import { SymmetricDifference } from 'utility-types'; 2 3// Expect: "1" | "4" 4type 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:
1import { FunctionKeys } from 'utility-types'; 2 3type MixedProps = { name: string; setName: (name: string) => void }; 4 5// Expect: "setName" 6type Keys = FunctionKeys<MixedProps>;
NonFunctionKeys<T>
Get union type of keys that are non-functions in object type T
Usage:
1import { NonFunctionKeys } from 'utility-types'; 2 3type MixedProps = { name: string; setName: (name: string) => void }; 4 5// Expect: "name" 6type Keys = NonFunctionKeys<MixedProps>;
MutableKeys<T>
Get union type of keys that are mutable (not readonly) in object type T
Alias: WritableKeys<T>
Usage:
1import { MutableKeys } from 'utility-types'; 2 3type Props = { readonly foo: string; bar: number }; 4 5// Expect: "bar" 6type Keys = MutableKeys<Props>;
ReadonlyKeys<T>
Get union type of keys that are readonly in object type T
Usage:
1import { ReadonlyKeys } from 'utility-types'; 2 3type Props = { readonly foo: string; bar: number }; 4 5// Expect: "foo" 6type Keys = ReadonlyKeys<Props>;
RequiredKeys<T>
Get union type of keys that are required in object type T
Usage:
1import { RequiredKeys } from 'utility-types'; 2 3type Props = { req: number; reqUndef: number | undefined; opt?: string; optUndef?: number | undefined; }; 4 5// Expect: "req" | "reqUndef" 6type Keys = RequiredKeys<Props>;
OptionalKeys<T>
Get union type of keys that are optional in object type T
Usage:
1import { OptionalKeys } from 'utility-types'; 2 3type Props = { req: number; reqUndef: number | undefined; opt?: string; optUndef?: number | undefined; }; 4 5// Expect: "opt" | "optUndef" 6type Keys = OptionalKeys<Props>;
UnionKeys<U>
Get keys of all objects in the union type U
Usage:
1import { UnionKeys } from 'utility-types'; 2 3type Props = { name: string } | { age: number } | { visible: boolean }; 4 5// Expect: "name" | "age" | "visible" 6type Keys = UnionKeys<Props>;
Optional<T, K>
From T
make a set of properties by key K
become optional
Usage:
1import { Optional } from 'utility-types'; 2 3type Props = { name: string; age: number; visible: boolean; }; 4 5// Expect: { name?: string; age?: number; visible?: boolean; } 6type Props = Optional<Props> 7// Expect: { name: string; age?: number; visible?: boolean; } 8type Props = Optional<Props, 'age' | 'visible'>;
Pick<T, K>
(built-in)From T
pick a set of properties by key K
Usage:
1type Props = { name: string; age: number; visible: boolean }; 2 3// Expect: { age: number; } 4type Props = Pick<Props, 'age'>;
PickByValue<T, ValueType>
From T
pick a set of properties by value matching ValueType
.
(Credit: Piotr Lewandowski)
Usage:
1import { PickByValue } from 'utility-types'; 2 3type Props = { req: number; reqUndef: number | undefined; opt?: string; }; 4 5// Expect: { req: number } 6type Props = PickByValue<Props, number>; 7// Expect: { req: number; reqUndef: number | undefined; } 8type Props = PickByValue<Props, number | undefined>;
PickByValueExact<T, ValueType>
From T
pick a set of properties by value matching exact ValueType
.
Usage:
1import { PickByValueExact } from 'utility-types'; 2 3type Props = { req: number; reqUndef: number | undefined; opt?: string; }; 4 5// Expect: { req: number } 6type Props = PickByValueExact<Props, number>; 7// Expect: { reqUndef: number | undefined; } 8type Props = PickByValueExact<Props, number | undefined>;
Omit<T, K>
From T
remove a set of properties by key K
Usage:
1import { Omit } from 'utility-types'; 2 3type Props = { name: string; age: number; visible: boolean }; 4 5// Expect: { name: string; visible: boolean; } 6type Props = Omit<Props, 'age'>;
OmitByValue<T, ValueType>
From T
remove a set of properties by value matching ValueType
.
(Credit: Piotr Lewandowski)
Usage:
1import { OmitByValue } from 'utility-types'; 2 3type Props = { req: number; reqUndef: number | undefined; opt?: string; }; 4 5// Expect: { reqUndef: number | undefined; opt?: string; } 6type Props = OmitByValue<Props, number>; 7// Expect: { opt?: string; } 8type Props = OmitByValue<Props, number | undefined>;
OmitByValueExact<T, ValueType>
From T
remove a set of properties by value matching exact ValueType
.
Usage:
1import { OmitByValueExact } from 'utility-types'; 2 3type Props = { req: number; reqUndef: number | undefined; opt?: string; }; 4 5// Expect: { reqUndef: number | undefined; opt?: string; } 6type Props = OmitByValueExact<Props, number>; 7// Expect: { req: number; opt?: string } 8type Props = OmitByValueExact<Props, number | undefined>;
Intersection<T, U>
From T
pick properties that exist in U
Usage:
1import { Intersection } from 'utility-types'; 2 3type Props = { name: string; age: number; visible: boolean }; 4type DefaultProps = { age: number }; 5 6// Expect: { age: number; } 7type DuplicatedProps = Intersection<Props, DefaultProps>;
Diff<T, U>
From T
remove properties that exist in U
Usage:
1import { Diff } from 'utility-types'; 2 3type Props = { name: string; age: number; visible: boolean }; 4type DefaultProps = { age: number }; 5 6// Expect: { name: string; visible: boolean; } 7type RequiredProps = Diff<Props, DefaultProps>;
Subtract<T, T1>
From T
remove properties that exist in T1
(T1
has a subset of the properties of T
)
Usage:
1import { Subtract } from 'utility-types'; 2 3type Props = { name: string; age: number; visible: boolean }; 4type DefaultProps = { age: number }; 5 6// Expect: { name: string; visible: boolean; } 7type RequiredProps = Subtract<Props, DefaultProps>;
Overwrite<T, U>
From U
overwrite properties to T
Usage:
1import { Overwrite } from 'utility-types'; 2 3type Props = { name: string; age: number; visible: boolean }; 4type NewProps = { age: string; other: string }; 5 6// Expect: { name: string; age: string; visible: boolean; } 7type ReplacedProps = Overwrite<Props, NewProps>;
Assign<T, U>
From U
assign properties to T
(just like object assign)
Usage:
1import { Assign } from 'utility-types'; 2 3type Props = { name: string; age: number; visible: boolean }; 4type NewProps = { age: string; other: string }; 5 6// Expect: { name: string; age: string; visible: boolean; other: string; } 7type ExtendedProps = Assign<Props, NewProps>;
ValuesType<T>
Get the union type of all the values in an object, tuple, array or array-like type T
.
Usage:
1import { ValuesType } from 'utility-types'; 2 3type Props = { name: string; age: number; visible: boolean }; 4// Expect: string | number | boolean 5type PropsValues = ValuesType<Props>; 6 7type NumberArray = number[]; 8// Expect: number 9type NumberItems = ValuesType<NumberArray>; 10 11type ReadonlyNumberTuple = readonly [1, 2]; 12// Expect: 1 | 2 13type AnotherNumberUnion = ValuesType<NumberTuple>; 14 15type BinaryArray = Uint8Array; 16// Expect: number 17type BinaryItems = ValuesType<BinaryArray>;
Partial<T>
Make all properties of object type optional
Required<T, K>
From T
make a set of properties by key K
become required
Usage:
1import { Required } from 'utility-types'; 2 3type Props = { name?: string; age?: number; visible?: boolean; }; 4 5// Expect: { name: string; age: number; visible: boolean; } 6type Props = Required<Props> 7// Expect: { name?: string; age: number; visible: boolean; } 8type Props = Required<Props, 'age' | 'visible'>;
Readonly<T>
Make all properties of object type readonly
Mutable<T>
From T
make all properties become mutable
Alias: Writable<T>
1import { Mutable } from 'utility-types'; 2 3type Props = { 4 readonly name: string; 5 readonly age: number; 6 readonly visible: boolean; 7}; 8 9// Expect: { name: string; age: number; visible: boolean; } 10Mutable<Props>;
ReturnType<T>
Obtain the return type of a function
InstanceType<T>
Obtain the instance type of a class
Unionize<T>
Disjoin object to form union of objects, each with single property
Usage:
1import { Unionize } from 'utility-types'; 2 3type Props = { name: string; age: number; visible: boolean }; 4 5// Expect: { name: string; } | { age: number; } | { visible: boolean; } 6type UnionizedType = Unionize<Props>;
PromiseType<T>
Obtain Promise resolve type
Usage:
1import { PromiseType } from 'utility-types'; 2 3// Expect: string 4type Response = PromiseType<Promise<string>>;
DeepReadonly<T>
Readonly that works for deeply nested structures
Usage:
1import { DeepReadonly } from 'utility-types'; 2 3type NestedProps = { 4 first: { 5 second: { 6 name: string; 7 }; 8 }; 9}; 10 11// Expect: { 12// readonly first: { 13// readonly second: { 14// readonly name: string; 15// }; 16// }; 17// } 18type ReadonlyNestedProps = DeepReadonly<NestedProps>;
DeepRequired<T>
Required that works for deeply nested structures
Usage:
1import { DeepRequired } from 'utility-types'; 2 3type NestedProps = { 4 first?: { 5 second?: { 6 name?: string; 7 }; 8 }; 9}; 10 11// Expect: { 12// first: { 13// second: { 14// name: string; 15// }; 16// }; 17// } 18type RequiredNestedProps = DeepRequired<NestedProps>;
DeepNonNullable<T>
NonNullable that works for deeply nested structure
Usage:
1import { DeepNonNullable } from 'utility-types'; 2 3type NestedProps = { 4 first?: null | { 5 second?: null | { 6 name?: string | null | undefined; 7 }; 8 }; 9}; 10 11// Expect: { 12// first: { 13// second: { 14// name: string; 15// }; 16// }; 17// } 18type RequiredNestedProps = DeepNonNullable<NestedProps>;
DeepPartial<T>
Partial that works for deeply nested structures
Usage:
1import { DeepPartial } from 'utility-types'; 2 3type NestedProps = { 4 first: { 5 second: { 6 name: string; 7 }; 8 }; 9}; 10 11// Expect: { 12// first?: { 13// second?: { 14// name?: string; 15// }; 16// }; 17// } 18type PartialNestedProps = DeepPartial<NestedProps>;
Brand<T, U>
Define nominal type of U
based on type of T
. Similar to Opaque types in Flow.
Usage:
1import { Brand } from 'utility-types'; 2 3type USD = Brand<number, "USD"> 4type EUR = Brand<number, "EUR"> 5 6const tax = 5 as USD; 7const usd = 10 as USD; 8const eur = 10 as EUR; 9 10function gross(net: USD): USD { 11 return (net + tax) as USD; 12} 13 14gross(usd); // ok 15gross(eur); // Type '"EUR"' is not assignable to type '"USD"'.
UnionToIntersection<U>
Get intersection type given union type U
Usage:
1import { UnionToIntersection } from 'utility-types'; 2 3// Expect: { name: string } & { age: number } & { visible: boolean } 4UnionToIntersection<{ name: string } | { age: number } | { visible: boolean }>
$Keys<T>
get the union type of all the keys in an object type T
https://flow.org/en/docs/types/utilities/#toc-keys
Usage:
1import { $Keys } from 'utility-types'; 2 3type Props = { name: string; age: number; visible: boolean }; 4 5// Expect: "name" | "age" | "visible" 6type PropsKeys = $Keys<Props>;
$Values<T>
get the union type of all the values in an object type T
https://flow.org/en/docs/types/utilities/#toc-values
Usage:
1import { $Values } from 'utility-types'; 2 3type Props = { name: string; age: number; visible: boolean }; 4 5// Expect: string | number | boolean 6type PropsValues = $Values<Props>;
$ReadOnly<T>
get the read-only version of a given object type T
https://flow.org/en/docs/types/utilities/#toc-readonly
Usage:
1import { $ReadOnly } from 'utility-types'; 2 3type Props = { name: string; age: number; visible: boolean }; 4 5// Expect: Readonly<{ name: string; age: number; visible: boolean; }> 6type ReadOnlyProps = $ReadOnly<Props>;
$Diff<T, U>
get the set difference of a given object types T
and U
(T \ U
)
https://flow.org/en/docs/types/utilities/#toc-diff
Usage:
1import { $Diff } from 'utility-types'; 2 3type Props = { name: string; age: number; visible: boolean }; 4type DefaultProps = { age: number }; 5 6// Expect: { name: string; visible: boolean; } 7type RequiredProps = $Diff<Props, DefaultProps>;
$PropertyType<T, K>
get the type of property of an object at a given key K
https://flow.org/en/docs/types/utilities/#toc-propertytype
Usage:
1import { $PropertyType } from 'utility-types'; 2 3type Props = { name: string; age: number; visible: boolean }; 4// Expect: string 5type NameType = $PropertyType<Props, 'name'>; 6 7type Tuple = [boolean, number]; 8// Expect: boolean 9type A = $PropertyType<Tuple, '0'>; 10// Expect: number 11type B = $PropertyType<Tuple, '1'>;
$ElementType<T, K>
get the type of elements inside of array, tuple or object of type T
, that matches the given index type K
https://flow.org/en/docs/types/utilities/#toc-elementtype
Usage:
1import { $ElementType } from 'utility-types'; 2 3type Props = { name: string; age: number; visible: boolean }; 4// Expect: string 5type NameType = $ElementType<Props, 'name'>; 6 7type Tuple = [boolean, number]; 8// Expect: boolean 9type A = $ElementType<Tuple, 0>; 10// Expect: number 11type B = $ElementType<Tuple, 1>; 12 13type Arr = boolean[]; 14// Expect: boolean 15type ItemsType = $ElementType<Arr, number>; 16 17type Obj = { [key: string]: number }; 18// Expect: number 19type ValuesType = $ElementType<Obj, string>;
$Call<T>
get the return type of a given expression type
https://flow.org/en/docs/types/utilities/#toc-call
The built-in ReturnType
can be used to accomplish the same goal, although it may have some subtle differences.
Usage:
1import { $Call } from 'utility-types'; 2 3// Common use-case 4const add = (amount: number) => ({ type: 'ADD' as 'ADD', payload: amount }); 5type AddAction = $Call<typeof add>; // { type: 'ADD'; payload: number } 6 7// Examples migrated from Flow docs 8type ExtractPropType<T extends { prop: any }> = (arg: T) => T['prop']; 9type Obj = { prop: number }; 10type PropType = $Call<ExtractPropType<Obj>>; // number 11// type Nope = $Call<ExtractPropType<{ nope: number }>>; // Error: argument doesn't match `Obj`. 12 13type ExtractReturnType<T extends () => any> = (arg: T) => ReturnType<T>; 14type Fn = () => number; 15type FnReturnType = $Call<ExtractReturnType<Fn>>; // number
$Shape<T>
Copies the shape of the type supplied, but marks every field optional.
https://flow.org/en/docs/types/utilities/#toc-shape
Usage:
1import { $Shape } from 'utility-types'; 2 3type Props = { name: string; age: number; visible: boolean }; 4 5// Expect: Partial<Props> 6type PartialProps = $Shape<Props>;
$NonMaybeType<T>
Converts a type T
to a non-maybe type. In other words, the values of $NonMaybeType<T>
are the values of T
except for null
and undefined
.
https://flow.org/en/docs/types/utilities/#toc-nonmaybe
Usage:
1import { $NonMaybeType } from 'utility-types'; 2 3type MaybeName = string | null; 4 5// Expect: string 6type Name = $NonMaybeType<MaybeName>;
Class<T>
Given a type T representing instances of a class C, the type Class
https://flow.org/en/docs/types/utilities/#toc-class
* Differs from original Flow's util - implements only constructor part and won't include any static members. Additionally classes in Typescript are not treated as nominal
Usage:
1import { Class } from 'utility-types'; 2 3 4function makeStore(storeClass: Class<Store>): Store { 5 return new storeClass(); 6}
An arbitrary type that could be anything (same as unknown
)
https://flow.org/en/docs/types/mixed
ts-toolbelt
- Higher type safety for TypeScript$mol_type
- Collection of TypeScript meta types for complex logicCopyright (c) 2016 Piotr Witek mailto:piotrek.witek@gmail.com (http://piotrwitek.github.io)
No vulnerabilities found.
Reason
security policy file detected
Details
Reason
no dangerous workflow patterns detected
Reason
no binaries found in the repo
Reason
license file detected
Details
Reason
Found 9/30 approved changesets -- score normalized to 3
Reason
0 commit(s) and 0 issue activity found in the last 90 days -- score normalized to 0
Reason
detected GitHub workflow tokens with excessive permissions
Details
Reason
dependency not pinned by hash detected -- score normalized to 0
Details
Reason
no effort to earn an OpenSSF best practices badge detected
Reason
project is not fuzzed
Details
Reason
branch protection not enabled on development/release branches
Details
Reason
SAST tool is not run on all commits -- score normalized to 0
Details
Reason
46 existing vulnerabilities detected
Details
Score
Last Scanned on 2024-11-18
The Open Source Security Foundation is a cross-industry collaboration to improve the security of open source software (OSS). The Scorecard provides security health metrics for open source projects.
Learn More