A collection of essential TypeScript types
Sindre Sorhus' open source work is supported by the community
Special thanks to:Add Single Sign-On (and more) in minutes instead of months.
Many of the types here should have been built-in. You can help by suggesting some of them to the TypeScript project.
Either add this package as a dependency or copy-paste the needed types. No credit required. 👌
PR welcome for additional commonly needed types and docs improvements. Read the contributing guidelines first.
Help wanted with reviewing proposals and pull requests.
Install
npm install type-fest
Requires TypeScript >=5.1
Works best with {strict: true}
in your tsconfig.
Usage
import type {Except} from 'type-fest';
type Foo = {
unicorn: string;
rainbow: boolean;
};
type FooWithoutRainbow = Except<Foo, 'rainbow'>;
//=> {unicorn: string}
API
Click the type names for complete docs.
Basic
Primitive
- Matches any primitive value.Class
- Matches aclass
.Constructor
- Matches aclass
constructor.AbstractClass
- Matches anabstract class
.AbstractConstructor
- Matches anabstract class
constructor.TypedArray
- Matches any typed array, likeUint8Array
orFloat64Array
.ObservableLike
- Matches a value that is like an Observable.
Utilities
EmptyObject
- Represents a strictly empty plain object, the{}
value.NonEmptyObject
- Represents an object with at least 1 non-optional key.UnknownRecord
- Represents an object withunknown
value. You probably want this instead of{}
.UnknownArray
- Represents an array withunknown
value.Except
- Create a type from an object type without certain keys. This is a stricter version ofOmit
.Writable
- Create a type that stripsreadonly
from the given type. Inverse ofReadonly<T>
.WritableDeep
- Create a deeply mutable version of anobject
/ReadonlyMap
/ReadonlySet
/ReadonlyArray
type. The inverse ofReadonlyDeep<T>
. UseWritable<T>
if you only need one level deep.Merge
- Merge two types into a new type. Keys of the second type overrides keys of the first type.MergeDeep
- Merge two objects or two arrays/tuples recursively into a new type.MergeExclusive
- Create a type that has mutually exclusive keys.OverrideProperties
- Override only existing properties of the given type. Similar toMerge
, but enforces that the original type has the properties you want to override.RequireAtLeastOne
- Create a type that requires at least one of the given keys.RequireExactlyOne
- Create a type that requires exactly a single key of the given keys and disallows more.RequireAllOrNone
- Create a type that requires all of the given keys or none of the given keys.RequireOneOrNone
- Create a type that requires exactly a single key of the given keys and disallows more, or none of the given keys.SingleKeyObject
- Create a type that only accepts an object with a single key.RequiredDeep
- Create a deeply required version of another type. UseRequired<T>
if you only need one level deep.PickDeep
- Pick properties from a deeply-nested object. UsePick<T>
if you only need one level deep.OmitDeep
- Omit properties from a deeply-nested object. UseOmit<T>
if you only need one level deep.OmitIndexSignature
- Omit any index signatures from the given object type, leaving only explicitly defined properties.PickIndexSignature
- Pick only index signatures from the given object type, leaving out all explicitly defined properties.PartialDeep
- Create a deeply optional version of another type. UsePartial<T>
if you only need one level deep.PartialOnUndefinedDeep
- Create a deep version of another type where all keys acceptingundefined
type are set to optional.UndefinedOnPartialDeep
- Create a deep version of another type where all optional keys are set to also acceptundefined
.ReadonlyDeep
- Create a deeply immutable version of anobject
/Map
/Set
/Array
type. UseReadonly<T>
if you only need one level deep.LiteralUnion
- Create a union type by combining primitive types and literal types without sacrificing auto-completion in IDEs for the literal type part of the union. Workaround for Microsoft/TypeScript#29729.Tagged
- Create a tagged type that can support multiple tags and per-tag metadata. (This replaces the previousOpaque
type, which is now deprecated.)UnwrapTagged
- Get the untagged portion of a tagged type created withTagged
. (This replaces the previousUnwrapOpaque
type, which is now deprecated.)InvariantOf
- Create an invariant type, which is a type that does not accept supertypes and subtypes.SetOptional
- Create a type that makes the given keys optional.SetReadonly
- Create a type that makes the given keys readonly.SetRequired
- Create a type that makes the given keys required.SetNonNullable
- Create a type that makes the given keys non-nullable.ValueOf
- Create a union of the given object's values, and optionally specify which keys to get the values from.ConditionalKeys
- Extract keys from a shape where values extend the givenCondition
type.ConditionalPick
- LikePick
except it selects properties from a shape where the values extend the givenCondition
type.ConditionalPickDeep
- LikeConditionalPick
except that it selects the properties deeply.ConditionalExcept
- LikeOmit
except it removes properties from a shape where the values extend the givenCondition
type.UnionToIntersection
- Convert a union type to an intersection type.LiteralToPrimitive
- Convert a literal type to the primitive type it belongs to.LiteralToPrimitiveDeep
- LikeLiteralToPrimitive
except it converts literal types inside an object or array deeply.Stringified
- Create a type with the keys of the given type changed tostring
type.IterableElement
- Get the element type of anIterable
/AsyncIterable
. For example,Array
,Set
,Map
, generator, stream, etc.Entry
- Create a type that represents the type of an entry of a collection.Entries
- Create a type that represents the type of the entries of a collection.SetReturnType
- Create a function type with a return type of your choice and the same parameters as the given function type.SetParameterType
- Create a function that replaces some parameters with the given parameters.Simplify
- Useful to flatten the type output to improve type hints shown in editors. And also to transform an interface into a type to aide with assignability.SimplifyDeep
- Deeply simplifies an object type.Get
- Get a deeply-nested property from an object using a key path, like Lodash's.get()
function.StringKeyOf
- Get keys of the given type as strings.Schema
- Create a deep version of another object type where property values are recursively replaced into a given value type.Exact
- Create a type that does not allow extra properties.OptionalKeysOf
- Extract all optional keys from the given type.KeysOfUnion
- Create a union of all keys from a given type, even those exclusive to specific union members.HasOptionalKeys
- Create atrue
/false
type depending on whether the given type has any optional fields.RequiredKeysOf
- Extract all required keys from the given type.HasRequiredKeys
- Create atrue
/false
type depending on whether the given type has any required fields.ReadonlyKeysOf
- Extract all readonly keys from the given type.HasReadonlyKeys
- Create atrue
/false
type depending on whether the given type has any readonly fields.WritableKeysOf
- Extract all writable (non-readonly) keys from the given type.HasWritableKeys
- Create atrue
/false
type depending on whether the given type has any writable fields.Spread
- Mimic the type inferred by TypeScript when merging two objects or two arrays/tuples using the spread syntax.IsEqual
- Returns a boolean for whether the two given types are equal.TaggedUnion
- Create a union of types that share a common discriminant property.IntRange
- Generate a union of numbers.ArrayIndices
- Provides valid indices for a constant array or tuple.ArrayValues
- Provides all values for a constant array or tuple.ArraySplice
- Creates a new array type by adding or removing elements at a specified index range in the original array.ArrayTail
- Extracts the type of an array or tuple minus the first element.SetFieldType
- Create a type that changes the type of the given keys.Paths
- Generate a union of all possible paths to properties in the given object.SharedUnionFieldsDeep