type-fest
- Version 4.32.0
- Published
- 410 kB
- No dependencies
- (MIT OR CC0-1.0) license
Install
npm i type-fest
yarn add type-fest
pnpm add type-fest
Overview
A collection of essential TypeScript types
Index
Interfaces
Type Aliases
- AbstractConstructor
- And
- Arrayable
- ArrayIndices
- ArraySlice
- ArraySplice
- ArrayTail
- ArrayValues
- Asyncify
- AsyncReturnType
- CamelCase
- CamelCasedProperties
- CamelCasedPropertiesDeep
- Class
- ConditionalExcept
- ConditionalKeys
- ConditionalPick
- ConditionalPickDeep
- ConditionalPickDeepOptions
- Constructor
- DelimiterCase
- DelimiterCasedProperties
- DelimiterCasedPropertiesDeep
- DistributedOmit
- DistributedPick
- EmptyObject
- Entries
- Entry
- Exact
- Except
- FindGlobalInstanceType
- FindGlobalType
- Finite
- FixedLengthArray
- Float
- Get
- GetTagMetadata
- GlobalThis
- GreaterThan
- GreaterThanOrEqual
- HasOptionalKeys
- HasReadonlyKeys
- HasRequiredKeys
- HasWritableKeys
- IfAny
- IfEmptyObject
- IfNever
- IfNull
- IfUnknown
- Includes
- IntClosedRange
- Integer
- IntRange
- InvariantOf
- IsAny
- IsBooleanLiteral
- IsEmptyObject
- IsEqual
- IsFloat
- IsInteger
- IsLiteral
- IsNegative
- IsNever
- IsNull
- IsNumericLiteral
- IsStringLiteral
- IsSymbolLiteral
- IsTuple
- IsUnknown
- IterableElement
- Join
- JsonArray
- Jsonifiable
- Jsonify
- JsonObject
- JsonPrimitive
- JsonValue
- KebabCase
- KebabCasedProperties
- KebabCasedPropertiesDeep
- KeysOfUnion
- LastArrayElement
- LessThan
- LessThanOrEqual
- LiteralToPrimitive
- LiteralToPrimitiveDeep
- LiteralUnion
- Merge
- MergeDeep
- MergeDeepOptions
- MergeExclusive
- MultidimensionalArray
- MultidimensionalReadonlyArray
- Negative
- NegativeFloat
- NegativeInfinity
- NegativeInteger
- NonEmptyObject
- NonEmptyTuple
- NonNegative
- NonNegativeInteger
- ObservableLike
- Observer
- OmitDeep
- OmitIndexSignature
- OnComplete
- OnError
- OnNext
- Opaque
- OptionalKeysOf
- Or
- OverrideProperties
- PackageJson
- PartialDeep
- PartialDeepOptions
- PartialOnUndefinedDeep
- PartialOnUndefinedDeepOptions
- PascalCase
- PascalCasedProperties
- PascalCasedPropertiesDeep
- Paths
- PickDeep
- PickIndexSignature
- PositiveInfinity
- Primitive
- Promisable
- ReadonlyDeep
- ReadonlyKeysOf
- ReadonlyTuple
- Replace
- RequireAllOrNone
- RequireAtLeastOne
- RequiredDeep
- RequiredKeysOf
- RequireExactlyOne
- RequireOneOrNone
- Schema
- SchemaOptions
- ScreamingSnakeCase
- SetFieldType
- SetNonNullable
- SetOptional
- SetParameterType
- SetReadonly
- SetRequired
- SetRequiredDeep
- SetReturnType
- SharedUnionFields
- SharedUnionFieldsDeep
- Simplify
- SimplifyDeep
- SingleKeyObject
- SnakeCase
- SnakeCasedProperties
- SnakeCasedPropertiesDeep
- Split
- Spread
- Stringified
- StringKeyOf
- StringRepeat
- StringSlice
- StructuredCloneable
- Subtract
- Sum
- Tagged
- TaggedUnion
- Trim
- TsConfigJson
- TupleToUnion
- TypedArray
- UndefinedOnPartialDeep
- UnionToIntersection
- UnionToTuple
- UnknownArray
- UnknownRecord
- Unsubscribable
- UnwrapOpaque
- UnwrapTagged
- ValueOf
- Words
- Writable
- WritableDeep
- WritableKeysOf
Namespaces
Interfaces
interface AbstractClass
interface AbstractClass<T, Arguments extends unknown[] = any[]> extends AbstractConstructor<T, Arguments> {}
Matches an [
abstract class
](https://www.typescriptlang.org/docs/handbook/classes.html#abstract-classes).Class
property prototype
prototype: Pick<T, keyof T>;
Type Aliases
type AbstractConstructor
type AbstractConstructor<T, Arguments extends unknown[] = any[]> = abstract new ( ...arguments_: Arguments) => T;
Matches an [
abstract class
](https://www.typescriptlang.org/docs/handbook/release-notes/typescript-4-2.html#abstract-construct-signatures) constructor.Class
type And
type And<A extends boolean, B extends boolean> = [A, B][number] extends true ? true : true extends [IsEqual<A, false>, IsEqual<B, false>][number] ? false : never;
Returns a boolean for whether two given types are both true.
Use-case: Constructing complex conditional types where multiple conditions must be satisfied.
Example 1
import type {And} from 'type-fest';And<true, true>;//=> trueAnd<true, false>;//=> falseSee Also
type Arrayable
type Arrayable<T> = | T // TODO: Use `readonly T[]` when this issue is resolved: https://github.com/microsoft/TypeScript/issues/17002 | T[];
Create a type that represents either the value or an array of the value.
Example 1
import type {Arrayable} from 'type-fest';function bundle(input: string, output: Arrayable<string>) {const outputList = Array.isArray(output) ? output : [output];// …for (const output of outputList) {console.log(`write to: ${output}`);}}bundle('src/index.js', 'dist/index.js');bundle('src/index.js', ['dist/index.cjs', 'dist/index.mjs']);Array
See Also
Promisable
type ArrayIndices
type ArrayIndices<Element extends readonly unknown[]> = Exclude< Partial<Element>['length'], Element['length']>;
Provides valid indices for a constant array or tuple.
Use-case: This type is useful when working with constant arrays or tuples and you want to enforce type-safety for accessing elements by their indices.
Example 1
import type {ArrayIndices, ArrayValues} from 'type-fest';const weekdays = ['Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday', 'Sunday'] as const;type Weekday = ArrayIndices<typeof weekdays>;type WeekdayName = ArrayValues<typeof weekdays>;const getWeekdayName = (day: Weekday): WeekdayName => weekdays[day];See Also
Array
type ArraySlice
type ArraySlice< Array_ extends readonly unknown[], Start extends number = never, End extends number = never> = Array_ extends unknown // To distributive type ? And<IsEqual<Start, never>, IsEqual<End, never>> extends true ? Array_ : number extends Array_['length'] ? VariableLengthArraySliceHelper<Array_, Start, End> : ArraySliceHelper< Array_, IsEqual<Start, never> extends true ? 0 : Start, IsEqual<End, never> extends true ? Array_['length'] : End > : never;
Returns an array slice of a given range, just like
Array#slice()
.Example 1
import type {ArraySlice} from 'type-fest';type T0 = ArraySlice<[0, 1, 2, 3, 4]>;//=> [0, 1, 2, 3, 4]type T1 = ArraySlice<[0, 1, 2, 3, 4], 0, -1>;//=> [0, 1, 2, 3]type T2 = ArraySlice<[0, 1, 2, 3, 4], 1, -2>;//=> [1, 2]type T3 = ArraySlice<[0, 1, 2, 3, 4], -2, 4>;//=> [3]type T4 = ArraySlice<[0, 1, 2, 3, 4], -2, -1>;//=> [3]type T5 = ArraySlice<[0, 1, 2, 3, 4], 0, -999>;//=> []function arraySlice<const Array_ extends readonly unknown[],Start extends number = 0,End extends number = Array_['length'],>(array: Array_, start?: Start, end?: End) {return array.slice(start, end) as ArraySlice<Array_, Start, End>;}const slice = arraySlice([1, '2', {a: 3}, [4, 5]], 0, -1);typeof slice;//=> [1, '2', { readonly a: 3; }]slice[2].a;//=> 3// @ts-expect-error -- TS2493: Tuple type '[1, "2", {readonly a: 3}]' of length '3' has no element at index '3'.slice[3];Array
type ArraySplice
type ArraySplice< T extends UnknownArray, Start extends number, DeleteCount extends number, Items extends UnknownArray = []> = SplitArrayByIndex<T, Start> extends [ infer U extends UnknownArray, infer V extends UnknownArray] ? SplitArrayByIndex<V, DeleteCount> extends [ infer _Deleted extends UnknownArray, infer X extends UnknownArray ] ? [...U, ...Items, ...X] : never // Should never happen : never;
Creates a new array type by adding or removing elements at a specified index range in the original array.
Use-case: Replace or insert items in an array type.
Like [
Array#splice()
](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/splice) but for types.Example 1
type SomeMonths0 = ['January', 'April', 'June'];type Mouths0 = ArraySplice<SomeMonths0, 1, 0, ['Feb', 'March']>;//=> type Mouths0 = ['January', 'Feb', 'March', 'April', 'June'];type SomeMonths1 = ['January', 'April', 'June'];type Mouths1 = ArraySplice<SomeMonths1, 1, 1>;//=> type Mouths1 = ['January', 'June'];type SomeMonths2 = ['January', 'Foo', 'April'];type Mouths2 = ArraySplice<SomeMonths2, 1, 1, ['Feb', 'March']>;//=> type Mouths2 = ['January', 'Feb', 'March', 'April'];Array
type ArrayTail
type ArrayTail<TArray extends UnknownArrayOrTuple> = TArray extends readonly [ unknown?, ...infer Tail] ? keyof TArray & `${number}` extends never ? [] : Tail : [];
Extracts the type of an array or tuple minus the first element.
Example 1
import type {ArrayTail} from 'type-fest';declare const curry: <Arguments extends unknown[], Return>(function_: (...arguments_: Arguments) => Return,...arguments_: ArrayTail<Arguments>) => (...arguments_: ArrayTail<Arguments>) => Return;const add = (a: number, b: number) => a + b;const add3 = curry(add, 3);add3(4);//=> 7Array
type ArrayValues
type ArrayValues<T extends readonly unknown[]> = T[number];
Provides all values for a constant array or tuple.
Use-case: This type is useful when working with constant arrays or tuples and you want to enforce type-safety with their values.
Example 1
import type {ArrayValues, ArrayIndices} from 'type-fest';const weekdays = ['Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday', 'Sunday'] as const;type WeekdayName = ArrayValues<typeof weekdays>;type Weekday = ArrayIndices<typeof weekdays>;const getWeekdayName = (day: Weekday): WeekdayName => weekdays[day];See Also
Array
type Asyncify
type Asyncify<Function_ extends (...arguments_: any[]) => any> = SetReturnType< Function_, Promise<Awaited<ReturnType<Function_>>>>;
Create an async version of the given function type, by boxing the return type in
Promise
while keeping the same parameter types.Use-case: You have two functions, one synchronous and one asynchronous that do the same thing. Instead of having to duplicate the type definition, you can use
Asyncify
to reuse the synchronous type.Example 1
import type {Asyncify} from 'type-fest';// Synchronous function.function getFooSync(someArg: SomeType): Foo {// …}type AsyncifiedFooGetter = Asyncify<typeof getFooSync>;//=> type AsyncifiedFooGetter = (someArg: SomeType) => Promise<Foo>;// Same as `getFooSync` but asynchronous.const getFooAsync: AsyncifiedFooGetter = (someArg) => {// TypeScript now knows that `someArg` is `SomeType` automatically.// It also knows that this function must return `Promise<Foo>`.// If you have `@typescript-eslint/promise-function-async` linter rule enabled, it will even report that "Functions that return promises must be async.".// …}Async
type AsyncReturnType
type AsyncReturnType<Target extends AsyncFunction> = Awaited<ReturnType<Target>>;
Unwrap the return type of a function that returns a
Promise
.There has been [discussion](https://github.com/microsoft/TypeScript/pull/35998) about implementing this type in TypeScript.
Example 1
import type {AsyncReturnType} from 'type-fest';import {asyncFunction} from 'api';// This type resolves to the unwrapped return type of `asyncFunction`.type Value = AsyncReturnType<typeof asyncFunction>;async function doSomething(value: Value) {}asyncFunction().then(value => doSomething(value));Async
type CamelCase
type CamelCase< Type, Options extends CamelCaseOptions = { preserveConsecutiveUppercase: true }> = Type extends string ? string extends Type ? Type : Uncapitalize< CamelCaseFromArray< Words<Type extends Uppercase<Type> ? Lowercase<Type> : Type>, Options > > : Type;
Convert a string literal to camel-case.
This can be useful when, for example, converting some kebab-cased command-line flags or a snake-cased database result.
By default, consecutive uppercase letter are preserved. See option to change this behaviour.
Example 1
import type {CamelCase} from 'type-fest';// Simpleconst someVariable: CamelCase<'foo-bar'> = 'fooBar';// Advancedtype CamelCasedProperties<T> = {[K in keyof T as CamelCase<K>]: T[K]};interface RawOptions {'dry-run': boolean;'full_family_name': string;foo: number;BAR: string;QUZ_QUX: number;'OTHER-FIELD': boolean;}const dbResult: CamelCasedProperties<RawOptions> = {dryRun: true,fullFamilyName: 'bar.js',foo: 123,bar: 'foo',quzQux: 6,otherField: false};Change case Template literal
type CamelCasedProperties
type CamelCasedProperties< Value, Options extends CamelCaseOptions = { preserveConsecutiveUppercase: true }> = Value extends Function ? Value : Value extends Array<infer U> ? Value : { [K in keyof Value as CamelCase<K, Options>]: Value[K]; };
Convert object properties to camel case but not recursively.
This can be useful when, for example, converting some API types from a different style.
Example 1
import type {CamelCasedProperties} from 'type-fest';interface User {UserId: number;UserName: string;}const result: CamelCasedProperties<User> = {userId: 1,userName: 'Tom',};Change case Template literal Object
See Also
CamelCasedPropertiesDeep
CamelCase
type CamelCasedPropertiesDeep
type CamelCasedPropertiesDeep< Value, Options extends CamelCaseOptions = { preserveConsecutiveUppercase: true }> = Value extends NonRecursiveType ? Value : Value extends UnknownArray ? CamelCasedPropertiesArrayDeep<Value> : Value extends Set<infer U> ? Set<CamelCasedPropertiesDeep<U, Options>> : { [K in keyof Value as CamelCase<K, Options>]: CamelCasedPropertiesDeep< Value[K], Options >; };
Convert object properties to camel case recursively.
This can be useful when, for example, converting some API types from a different style.
Example 1
import type {CamelCasedPropertiesDeep} from 'type-fest';interface User {UserId: number;UserName: string;}interface UserWithFriends {UserInfo: User;UserFriends: User[];}const result: CamelCasedPropertiesDeep<UserWithFriends> = {userInfo: {userId: 1,userName: 'Tom',},userFriends: [{userId: 2,userName: 'Jerry',},{userId: 3,userName: 'Spike',},],};Change case Template literal Object
See Also
CamelCasedProperties
CamelCase
type Class
type Class<T, Arguments extends unknown[] = any[]> = { prototype: Pick<T, keyof T>; new (...arguments_: Arguments): T;};
Matches a [
class
](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Classes).Class
type ConditionalExcept
type ConditionalExcept<Base, Condition> = Except< Base, ConditionalKeys<Base, Condition>>;
Exclude keys from a shape that matches the given
Condition
.This is useful when you want to create a new type with a specific set of keys from a shape. For example, you might want to exclude all the primitive properties from a class and form a new shape containing everything but the primitive properties.
Example 1
import type {Primitive, ConditionalExcept} from 'type-fest';class Awesome {name: string;successes: number;failures: bigint;run() {}}type ExceptPrimitivesFromAwesome = ConditionalExcept<Awesome, Primitive>;//=> {run: () => void}Example 2
import type {ConditionalExcept} from 'type-fest';interface Example {a: string;b: string | number;c: () => void;d: {};}type NonStringKeysOnly = ConditionalExcept<Example, string>;//=> {b: string | number; c: () => void; d: {}}Object
type ConditionalKeys
type ConditionalKeys<Base, Condition> = { // Map through all the keys of the given base type. [Key in keyof Base]-?: // Pick only keys with types extending the given `Condition` type. Base[Key] extends Condition ? // Retain this key // If the value for the key extends never, only include it if `Condition` also extends never IfNever<Base[Key], IfNever<Condition, Key, never>, Key> : // Discard this key since the condition fails. never; // Convert the produced object into a union type of the keys which passed the conditional test.}[keyof Base];
Extract the keys from a type where the value type of the key extends the given
Condition
.Internally this is used for the
ConditionalPick
andConditionalExcept
types.Example 1
import type {ConditionalKeys} from 'type-fest';interface Example {a: string;b: string | number;c?: string;d: {};}type StringKeysOnly = ConditionalKeys<Example, string>;//=> 'a'To support partial types, make sure your
Condition
is a union of undefined (for example,string | undefined
) as demonstrated below.Example 2
import type {ConditionalKeys} from 'type-fest';type StringKeysAndUndefined = ConditionalKeys<Example, string | undefined>;//=> 'a' | 'c'Object
type ConditionalPick
type ConditionalPick<Base, Condition> = Pick<Base, ConditionalKeys<Base, Condition>>;
Pick keys from the shape that matches the given
Condition
.This is useful when you want to create a new type from a specific subset of an existing type. For example, you might want to pick all the primitive properties from a class and form a new automatically derived type.
Example 1
import type {Primitive, ConditionalPick} from 'type-fest';class Awesome {name: string;successes: number;failures: bigint;run() {}}type PickPrimitivesFromAwesome = ConditionalPick<Awesome, Primitive>;//=> {name: string; successes: number; failures: bigint}Example 2
import type {ConditionalPick} from 'type-fest';interface Example {a: string;b: string | number;c: () => void;d: {};}type StringKeysOnly = ConditionalPick<Example, string>;//=> {a: string}Object
type ConditionalPickDeep
type ConditionalPickDeep< Type, Condition, Options extends ConditionalPickDeepOptions = {}> = ConditionalSimplifyDeep< ConditionalExcept< { [Key in keyof Type]: AssertCondition< Type[Key], Condition, Options > extends true ? Type[Key] : IsPlainObject<Type[Key]> extends true ? ConditionalPickDeep<Type[Key], Condition, Options> : typeof conditionalPickDeepSymbol; }, (typeof conditionalPickDeepSymbol | undefined) | EmptyObject >, never, UnknownRecord>;
Pick keys recursively from the shape that matches the given condition.
Example 1
import type {ConditionalPickDeep} from 'type-fest';interface Example {a: string;b: string | boolean;c: {d: string;e: {f?: string;g?: boolean;h: string | boolean;i: boolean | bigint;};j: boolean;};}type StringPick = ConditionalPickDeep<Example, string>;//=> {a: string; c: {d: string}}type StringPickOptional = ConditionalPickDeep<Example, string | undefined>;//=> {a: string; c: {d: string; e: {f?: string}}}type StringPickOptionalOnly = ConditionalPickDeep<Example, string | undefined, {condition: 'equality'}>;//=> {c: {e: {f?: string}}}type BooleanPick = ConditionalPickDeep<Example, boolean | undefined>;//=> {c: {e: {g?: boolean}; j: boolean}}type NumberPick = ConditionalPickDeep<Example, number>;//=> {}type StringOrBooleanPick = ConditionalPickDeep<Example, string | boolean>;//=> {// a: string;// b: string | boolean;// c: {// d: string;// e: {// h: string | boolean// };// j: boolean;// };// }type StringOrBooleanPickOnly = ConditionalPickDeep<Example, string | boolean, {condition: 'equality'}>;//=> {b: string | boolean; c: {e: {h: string | boolean}}}Object
See Also
ConditionalPick
type ConditionalPickDeepOptions
type ConditionalPickDeepOptions = { /** The condition assertion mode.
@default 'extends' */ condition?: 'extends' | 'equality';};
ConditionalPickDeep options.
See Also
ConditionalPickDeep
type Constructor
type Constructor<T, Arguments extends unknown[] = any[]> = new ( ...arguments_: Arguments) => T;
Matches a [
class
constructor](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Classes).Class
type DelimiterCase
type DelimiterCase<Value, Delimiter extends string> = string extends Value ? Value : Value extends string ? StringArrayToDelimiterCase< SplitIncludingDelimiters<Value, WordSeparators | UpperCaseCharacters>, true, WordSeparators, UpperCaseCharacters, Delimiter > : Value;
Convert a string literal to a custom string delimiter casing.
This can be useful when, for example, converting a camel-cased object property to an oddly cased one.
Example 1
import type {DelimiterCase} from 'type-fest';// Simpleconst someVariable: DelimiterCase<'fooBar', '#'> = 'foo#bar';// Advancedtype OddlyCasedProperties<T> = {[K in keyof T as DelimiterCase<K, '#'>]: T[K]};interface SomeOptions {dryRun: boolean;includeFile: string;foo: number;}const rawCliOptions: OddlyCasedProperties<SomeOptions> = {'dry#run': true,'include#file': 'bar.js',foo: 123};Change case Template literal
See Also
KebabCase
SnakeCase
type DelimiterCasedProperties
type DelimiterCasedProperties< Value, Delimiter extends string> = Value extends Function ? Value : Value extends Array<infer U> ? Value : { [K in keyof Value as DelimiterCase<K, Delimiter>]: Value[K] };
Convert object properties to delimiter case but not recursively.
This can be useful when, for example, converting some API types from a different style.
Example 1
import type {DelimiterCasedProperties} from 'type-fest';interface User {userId: number;userName: string;}const result: DelimiterCasedProperties<User, '-'> = {'user-id': 1,'user-name': 'Tom',};Change case Template literal Object
See Also
DelimiterCase
DelimiterCasedPropertiesDeep
type DelimiterCasedPropertiesDeep
type DelimiterCasedPropertiesDeep< Value, Delimiter extends string> = Value extends NonRecursiveType ? Value : Value extends UnknownArray ? DelimiterCasedPropertiesArrayDeep<Value, Delimiter> : Value extends Set<infer U> ? Set<DelimiterCasedPropertiesDeep<U, Delimiter>> : { [K in keyof Value as DelimiterCase< K, Delimiter >]: DelimiterCasedPropertiesDeep<Value[K], Delimiter>; };
Convert object properties to delimiter case recursively.
This can be useful when, for example, converting some API types from a different style.
Example 1
import type {DelimiterCasedPropertiesDeep} from 'type-fest';interface User {userId: number;userName: string;}interface UserWithFriends {userInfo: User;userFriends: User[];}const result: DelimiterCasedPropertiesDeep<UserWithFriends, '-'> = {'user-info': {'user-id': 1,'user-name': 'Tom',},'user-friends': [{'user-id': 2,'user-name': 'Jerry',},{'user-id': 3,'user-name': 'Spike',},],};Change case Template literal Object
See Also
DelimiterCase
DelimiterCasedProperties
type DistributedOmit
type DistributedOmit< ObjectType, KeyType extends KeysOfUnion<ObjectType>> = ObjectType extends unknown ? Omit<ObjectType, KeyType> : never;
Omits keys from a type, distributing the operation over a union.
TypeScript's
Omit
doesn't distribute over unions, leading to the erasure of unique properties from union members when omitting keys. This creates a type that only retains properties common to all union members, making it impossible to access member-specific properties after the Omit. Essentially, usingOmit
on a union type merges the types into a less specific one, hindering type narrowing and property access based on discriminants. This type solves that.Example:
type A = {discriminant: 'A';foo: string;a: number;};type B = {discriminant: 'B';foo: string;b: string;};type Union = A | B;type OmittedUnion = Omit<Union, 'foo'>;//=> {discriminant: 'A' | 'B'}const omittedUnion: OmittedUnion = createOmittedUnion();if (omittedUnion.discriminant === 'A') {// We would like to narrow `omittedUnion`'s type// to `A` here, but we can't because `Omit`// doesn't distribute over unions.omittedUnion.a;//=> Error: `a` is not a property of `{discriminant: 'A' | 'B'}`}While
Except
solves this problem, it restricts the keys you can omit to the ones that are present in **ALL** union members, whereDistributedOmit
allows you to omit keys that are present in **ANY** union member.Example 1
type A = {discriminant: 'A';foo: string;a: number;};type B = {discriminant: 'B';foo: string;bar: string;b: string;};type C = {discriminant: 'C';bar: string;c: boolean;};// Notice that `foo` exists in `A` and `B`, but not in `C`, and// `bar` exists in `B` and `C`, but not in `A`.type Union = A | B | C;type OmittedUnion = DistributedOmit<Union, 'foo' | 'bar'>;const omittedUnion: OmittedUnion = createOmittedUnion();if (omittedUnion.discriminant === 'A') {omittedUnion.a;//=> OKomittedUnion.foo;//=> Error: `foo` is not a property of `{discriminant: 'A'; a: string}`omittedUnion.bar;//=> Error: `bar` is not a property of `{discriminant: 'A'; a: string}`}Object
type DistributedPick
type DistributedPick< ObjectType, KeyType extends KeysOfUnion<ObjectType>> = ObjectType extends unknown ? Pick<ObjectType, Extract<KeyType, keyof ObjectType>> : never;
Pick keys from a type, distributing the operation over a union.
TypeScript's
Pick
doesn't distribute over unions, leading to the erasure of unique properties from union members when picking keys. This creates a type that only retains properties common to all union members, making it impossible to access member-specific properties after the Pick. Essentially, usingPick
on a union type merges the types into a less specific one, hindering type narrowing and property access based on discriminants. This type solves that.Example:
type A = {discriminant: 'A';foo: {bar: string;};};type B = {discriminant: 'B';foo: {baz: string;};};type Union = A | B;type PickedUnion = Pick<Union, 'discriminant' | 'foo'>;//=> {discriminant: 'A' | 'B', foo: {bar: string} | {baz: string}}const pickedUnion: PickedUnion = createPickedUnion();if (pickedUnion.discriminant === 'A') {// We would like to narrow `pickedUnion`'s type// to `A` here, but we can't because `Pick`// doesn't distribute over unions.pickedUnion.foo.bar;//=> Error: Property 'bar' does not exist on type '{bar: string} | {baz: string}'.}Example 1
type A = {discriminant: 'A';foo: {bar: string;};extraneous: boolean;};type B = {discriminant: 'B';foo: {baz: string;};extraneous: boolean;};// Notice that `foo.bar` exists in `A` but not in `B`.type Union = A | B;type PickedUnion = DistributedPick<Union, 'discriminant' | 'foo'>;const pickedUnion: PickedUnion = createPickedUnion();if (pickedUnion.discriminant === 'A') {pickedUnion.foo.bar;//=> OKpickedUnion.extraneous;//=> Error: Property `extraneous` does not exist on type `Pick<A, 'discriminant' | 'foo'>`.pickedUnion.foo.baz;//=> Error: `bar` is not a property of `{discriminant: 'A'; a: string}`.}Object
type EmptyObject
type EmptyObject = { [emptyObjectSymbol]?: never };
Represents a strictly empty plain object, the
{}
value.When you annotate something as the type
{}
, it can be anything exceptnull
andundefined
. This means that you cannot use{}
to represent an empty plain object ([read more](https://stackoverflow.com/questions/47339869/typescript-empty-object-and-any-difference/52193484#52193484)).Example 1
import type {EmptyObject} from 'type-fest';// The following illustrates the problem with `{}`.const foo1: {} = {}; // Passconst foo2: {} = []; // Passconst foo3: {} = 42; // Passconst foo4: {} = {a: 1}; // Pass// With `EmptyObject` only the first case is valid.const bar1: EmptyObject = {}; // Passconst bar2: EmptyObject = 42; // Failconst bar3: EmptyObject = []; // Failconst bar4: EmptyObject = {a: 1}; // FailUnfortunately,
Record<string, never>
,Record<keyof any, never>
andRecord<never, never>
do not work. See .Object
type Entries
type Entries<BaseType> = BaseType extends Map<unknown, unknown> ? MapEntries<BaseType> : BaseType extends Set<unknown> ? SetEntries<BaseType> : BaseType extends readonly unknown[] ? ArrayEntries<BaseType> : BaseType extends object ? ObjectEntries<BaseType> : never;
Many collections have an
entries
method which returns an array of a given object's own enumerable string-keyed property [key, value] pairs. TheEntries
type will return the type of that collection's entries.For example the `Object`, `Map`, `Array`, and `Set` collections all have this method. Note that
WeakMap
andWeakSet
do not have this method since their entries are not enumerable.Example 1
import type {Entries} from 'type-fest';interface Example {someKey: number;}const manipulatesEntries = (examples: Entries<Example>) => examples.map(example => [// Does some arbitrary processing on the key (with type information available)example[0].toUpperCase(),// Does some arbitrary processing on the value (with type information available)example[1].toFixed()]);const example: Example = {someKey: 1};const entries = Object.entries(example) as Entries<Example>;const output = manipulatesEntries(entries);// Objectsconst objectExample = {a: 1};const objectEntries: Entries<typeof objectExample> = [['a', 1]];// Arraysconst arrayExample = ['a', 1];const arrayEntries: Entries<typeof arrayExample> = [[0, 'a'], [1, 1]];// Mapsconst mapExample = new Map([['a', 1]]);const mapEntries: Entries<typeof mapExample> = [['a', 1]];// Setsconst setExample = new Set(['a', 1]);const setEntries: Entries<typeof setExample> = [['a', 'a'], [1, 1]];Object Map Set Array
See Also
Entry
if you want to just access the type of a single entry.
type Entry
type Entry<BaseType> = BaseType extends Map<unknown, unknown> ? MapEntry<BaseType> : BaseType extends Set<unknown> ? SetEntry<BaseType> : BaseType extends readonly unknown[] ? ArrayEntry<BaseType> : BaseType extends object ? ObjectEntry<BaseType> : never;
Many collections have an
entries
method which returns an array of a given object's own enumerable string-keyed property [key, value] pairs. TheEntry
type will return the type of that collection's entry.For example the `Object`, `Map`, `Array`, and `Set` collections all have this method. Note that
WeakMap
andWeakSet
do not have this method since their entries are not enumerable.Example 1
import type {Entry} from 'type-fest';interface Example {someKey: number;}const manipulatesEntry = (example: Entry<Example>) => [// Does some arbitrary processing on the key (with type information available)example[0].toUpperCase(),// Does some arbitrary processing on the value (with type information available)example[1].toFixed(),];const example: Example = {someKey: 1};const entry = Object.entries(example)[0] as Entry<Example>;const output = manipulatesEntry(entry);// Objectsconst objectExample = {a: 1};const objectEntry: Entry<typeof objectExample> = ['a', 1];// Arraysconst arrayExample = ['a', 1];const arrayEntryString: Entry<typeof arrayExample> = [0, 'a'];const arrayEntryNumber: Entry<typeof arrayExample> = [1, 1];// Mapsconst mapExample = new Map([['a', 1]]);const mapEntry: Entry<typeof mapExample> = ['a', 1];// Setsconst setExample = new Set(['a', 1]);const setEntryString: Entry<typeof setExample> = ['a', 'a'];const setEntryNumber: Entry<typeof setExample> = [1, 1];Object Map Array Set
See Also
Entries
if you want to just access the type of the array of entries (which is the return of the.entries()
method).
type Exact
type Exact<ParameterType, InputType> = // Before distributing, check if the two types are equal and if so, return the parameter type immediately IsEqual<ParameterType, InputType> extends true ? ParameterType : // If the parameter is a primitive, return it as is immediately to avoid it being converted to a complex type ParameterType extends Primitive ? ParameterType : // If the parameter is an unknown, return it as is immediately to avoid it being converted to a complex type IsUnknown<ParameterType> extends true ? unknown : // If the parameter is a Function, return it as is because this type is not capable of handling function, leave it to TypeScript ParameterType extends Function ? ParameterType : // Convert union of array to array of union: A[] & B[] => (A & B)[] ParameterType extends unknown[] ? Array<Exact<ArrayElement<ParameterType>, ArrayElement<InputType>>> : // In TypeScript, Array is a subtype of ReadonlyArray, so always test Array before ReadonlyArray. ParameterType extends readonly unknown[] ? ReadonlyArray<Exact<ArrayElement<ParameterType>, ArrayElement<InputType>>> : ExactObject<ParameterType, InputType>;
Create a type that does not allow extra properties, meaning it only allows properties that are explicitly declared.
This is useful for function type-guarding to reject arguments with excess properties. Due to the nature of TypeScript, it does not complain if excess properties are provided unless the provided value is an object literal.
Please upvote [this issue](https://github.com/microsoft/TypeScript/issues/12936) if you want to have this type as a built-in in TypeScript.*
Example 1
type OnlyAcceptName = {name: string};function onlyAcceptName(arguments_: OnlyAcceptName) {}// TypeScript complains about excess properties when an object literal is provided.onlyAcceptName({name: 'name', id: 1});//=> `id` is excess// TypeScript does not complain about excess properties when the provided value is a variable (not an object literal).const invalidInput = {name: 'name', id: 1};onlyAcceptName(invalidInput); // No errorsHaving
Exact
allows TypeScript to reject excess properties.Example 2
import {Exact} from 'type-fest';type OnlyAcceptName = {name: string};function onlyAcceptNameImproved<T extends Exact<OnlyAcceptName, T>>(arguments_: T) {}const invalidInput = {name: 'name', id: 1};onlyAcceptNameImproved(invalidInput); // Compilation error[Read more](https://stackoverflow.com/questions/49580725/is-it-possible-to-restrict-typescript-object-to-contain-only-properties-defined)
Utilities
type Except
type Except< ObjectType, KeysType extends keyof ObjectType, Options extends ExceptOptions = { requireExactProps: false }> = { [KeyType in keyof ObjectType as Filter<KeyType, KeysType>]: ObjectType[KeyType];} & (Options['requireExactProps'] extends true ? Partial<Record<KeysType, never>> : {});
Create a type from an object type without certain keys.
We recommend setting the
requireExactProps
option totrue
.This type is a stricter version of [
Omit
](https://www.typescriptlang.org/docs/handbook/release-notes/typescript-3-5.html#the-omit-helper-type). TheOmit
type does not restrict the omitted keys to be keys present on the given type, whileExcept
does. The benefits of a stricter type are avoiding typos and allowing the compiler to pick up on rename refactors automatically.This type was proposed to the TypeScript team, which declined it, saying they prefer that libraries implement stricter versions of the built-in types ([microsoft/TypeScript#30825](https://github.com/microsoft/TypeScript/issues/30825#issuecomment-523668235)).
Example 1
import type {Except} from 'type-fest';type Foo = {a: number;b: string;};type FooWithoutA = Except<Foo, 'a'>;//=> {b: string}const fooWithoutA: FooWithoutA = {a: 1, b: '2'};//=> errors: 'a' does not exist in type '{ b: string; }'type FooWithoutB = Except<Foo, 'b', {requireExactProps: true}>;//=> {a: number} & Partial<Record<"b", never>>const fooWithoutB: FooWithoutB = {a: 1, b: '2'};//=> errors at 'b': Type 'string' is not assignable to type 'undefined'.Object
type FindGlobalInstanceType
type FindGlobalInstanceType<Name extends string> = Name extends string ? typeof globalThis extends Record< Name, abstract new (...arguments: any[]) => infer T > ? T : never : never;
Tries to find one or more types from their globally-defined constructors.
Use-case: Conditionally referencing DOM types only when the DOM library present.
Limitations:* Due to peculiarities with the behavior of
globalThis
, "globally defined" has a narrow definition in this case. Declaring a class in adeclare global
block won't work, instead you must declare its type using an interface and declare its constructor as avar
(*not*let
/const
) inside thedeclare global
block.Example 1
import type {FindGlobalInstanceType} from 'type-fest';class Point {constructor(public x: number, public y: number) {}}type PointLike = Point | FindGlobalInstanceType<'DOMPoint'>;Example 2
import type {FindGlobalInstanceType} from 'type-fest';declare global {// Class syntax won't add the key to `globalThis`class Foo {}// interface + constructor style worksinterface Bar {}var Bar: new () => Bar; // Not let or const}type FindFoo = FindGlobalInstanceType<'Foo'>; // Doesn't worktype FindBar = FindGlobalInstanceType<'Bar'>; // WorksUtilities
type FindGlobalType
type FindGlobalType<Name extends string> = typeof globalThis extends Record< Name, infer T> ? T : never;
Tries to find the type of a global with the given name.
Limitations: Due to peculiarities with the behavior of
globalThis
, "globally defined" only includesvar
declarations indeclare global
blocks, notlet
orconst
declarations.Example 1
import type {FindGlobalType} from 'type-fest';declare global {const foo: number; // let and const don't workvar bar: string; // var works}type FooType = FindGlobalType<'foo'> //=> never (let/const don't work)type BarType = FindGlobalType<'bar'> //=> stringtype OtherType = FindGlobalType<'other'> //=> never (no global named 'other')Utilities
type Finite
type Finite<T extends number> = T extends PositiveInfinity | NegativeInfinity ? never : T;
A finite
number
. You can't pass abigint
as they are already guaranteed to be finite.Use-case: Validating and documenting parameters.
Note: This can't detect
NaN
, please upvote [this issue](https://github.com/microsoft/TypeScript/issues/28682) if you want to have this type as a built-in in TypeScript.Example 1
import type {Finite} from 'type-fest';declare function setScore<T extends number>(length: Finite<T>): void;Numeric
type FixedLengthArray
type FixedLengthArray< Element, Length extends number, ArrayPrototype = [Element, ...Element[]]> = Pick<ArrayPrototype, Exclude<keyof ArrayPrototype, ArrayLengthMutationKeys>> & { [index: number]: Element; [Symbol.iterator]: () => IterableIterator<Element>; readonly length: Length;};
Create a type that represents an array of the given type and length. The array's length and the
Array
prototype methods that manipulate its length are excluded in the resulting type.Please participate in [this issue](https://github.com/microsoft/TypeScript/issues/26223) if you want to have a similar type built into TypeScript.
Use-cases: - Declaring fixed-length tuples or arrays with a large number of items. - Creating a range union (for example,
0 | 1 | 2 | 3 | 4
from the keys of such a type) without having to resort to recursive types. - Creating an array of coordinates with a static length, for example, length of 3 for a 3D vector.Note: This type does not prevent out-of-bounds access. Prefer
ReadonlyTuple
unless you need mutability.Example 1
import type {FixedLengthArray} from 'type-fest';type FencingTeam = FixedLengthArray<string, 3>;const guestFencingTeam: FencingTeam = ['Josh', 'Michael', 'Robert'];const homeFencingTeam: FencingTeam = ['George', 'John'];//=> error TS2322: Type string[] is not assignable to type 'FencingTeam'guestFencingTeam.push('Sam');//=> error TS2339: Property 'push' does not exist on type 'FencingTeam'Array
See Also
ReadonlyTuple
type Float
type Float<T> = T extends unknown // To distributive type ? IsFloat<T> extends true ? T : never : never;
A
number
that is not an integer.Use-case: Validating and documenting parameters.
It does not accept
Infinity
.Example 1
import type {Float} from 'type-fest';declare function setPercentage<T extends number>(length: Float<T>): void;See Also
Integer
Numeric
type Get
type Get< BaseType, Path extends | readonly string[] | LiteralStringUnion< ToString< | Paths<BaseType, { bracketNotation: false }> | Paths<BaseType, { bracketNotation: true }> > >, Options extends GetOptions = {}> = GetWithPath<BaseType, Path extends string ? ToPath<Path> : Path, Options>;
Get a deeply-nested property from an object using a key path, like Lodash's
.get()
function.Use-case: Retrieve a property from deep inside an API response or some other complex object.
Example 1
import type {Get} from 'type-fest';import * as lodash from 'lodash';const get = <BaseType, Path extends string | readonly string[]>(object: BaseType, path: Path): Get<BaseType, Path> =>lodash.get(object, path);interface ApiResponse {hits: {hits: Array<{_id: string_source: {name: Array<{given: string[]family: string}>birthDate: string}}>}}const getName = (apiResponse: ApiResponse) =>get(apiResponse, 'hits.hits[0]._source.name');//=> Array<{given: string[]; family: string}> | undefined// Path also supports a readonly array of stringsconst getNameWithPathArray = (apiResponse: ApiResponse) =>get(apiResponse, ['hits','hits', '0', '_source', 'name'] as const);//=> Array<{given: string[]; family: string}> | undefined// Non-strict mode:Get<string[], '3', {strict: false}> //=> stringGet<Record<string, string>, 'foo', {strict: true}> // => stringObject Array Template literal
type GetTagMetadata
type GetTagMetadata< Type extends Tag<TagName, unknown>, TagName extends PropertyKey> = Type[typeof tag][TagName];
Given a type and a tag name, returns the metadata associated with that tag on that type.
In the example below, one could use
Tagged<string, 'JSON'>
to represent "a string that is valid JSON". That type might be useful -- for instance, it communicates that the value can be safely passed toJSON.parse
without it throwing an exception. However, it doesn't indicate what type of value will be produced on parse (which is sometimes known).JsonOf<T>
solves this; it represents "a string that is valid JSON and that, if parsed, would produce a value of type T". The type T is held in the metadata associated with the'JSON'
tag.This article explains more about [how tag metadata works and when it can be useful](https://medium.com/@ethanresnick/advanced-typescript-tagged-types-improved-with-type-level-metadata-5072fc125fcf).
Example 1
import type {Tagged} from 'type-fest';type JsonOf<T> = Tagged<string, 'JSON', T>;function stringify<T>(it: T) {return JSON.stringify(it) as JsonOf<T>;}function parse<T extends JsonOf<unknown>>(it: T) {return JSON.parse(it) as GetTagMetadata<T, 'JSON'>;}const x = stringify({ hello: 'world' });const parsed = parse(x); // The type of `parsed` is { hello: string }Type
type GlobalThis
type GlobalThis = typeof globalThis;
Declare locally scoped properties on
globalThis
.When defining a global variable in a declaration file is inappropriate, it can be helpful to define a
type
orinterface
(sayExtraGlobals
) with the global variable and then castglobalThis
via code likeglobalThis as unknown as ExtraGlobals
.Instead of casting through
unknown
, you can update yourtype
orinterface
to extendGlobalThis
and then directly castglobalThis
.Example 1
import type {GlobalThis} from 'type-fest';type ExtraGlobals = GlobalThis & {readonly GLOBAL_TOKEN: string;};(globalThis as ExtraGlobals).GLOBAL_TOKEN;Type
type GreaterThan
type GreaterThan<A extends number, B extends number> = number extends A | B ? never : [ IsEqual<A, PositiveInfinity>, IsEqual<A, NegativeInfinity>, IsEqual<B, PositiveInfinity>, IsEqual<B, NegativeInfinity> ] extends infer R extends [boolean, boolean, boolean, boolean] ? Or< And<IsEqual<R[0], true>, IsEqual<R[2], false>>, And<IsEqual<R[3], true>, IsEqual<R[1], false>> > extends true ? true : Or< And<IsEqual<R[1], true>, IsEqual<R[3], false>>, And<IsEqual<R[2], true>, IsEqual<R[0], false>> > extends true ? false : true extends R[number] ? false : [IsNegative<A>, IsNegative<B>] extends infer R extends [boolean, boolean] ? [true, false] extends R ? false : [false, true] extends R ? true : [false, false] extends R ? PositiveNumericStringGt<`${A}`, `${B}`> : PositiveNumericStringGt<`${NumberAbsolute<B>}`, `${NumberAbsolute<A>}`> : never : never;
Returns a boolean for whether a given number is greater than another number.
Example 1
import type {GreaterThan} from 'type-fest';GreaterThan<1, -5>;//=> trueGreaterThan<1, 1>;//=> falseGreaterThan<1, 5>;//=> false
type GreaterThanOrEqual
type GreaterThanOrEqual<A extends number, B extends number> = number extends A | B ? never : A extends B ? true : GreaterThan<A, B>;
Returns a boolean for whether a given number is greater than or equal to another number.
Example 1
import type {GreaterThanOrEqual} from 'type-fest';GreaterThanOrEqual<1, -5>;//=> trueGreaterThanOrEqual<1, 1>;//=> trueGreaterThanOrEqual<1, 5>;//=> false
type HasOptionalKeys
type HasOptionalKeys<BaseType extends object> = OptionalKeysOf<BaseType> extends never ? false : true;
Creates a type that represents
true
orfalse
depending on whether the given type has any optional fields.This is useful when you want to create an API whose behavior depends on the presence or absence of optional fields.
Example 1
import type {HasOptionalKeys, OptionalKeysOf} from 'type-fest';type UpdateService<Entity extends object> = {removeField: HasOptionalKeys<Entity> extends true? (field: OptionalKeysOf<Entity>) => Promise<void>: never}Utilities
type HasReadonlyKeys
type HasReadonlyKeys<BaseType extends object> = ReadonlyKeysOf<BaseType> extends never ? false : true;
Creates a type that represents
true
orfalse
depending on whether the given type has any readonly fields.This is useful when you want to create an API whose behavior depends on the presence or absence of readonly fields.
Example 1
import type {HasReadonlyKeys, ReadonlyKeysOf} from 'type-fest';type UpdateService<Entity extends object> = {removeField: HasReadonlyKeys<Entity> extends true? (field: ReadonlyKeysOf<Entity>) => Promise<void>: never}Utilities
type HasRequiredKeys
type HasRequiredKeys<BaseType extends object> = RequiredKeysOf<BaseType> extends never ? false : true;
Creates a type that represents
true
orfalse
depending on whether the given type has any required fields.This is useful when you want to create an API whose behavior depends on the presence or absence of required fields.
Example 1
import type {HasRequiredKeys} from 'type-fest';type GeneratorOptions<Template extends object> = {prop1: number;prop2: string;} & (HasRequiredKeys<Template> extends true? {template: Template}: {template?: Template});interface Template1 {optionalSubParam?: string;}interface Template2 {requiredSubParam: string;}type Options1 = GeneratorOptions<Template1>;type Options2 = GeneratorOptions<Template2>;const optA: Options1 = {prop1: 0,prop2: 'hi'};const optB: Options1 = {prop1: 0,prop2: 'hi',template: {}};const optC: Options1 = {prop1: 0,prop2: 'hi',template: {optionalSubParam: 'optional value'}};const optD: Options2 = {prop1: 0,prop2: 'hi',template: {requiredSubParam: 'required value'}};Utilities
type HasWritableKeys
type HasWritableKeys<BaseType extends object> = WritableKeysOf<BaseType> extends never ? false : true;
Creates a type that represents
true
orfalse
depending on whether the given type has any writable fields.This is useful when you want to create an API whose behavior depends on the presence or absence of writable fields.
Example 1
import type {HasWritableKeys, WritableKeysOf} from 'type-fest';type UpdateService<Entity extends object> = {removeField: HasWritableKeys<Entity> extends true? (field: WritableKeysOf<Entity>) => Promise<void>: never}Utilities
type IfAny
type IfAny<T, TypeIfAny = true, TypeIfNotAny = false> = IsAny<T> extends true ? TypeIfAny : TypeIfNotAny;
An if-else-like type that resolves depending on whether the given type is
any
.Example 1
import type {IfAny} from 'type-fest';type ShouldBeTrue = IfAny<any>;//=> truetype ShouldBeBar = IfAny<'not any', 'foo', 'bar'>;//=> 'bar'Type Guard Utilities
See Also
type IfEmptyObject
type IfEmptyObject< T, TypeIfEmptyObject = true, TypeIfNotEmptyObject = false> = IsEmptyObject<T> extends true ? TypeIfEmptyObject : TypeIfNotEmptyObject;
An if-else-like type that resolves depending on whether the given type is
{}
.Example 1
import type {IfEmptyObject} from 'type-fest';type ShouldBeTrue = IfEmptyObject<{}>;//=> truetype ShouldBeBar = IfEmptyObject<{key: any}, 'foo', 'bar'>;//=> 'bar'Type Guard Utilities
See Also
type IfNever
type IfNever<T, TypeIfNever = true, TypeIfNotNever = false> = IsNever<T> extends true ? TypeIfNever : TypeIfNotNever;
An if-else-like type that resolves depending on whether the given type is
never
.Example 1
import type {IfNever} from 'type-fest';type ShouldBeTrue = IfNever<never>;//=> truetype ShouldBeBar = IfNever<'not never', 'foo', 'bar'>;//=> 'bar'Type Guard Utilities
See Also
type IfNull
type IfNull<T, TypeIfNull = true, TypeIfNotNull = false> = IsNull<T> extends true ? TypeIfNull : TypeIfNotNull;
An if-else-like type that resolves depending on whether the given type is
null
.Example 1
import type {IfNull} from 'type-fest';type ShouldBeTrue = IfNull<null>;//=> truetype ShouldBeBar = IfNull<'not null', 'foo', 'bar'>;//=> 'bar'Type Guard Utilities
See Also
type IfUnknown
type IfUnknown< T, TypeIfUnknown = true, TypeIfNotUnknown = false> = IsUnknown<T> extends true ? TypeIfUnknown : TypeIfNotUnknown;
An if-else-like type that resolves depending on whether the given type is
unknown
.Example 1
import type {IfUnknown} from 'type-fest';type ShouldBeTrue = IfUnknown<unknown>;//=> truetype ShouldBeBar = IfUnknown<'not unknown', 'foo', 'bar'>;//=> 'bar'Type Guard Utilities
See Also
type Includes
type Includes<Value extends readonly any[], Item> = Value extends readonly [ Value[0], ...infer rest] ? IsEqual<Value[0], Item> extends true ? true : Includes<rest, Item> : false;
Returns a boolean for whether the given array includes the given item.
This can be useful if another type wants to make a decision based on whether the array includes that item.
Example 1
import type {Includes} from 'type-fest';type hasRed<array extends any[]> = Includes<array, 'red'>;Array
type IntClosedRange
type IntClosedRange< Start extends number, End extends number, Skip extends number = 1> = IntRange<Start, Sum<End, 1>, Skip>;
Generate a union of numbers.
The numbers are created from the given
Start
(inclusive) parameter to the givenEnd
(inclusive) parameter.You skip over numbers using the
Step
parameter (defaults to1
). For example,IntClosedRange<0, 10, 2>
will create a union of0 | 2 | 4 | 6 | 8 | 10
.Note:
Start
orEnd
must be non-negative and smaller than999
.Use-cases: 1. This can be used to define a set of valid input/output values. for example: ``` type Age = IntClosedRange<0, 120>; //=> 0 | 1 | 2 | ... | 119 | 120 type FontSize = IntClosedRange<10, 20>; //=> 10 | 11 | ... | 19 | 20 type EvenNumber = IntClosedRange<0, 10, 2>; //=> 0 | 2 | 4 | 6 | 8 | 10 ``` 2. This can be used to define random numbers in a range. For example,
type RandomNumber = IntClosedRange<0, 100>;
Example 1
import type {IntClosedRange} from 'type-fest';// Create union type `0 | 1 | ... | 9`type ZeroToNine = IntClosedRange<0, 9>;// Create union type `100 | 200 | 300 | ... | 900`type Hundreds = IntClosedRange<100, 900, 100>;See Also
IntRange
type Integer
type Integer<T> = T extends unknown // To distributive type ? IsInteger<T> extends true ? T : never : never;
A
number
that is an integer.Use-case: Validating and documenting parameters.
Example 1
type Integer = Integer<1>;//=> 1type IntegerWithDecimal = Integer<1.0>;//=> 1type NegativeInteger = Integer<-1>;//=> -1type Float = Integer<1.5>;//=> never// Supports non-decimal numberstype OctalInteger: Integer<0o10>;//=> 0o10type BinaryInteger: Integer<0b10>;//=> 0b10type HexadecimalInteger: Integer<0x10>;//=> 0x10Example 2
import type {Integer} from 'type-fest';declare function setYear<T extends number>(length: Integer<T>): void;See Also
NegativeInteger
NonNegativeInteger
Numeric
type IntRange
type IntRange< Start extends number, End extends number, Step extends number = 1> = PrivateIntRange<Start, End, Step>;
Generate a union of numbers.
The numbers are created from the given
Start
(inclusive) parameter to the givenEnd
(exclusive) parameter.You skip over numbers using the
Step
parameter (defaults to1
). For example,IntRange<0, 10, 2>
will create a union of0 | 2 | 4 | 6 | 8
.Note:
Start
orEnd
must be non-negative and smaller than1000
.Use-cases: 1. This can be used to define a set of valid input/output values. for example: ``` type Age = IntRange<0, 120>; type FontSize = IntRange<10, 20>; type EvenNumber = IntRange<0, 11, 2>; //=> 0 | 2 | 4 | 6 | 8 | 10 ``` 2. This can be used to define random numbers in a range. For example,
type RandomNumber = IntRange<0, 100>;
Example 1
import type {IntRange} from 'type-fest';// Create union type `0 | 1 | ... | 9`type ZeroToNine = IntRange<0, 10>;// Create union type `100 | 200 | 300 | ... | 900`type Hundreds = IntRange<100, 901, 100>;See Also
IntClosedRange
type InvariantOf
type InvariantOf<Type> = Type & { [invariantBrand]: (_: Type) => Type };
Create an [invariant type](https://basarat.gitbook.io/typescript/type-system/type-compatibility#footnote-invariance), which is a type that does not accept supertypes and subtypes.
Use-case: - Prevent runtime errors that may occur due to assigning subtypes to supertypes. - Improve type signature of object methods like [
Object.keys()
orObject.entries()
](https://github.com/microsoft/TypeScript/pull/12253#issuecomment-263132208) by sealing the object type.Example 1
import type {InvariantOf} from 'type-fest';class Animal {constructor(public name: string){}}class Cat extends Animal {meow() {}}let animalArray: Animal[] = [animal];let catArray: Cat[] = [cat];animalArray = catArray; // Okay if covariantanimalArray.push(new Animal('another animal')); // Pushed an animal into catArraycatArray.forEach(c => c.meow()); // Allowed but, error at runtimelet invariantAnimalArray: InvariantOf<Animal>[] = [animal] as InvariantOf<Animal>[];let invariantCatArray: InvariantOf<Cat>[] = [cat] as InvariantOf<Cat>[];invariantAnimalArray = invariantCatArray; // Error: Type 'InvariantOf<Cat>[]' is not assignable to type 'InvariantOf<Animal>[]'.Example 2
import type {InvariantOf} from 'type-fest';// In covariance (default)interface FooBar {foo: number;bar: string}interface FooBarBaz extends FooBar {baz: boolean}declare const fooBar: FooBardeclare const fooBarBaz: FooBarBazfunction keyOfFooBar(fooBar: FooBar) {return Object.keys(fooBar) as (keyof FooBar)[]}keyOfFooBar(fooBar) //=> (keyof FooBar)[]keyOfFooBar(fooBarBaz) //=> (keyof FooBar)[] but, (keyof FooBarBaz)[] at runtime// In invarianceexport function invariantOf<Type>(value: Type): InvariantOf<Type> {return value as InvariantOf<Type>;}function keyOfInvariantFooBar(fooBar: InvariantOf<FooBar>) {return Object.keys(fooBar) as (keyof FooBar)[]}keyOfInvariantFooBar(invariantOf(fooBar)); // (keyof FooBar)[]keyOfInvariantFooBar(invariantOf(fooBarBaz)); // Error: Argument of type 'InvariantOf<FooBarBaz>' is not assignable to parameter of type 'InvariantOf<FooBar>'.Type
type IsAny
type IsAny<T> = 0 extends 1 & NoInfer<T> ? true : false;
Returns a boolean for whether the given type is
any
.https://stackoverflow.com/a/49928360/1490091
Useful in type utilities, such as disallowing
any
s to be passed to a function.Example 1
import type {IsAny} from 'type-fest';const typedObject = {a: 1, b: 2} as const;const anyObject: any = {a: 1, b: 2};function get<O extends (IsAny<O> extends true ? {} : Record<string, number>), K extends keyof O = keyof O>(obj: O, key: K) {return obj[key];}const typedA = get(typedObject, 'a');//=> 1const anyA = get(anyObject, 'a');//=> anyType Guard Utilities
type IsBooleanLiteral
type IsBooleanLiteral<T> = LiteralCheck<T, boolean>;
Returns a boolean for whether the given type is a
true
orfalse
[literal type](https://www.typescriptlang.org/docs/handbook/2/everyday-types.html#literal-types).Useful for: - providing strongly-typed functions when given literal arguments - type utilities, such as when constructing parsers and ASTs
Example 1
import type {IsBooleanLiteral} from 'type-fest';const id = 123;type GetId<AsString extends boolean> =IsBooleanLiteral<AsString> extends true? AsString extends true? `${typeof id}`: typeof id: number | string;function getId<AsString extends boolean = false>(options?: {asString: AsString}) {return (options?.asString ? `${id}` : id) as GetId<AsString>;}const numberId = getId();//=> 123const stringId = getId({asString: true});//=> '123'declare const runtimeBoolean: boolean;const eitherId = getId({asString: runtimeBoolean});//=> number | stringType Guard Utilities
type IsEmptyObject
type IsEmptyObject<T> = T extends EmptyObject ? true : false;
Returns a
boolean
for whether the type is strictly equal to an empty plain object, the{}
value.Example 1
import type {IsEmptyObject} from 'type-fest';type Pass = IsEmptyObject<{}>; //=> truetype Fail = IsEmptyObject<[]>; //=> falsetype Fail = IsEmptyObject<null>; //=> falseSee Also
EmptyObject Object
type IsEqual
type IsEqual<A, B> = (<G>() => G extends (A & G) | G ? 1 : 2) extends < G>() => G extends (B & G) | G ? 1 : 2 ? true : false;
Returns a boolean for whether the two given types are equal.
https://github.com/microsoft/TypeScript/issues/27024#issuecomment-421529650 https://stackoverflow.com/questions/68961864/how-does-the-equals-work-in-typescript/68963796#68963796
Use-cases: - If you want to make a conditional branch based on the result of a comparison of two types.
Example 1
import type {IsEqual} from 'type-fest';// This type returns a boolean for whether the given array includes the given item.// `IsEqual` is used to compare the given array at position 0 and the given item and then return true if they are equal.type Includes<Value extends readonly any[], Item> =Value extends readonly [Value[0], ...infer rest]? IsEqual<Value[0], Item> extends true? true: Includes<rest, Item>: false;Type Guard Utilities
type IsFloat
type IsFloat<T> = T extends number ? `${T}` extends `${infer _Sign extends | '' | '-'}${number}.${infer Decimal extends number}` ? Decimal extends Zero ? false : true : false : false;
Returns a boolean for whether the given number is a float, like
1.5
or-1.5
.It returns
false
forInfinity
.Use-case: - If you want to make a conditional branch based on the result of whether a number is a float or not.
Example 1
type Float = IsFloat<1.5>;//=> truetype IntegerWithDecimal = IsInteger<1.0>;//=> falsetype NegativeFloat = IsInteger<-1.5>;//=> truetype Infinity_ = IsInteger<Infinity>;//=> false
type IsInteger
type IsInteger<T> = T extends bigint ? true : T extends number ? number extends T ? false : T extends PositiveInfinity | NegativeInfinity ? false : Not<IsFloat<T>> : false;
Returns a boolean for whether the given number is a integer, like
-5
,1.0
or100
.Like [
Number#IsInteger()
](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number/IsInteger) but for types.Use-case: - If you want to make a conditional branch based on the result of whether a number is a intrger or not.
Example 1
type Integer = IsInteger<1>;//=> truetype IntegerWithDecimal = IsInteger<1.0>;//=> truetype NegativeInteger = IsInteger<-1>;//=> truetype Float = IsInteger<1.5>;//=> false// Supports non-decimal numberstype OctalInteger: IsInteger<0o10>;//=> truetype BinaryInteger: IsInteger<0b10>;//=> truetype HexadecimalInteger: IsInteger<0x10>;//=> true
type IsLiteral
type IsLiteral<T> = IsPrimitive<T> extends true ? IsNotFalse<IsLiteralUnion<T>> : false;
Returns a boolean for whether the given type is a [literal type](https://www.typescriptlang.org/docs/handbook/2/everyday-types.html#literal-types).
Useful for: - providing strongly-typed functions when given literal arguments - type utilities, such as when constructing parsers and ASTs
Example 1
import type {IsLiteral} from 'type-fest';// https://github.com/inocan-group/inferred-types/blob/master/src/types/string-literals/StripLeading.tsexport type StripLeading<A, B> =A extends string? B extends string? IsLiteral<A> extends true? string extends B ? never : A extends `${B & string}${infer After}` ? After : A: string: A: A;function stripLeading<Input extends string, Strip extends string>(input: Input, strip: Strip) {return input.replace(`^${strip}`, '') as StripLeading<Input, Strip>;}stripLeading('abc123', 'abc');//=> '123'const str = 'abc123' as string;stripLeading(str, 'abc');//=> stringType Guard Utilities
type IsNegative
type IsNegative<T extends Numeric> = T extends Negative<T> ? true : false;
Returns a boolean for whether the given number is a negative number.
Example 1
import type {IsNegative} from 'type-fest';type ShouldBeFalse = IsNegative<1>;type ShouldBeTrue = IsNegative<-1>;Numeric
See Also
Negative
type IsNever
type IsNever<T> = [T] extends [never] ? true : false;
Returns a boolean for whether the given type is
never
.https://github.com/microsoft/TypeScript/issues/31751#issuecomment-498526919 https://stackoverflow.com/a/53984913/10292952 https://www.zhenghao.io/posts/ts-never
Useful in type utilities, such as checking if something does not occur.
Example 1
import type {IsNever, And} from 'type-fest';// https://github.com/andnp/SimplyTyped/blob/master/src/types/strings.tstype AreStringsEqual<A extends string, B extends string> =And<IsNever<Exclude<A, B>> extends true ? true : false,IsNever<Exclude<B, A>> extends true ? true : false>;type EndIfEqual<I extends string, O extends string> =AreStringsEqual<I, O> extends true? never: void;function endIfEqual<I extends string, O extends string>(input: I, output: O): EndIfEqual<I, O> {if (input === output) {process.exit(0);}}endIfEqual('abc', 'abc');//=> neverendIfEqual('abc', '123');//=> voidType Guard Utilities
type IsNull
type IsNull<T> = [T] extends [null] ? true : false;
Returns a boolean for whether the given type is
null
.Example 1
import type {IsNull} from 'type-fest';type NonNullFallback<T, Fallback> = IsNull<T> extends true ? Fallback : T;type Example1 = NonNullFallback<null, string>;//=> stringtype Example2 = NonNullFallback<number, string>;//=? numberType Guard Utilities
type IsNumericLiteral
type IsNumericLiteral<T> = LiteralChecks<T, Numeric>;
Returns a boolean for whether the given type is a
number
orbigint
[literal type](https://www.typescriptlang.org/docs/handbook/2/everyday-types.html#literal-types).Useful for: - providing strongly-typed functions when given literal arguments - type utilities, such as when constructing parsers and ASTs
Example 1
import type {IsNumericLiteral} from 'type-fest';// https://github.com/inocan-group/inferred-types/blob/master/src/types/boolean-logic/EndsWith.tstype EndsWith<TValue, TEndsWith extends string> =TValue extends string? IsStringLiteral<TEndsWith> extends true? IsStringLiteral<TValue> extends true? TValue extends `${string}${TEndsWith}`? true: false: boolean: boolean: TValue extends number? IsNumericLiteral<TValue> extends true? EndsWith<`${TValue}`, TEndsWith>: false: false;function endsWith<Input extends string | number, End extends string>(input: Input, end: End) {return `${input}`.endsWith(end) as EndsWith<Input, End>;}endsWith('abc', 'c');//=> trueendsWith(123456, '456');//=> trueconst end = '123' as string;endsWith('abc123', end);//=> booleanType Guard Utilities
type IsStringLiteral
type IsStringLiteral<T> = LiteralCheck<T, string>;
Returns a boolean for whether the given type is a
string
[literal type](https://www.typescriptlang.org/docs/handbook/2/everyday-types.html#literal-types).Useful for: - providing strongly-typed string manipulation functions - constraining strings to be a string literal - type utilities, such as when constructing parsers and ASTs
Example 1
import type {IsStringLiteral} from 'type-fest';type CapitalizedString<T extends string> = IsStringLiteral<T> extends true ? Capitalize<T> : string;// https://github.com/yankeeinlondon/native-dash/blob/master/src/capitalize.tsfunction capitalize<T extends Readonly<string>>(input: T): CapitalizedString<T> {return (input.slice(0, 1).toUpperCase() + input.slice(1)) as CapitalizedString<T>;}const output = capitalize('hello, world!');//=> 'Hello, world!'Type Guard Utilities
type IsSymbolLiteral
type IsSymbolLiteral<T> = LiteralCheck<T, symbol>;
Returns a boolean for whether the given type is a
symbol
[literal type](https://www.typescriptlang.org/docs/handbook/2/everyday-types.html#literal-types).Useful for: - providing strongly-typed functions when given literal arguments - type utilities, such as when constructing parsers and ASTs
Example 1
import type {IsSymbolLiteral} from 'type-fest';type Get<Obj extends Record<symbol, number>, Key extends keyof Obj> =IsSymbolLiteral<Key> extends true? Obj[Key]: number;function get<Obj extends Record<symbol, number>, Key extends keyof Obj>(o: Obj, key: Key) {return o[key] as Get<Obj, Key>;}const symbolLiteral = Symbol('literal');const symbolValue: symbol = Symbol('value');get({[symbolLiteral]: 1} as const, symbolLiteral);//=> 1get({[symbolValue]: 1} as const, symbolValue);//=> numberType Guard Utilities
type IsTuple
type IsTuple< TArray extends UnknownArray, Options extends IsTupleOptions = { fixedLengthOnly: true }> = IfAny< TArray, boolean, IfNever< TArray, false, TArray extends unknown // For distributing `TArray` ? number extends TArray['length'] ? Options['fixedLengthOnly'] extends false ? IfNever< keyof TArray & `${number}`, TArray extends readonly [...any, any] ? true : false, // To handle cases where a non-rest element follows a rest element, e.g., `[...number[], number]` true > : false : true : false >>;
Returns a boolean for whether the given array is a tuple.
Use-case: - If you want to make a conditional branch based on the result of whether an array is a tuple or not.
Note:
IsTuple
returnsboolean
when instantiated with a union of tuple and non-tuple (e.g.,IsTuple<[1, 2] | number[]>
).Example 1
import type {IsTuple} from 'type-fest';type Tuple = IsTuple<[1, 2, 3]>;//=> truetype NotTuple = IsTuple<number[]>;//=> falsetype TupleWithOptionalItems = IsTuple<[1?, 2?]>;//=> truetype RestItemsNotAllowed = IsTuple<[1, 2, ...number[]]>;//=> falsetype RestItemsAllowed = IsTuple<[1, 2, ...number[]], {fixedLengthOnly: false}>;//=> trueSee Also
Type Guard Utilities
type IsUnknown
type IsUnknown<T> = unknown extends T // `T` can be `unknown` or `any` ? IsNull<T> extends false // `any` can be `null`, but `unknown` can't be ? true : false : false;
Returns a boolean for whether the given type is
unknown
.https://github.com/dsherret/conditional-type-checks/pull/16
Useful in type utilities, such as when dealing with unknown data from API calls.
Example 1
import type {IsUnknown} from 'type-fest';// https://github.com/pajecawav/tiny-global-store/blob/master/src/index.tstype Action<TState, TPayload = void> =IsUnknown<TPayload> extends true? (state: TState) => TState,: (state: TState, payload: TPayload) => TState;class Store<TState> {constructor(private state: TState) {}execute<TPayload = void>(action: Action<TState, TPayload>, payload?: TPayload): TState {this.state = action(this.state, payload);return this.state;}// ... other methods}const store = new Store({value: 1});declare const someExternalData: unknown;store.execute(state => ({value: state.value + 1}));//=> `TPayload` is `void`store.execute((state, payload) => ({value: state.value + payload}), 5);//=> `TPayload` is `5`store.execute((state, payload) => ({value: state.value + payload}), someExternalData);//=> Errors: `action` is `(state: TState) => TState`Utilities
type IterableElement
type IterableElement<TargetIterable> = TargetIterable extends Iterable< infer ElementType> ? ElementType : TargetIterable extends AsyncIterable<infer ElementType> ? ElementType : never;
Get the element type of an
Iterable
/AsyncIterable
. For example,Array
,Set
,Map
, generator, stream, etc.This can be useful, for example, if you want to get the type that is yielded in a generator function. Often the return type of those functions are not specified.
This type works with both
Iterable
s andAsyncIterable
s, so it can be use with synchronous and asynchronous generators.Here is an example of
IterableElement
in action with a generator function:Example 1
import type {IterableElement} from 'type-fest';function * iAmGenerator() {yield 1;yield 2;}type MeNumber = IterableElement<ReturnType<typeof iAmGenerator>>And here is an example with an async generator:
Example 2
import type {IterableElement} from 'type-fest';async function * iAmGeneratorAsync() {yield 'hi';yield true;}type MeStringOrBoolean = IterableElement<ReturnType<typeof iAmGeneratorAsync>>Many types in JavaScript/TypeScript are iterables. This type works on all types that implement those interfaces.
An example with an array of strings:
Example 3
import type {IterableElement} from 'type-fest';type MeString = IterableElement<string[]>Example 4
import type {IterableElement} from 'type-fest';const fruits = new Set(['🍎', '🍌', '🍉'] as const);type Fruit = IterableElement<typeof fruits>;//=> '🍎' | '🍌' | '🍉'Iterable
type Join
type Join< Items extends readonly JoinableItem[], Delimiter extends string> = Items extends readonly [] ? '' : Items extends readonly [JoinableItem?] ? `${NullishCoalesce<Items[0], ''>}` : Items extends readonly [ infer First extends JoinableItem, ...infer Tail extends readonly JoinableItem[] ] ? `${NullishCoalesce<First, ''>}${Delimiter}${Join<Tail, Delimiter>}` : Items extends readonly [ ...infer Head extends readonly JoinableItem[], infer Last extends JoinableItem ] ? `${Join<Head, Delimiter>}${Delimiter}${NullishCoalesce<Last, ''>}` : string;
Join an array of strings and/or numbers using the given string as a delimiter.
Use-case: Defining key paths in a nested object. For example, for dot-notation fields in MongoDB queries.
Example 1
import type {Join} from 'type-fest';// Mixed (strings & numbers) items; result is: 'foo.0.baz'const path: Join<['foo', 0, 'baz'], '.'> = ['foo', 0, 'baz'].join('.');// Only string items; result is: 'foo.bar.baz'const path: Join<['foo', 'bar', 'baz'], '.'> = ['foo', 'bar', 'baz'].join('.');// Only number items; result is: '1.2.3'const path: Join<[1, 2, 3], '.'> = [1, 2, 3].join('.');// Only bigint items; result is '1.2.3'const path: Join<[1n, 2n, 3n], '.'> = [1n, 2n, 3n].join('.');// Only boolean items; result is: 'true.false.true'const path: Join<[true, false, true], '.'> = [true, false, true].join('.');// Contains nullish items; result is: 'foo..baz..xyz'const path: Join<['foo', undefined, 'baz', null, 'xyz'], '.'> = ['foo', undefined, 'baz', null, 'xyz'].join('.');// Partial tuple shapes (rest param last); result is: `prefix.${string}`const path: Join<['prefix', ...string[]], '.'> = ['prefix'].join('.');// Partial tuple shapes (rest param first); result is: `${string}.suffix`const path: Join<[...string[], 'suffix'], '.'> = ['suffix'].join('.');// Tuples items with nullish unions; result is '.' | 'hello.' | '.world' | 'hello.world'const path: Join<['hello' | undefined, 'world' | null], '.'> = ['hello', 'world'].join('.');Array Template literal
type JsonArray
type JsonArray = JsonValue[] | readonly JsonValue[];
Matches a JSON array.
JSON
type Jsonifiable
type Jsonifiable = JsonPrimitive | JsonifiableObject | JsonifiableArray;
Matches a value that can be losslessly converted to JSON.
Can be used to type values that you expect to pass to
JSON.stringify
.undefined
is allowed in object fields (for example,{a?: number}
) as a special case even thoughJSON.stringify({a: undefined})
is{}
because it makes this class more widely useful and checking for undefined-but-present values is likely an anti-pattern.Example 1
import type {Jsonifiable} from 'type-fest';// @ts-expect-errorconst error: Jsonifiable = {map: new Map([['a', 1]]),};JSON.stringify(error);//=> {"map": {}}const good: Jsonifiable = {number: 3,date: new Date(),missing: undefined,}JSON.stringify(good);//=> {"number": 3, "date": "2022-10-17T22:22:35.920Z"}JSON
type Jsonify
type Jsonify<T> = IsAny<T> extends true ? any : T extends PositiveInfinity | NegativeInfinity ? null : T extends JsonPrimitive ? T : // Any object with toJSON is special case T extends { toJSON(): infer J } ? (() => J) extends () => JsonValue // Is J assignable to JsonValue? ? J // Then T is Jsonable and its Jsonable value is J : Jsonify<J> // Maybe if we look a level deeper we'll find a JsonValue : // Instanced primitives are objects T extends Number ? number : T extends String ? string : T extends Boolean ? boolean : T extends Map<any, any> | Set<any> ? EmptyObject : T extends TypedArray ? Record<string, number> : T extends NotJsonable ? never // Non-JSONable type union was found not empty : T extends UnknownArray ? JsonifyList<T> : T extends object ? JsonifyObject<UndefinedToOptional<T>> // JsonifyObject recursive call for its children : never;
Transform a type to one that is assignable to the
JsonValue
type.This includes: 1. Transforming JSON
interface
to atype
that is assignable toJsonValue
. 2. Transforming non-JSON value that is *jsonable* to a type that is assignable toJsonValue
, where *jsonable* means the non-JSON value implements the.toJSON()
method that returns a value that is assignable toJsonValue
.Remarks
An interface cannot be structurally compared to
JsonValue
because an interface can be re-opened to add properties that may not be satisfyJsonValue
.Example 1
import type {Jsonify, JsonValue} from 'type-fest';interface Geometry {type: 'Point' | 'Polygon';coordinates: [number, number];}const point: Geometry = {type: 'Point',coordinates: [1, 1]};const problemFn = (data: JsonValue) => {// Does something with data};problemFn(point); // Error: type Geometry is not assignable to parameter of type JsonValue because it is an interfaceconst fixedFn = <T>(data: Jsonify<T>) => {// Does something with data};fixedFn(point); // Good: point is assignable. Jsonify<T> transforms Geometry into value assignable to JsonValuefixedFn(new Date()); // Error: As expected, Date is not assignable. Jsonify<T> cannot transforms Date into value assignable to JsonValueNon-JSON values such as
Date
implement.toJSON()
, so they can be transformed to a value assignable toJsonValue
:Example 2
import type {Jsonify} from 'type-fest';const time = {timeValue: new Date()};// `Jsonify<typeof time>` is equivalent to `{timeValue: string}`const timeJson = JSON.parse(JSON.stringify(time)) as Jsonify<typeof time>;https://github.com/Microsoft/TypeScript/issues/1897#issuecomment-710744173
JSON
type JsonObject
type JsonObject = { [Key in string]: JsonValue } & { [Key in string]?: JsonValue | undefined;};
Matches a JSON object.
This type can be useful to enforce some input to be JSON-compatible or as a super-type to be extended from. Don't use this as a direct return type as the user would have to double-cast it:
jsonObject as unknown as CustomResponse
. Instead, you could extend your CustomResponse type from it to ensure your type only uses JSON-compatible types:interface CustomResponse extends JsonObject { … }
.JSON
type JsonPrimitive
type JsonPrimitive = string | number | boolean | null;
Matches any valid JSON primitive value.
JSON
type JsonValue
type JsonValue = JsonPrimitive | JsonObject | JsonArray;
Matches any valid JSON value.
See Also
Jsonify
if you need to transform a type to one that is assignable toJsonValue
.JSON
type KebabCase
type KebabCase<Value> = DelimiterCase<Value, '-'>;
Convert a string literal to kebab-case.
This can be useful when, for example, converting a camel-cased object property to a kebab-cased CSS class name or a command-line flag.
Example 1
import type {KebabCase} from 'type-fest';// Simpleconst someVariable: KebabCase<'fooBar'> = 'foo-bar';// Advancedtype KebabCasedProperties<T> = {[K in keyof T as KebabCase<K>]: T[K]};interface CliOptions {dryRun: boolean;includeFile: string;foo: number;}const rawCliOptions: KebabCasedProperties<CliOptions> = {'dry-run': true,'include-file': 'bar.js',foo: 123};Change case Template literal
type KebabCasedProperties
type KebabCasedProperties<Value> = DelimiterCasedProperties<Value, '-'>;
Convert object properties to kebab case but not recursively.
This can be useful when, for example, converting some API types from a different style.
Example 1
import type {KebabCasedProperties} from 'type-fest';interface User {userId: number;userName: string;}const result: KebabCasedProperties<User> = {'user-id': 1,'user-name': 'Tom',};Change case Template literal Object
See Also
KebabCase
KebabCasedPropertiesDeep
type KebabCasedPropertiesDeep
type KebabCasedPropertiesDeep<Value> = DelimiterCasedPropertiesDeep<Value, '-'>;
Convert object properties to kebab case recursively.
This can be useful when, for example, converting some API types from a different style.
Example 1
import type [KebabCasedPropertiesDeep] from 'type-fest';interface User {userId: number;userName: string;}interface UserWithFriends {userInfo: User;userFriends: User[];}const result: KebabCasedPropertiesDeep<UserWithFriends> = {'user-info': {'user-id': 1,'user-name': 'Tom',},'user-friends': [{'user-id': 2,'user-name': 'Jerry',},{'user-id': 3,'user-name': 'Spike',},],};Change case Template literal Object
See Also
KebabCase
KebabCasedProperties
type KeysOfUnion
type KeysOfUnion<ObjectType> = ObjectType extends unknown ? keyof ObjectType : never;
Create a union of all keys from a given type, even those exclusive to specific union members.
Unlike the native
keyof
keyword, which returns keys present in **all** union members, this type returns keys from **any** member.https://stackoverflow.com/a/49402091
Example 1
import type {KeysOfUnion} from 'type-fest';type A = {common: string;a: number;};type B = {common: string;b: string;};type C = {common: string;c: boolean;};type Union = A | B | C;type CommonKeys = keyof Union;//=> 'common'type AllKeys = KeysOfUnion<Union>;//=> 'common' | 'a' | 'b' | 'c'Object
type LastArrayElement
type LastArrayElement< Elements extends readonly unknown[], ElementBeforeTailingSpreadElement = never> = // If the last element of an array is a spread element, the `LastArrayElement` result should be `'the type of the element before the spread element' | 'the type of the spread element'`. Elements extends readonly [] ? ElementBeforeTailingSpreadElement : Elements extends readonly [...infer U, infer V] ? V : Elements extends readonly [infer U, ...infer V] ? // If we return `V[number] | U` directly, it would be wrong for `[[string, boolean, object, ...number[]]`. // So we need to recurse type `V` and carry over the type of the element before the spread element. LastArrayElement<V, U> : Elements extends ReadonlyArray<infer U> ? U | ElementBeforeTailingSpreadElement : never;
Extracts the type of the last element of an array.
Use-case: Defining the return type of functions that extract the last element of an array, for example [
lodash.last
](https://lodash.com/docs/4.17.15#last).Example 1
import type {LastArrayElement} from 'type-fest';declare function lastOf<V extends readonly any[]>(array: V): LastArrayElement<V>;const array = ['foo', 2];typeof lastOf(array);//=> numberconst array = ['foo', 2] as const;typeof lastOf(array);//=> 2Array Template literal
type LessThan
type LessThan<A extends number, B extends number> = number extends A | B ? never : GreaterThanOrEqual<A, B> extends true ? false : true;
Returns a boolean for whether a given number is less than another number.
Example 1
import type {LessThan} from 'type-fest';LessThan<1, -5>;//=> falseLessThan<1, 1>;//=> falseLessThan<1, 5>;//=> true
type LessThanOrEqual
type LessThanOrEqual<A extends number, B extends number> = number extends A | B ? never : GreaterThan<A, B> extends true ? false : true;
Returns a boolean for whether a given number is less than or equal to another number.
Example 1
import type {LessThanOrEqual} from 'type-fest';LessThanOrEqual<1, -5>;//=> falseLessThanOrEqual<1, 1>;//=> trueLessThanOrEqual<1, 5>;//=> true
type LiteralToPrimitive
type LiteralToPrimitive<T> = T extends number ? number : T extends bigint ? bigint : T extends string ? string : T extends boolean ? boolean : T extends symbol ? symbol : T extends null ? null : T extends undefined ? undefined : never;
Given a [literal type](https://www.typescriptlang.org/docs/handbook/2/everyday-types.html#literal-types) return the primitive type it belongs to, or
never
if it's not a primitive.Use-case: Working with generic types that may be literal types.
Example 1
import type {LiteralToPrimitive} from 'type-fest';// No overloads needed to get the correct return typefunction plus<T extends number | bigint | string>(x: T, y: T): LiteralToPrimitive<T> {return x + (y as any);}plus('a', 'b'); // stringplus(1, 2); // numberplus(1n, 2n); // bigintType
type LiteralToPrimitiveDeep
type LiteralToPrimitiveDeep<T> = T extends object ? T extends Array<infer U> ? Array<LiteralToPrimitiveDeep<U>> : { [K in keyof OmitIndexSignature<T>]: LiteralToPrimitiveDeep<T[K]>; } : LiteralToPrimitive<T>;
Like
LiteralToPrimitive
except it converts literal types inside an object or array deeply.For example, given a constant object, it returns a new object type with the same keys but with all the values converted to primitives.
Example 1
import type {LiteralToPrimitiveDeep, TsConfigJson} from 'type-fest';import tsconfig from 'path/to/tsconfig.json';function doSomethingWithTSConfig(config: LiteralToPrimitiveDeep<TsConfigJson>) { ... }// No casting is needed to pass the type checkdoSomethingWithTSConfig(tsconfig);// If LiteralToPrimitiveDeep is not used, you need to cast the imported data like this:doSomethingWithTSConfig(tsconfig as TsConfigJson);Type Object
See Also
LiteralToPrimitive
Use-case: Deal with data that is imported from a JSON file.
type LiteralUnion
type LiteralUnion<LiteralType, BaseType extends Primitive> = | LiteralType | (BaseType & Record<never, never>);
Allows creating a union type by combining primitive types and literal types without sacrificing auto-completion in IDEs for the literal type part of the union.
Currently, when a union type of a primitive type is combined with literal types, TypeScript loses all information about the combined literals. Thus, when such type is used in an IDE with autocompletion, no suggestions are made for the declared literals.
This type is a workaround for [Microsoft/TypeScript#29729](https://github.com/Microsoft/TypeScript/issues/29729). It will be removed as soon as it's not needed anymore.
Example 1
import type {LiteralUnion} from 'type-fest';// Beforetype Pet = 'dog' | 'cat' | string;const pet: Pet = '';// Start typing in your TypeScript-enabled IDE.// You **will not** get auto-completion for `dog` and `cat` literals.// Aftertype Pet2 = LiteralUnion<'dog' | 'cat', string>;const pet: Pet2 = '';// You **will** get auto-completion for `dog` and `cat` literals.Type
type Merge
type Merge<Destination, Source> = Simplify< SimpleMerge<PickIndexSignature<Destination>, PickIndexSignature<Source>> & SimpleMerge<OmitIndexSignature<Destination>, OmitIndexSignature<Source>>>;
Merge two types into a new type. Keys of the second type overrides keys of the first type.
Example 1
import type {Merge} from 'type-fest';interface Foo {[x: string]: unknown;[x: number]: unknown;foo: string;bar: symbol;}type Bar = {[x: number]: number;[x: symbol]: unknown;bar: Date;baz: boolean;};export type FooBar = Merge<Foo, Bar>;// => {// [x: string]: unknown;// [x: number]: number;// [x: symbol]: unknown;// foo: string;// bar: Date;// baz: boolean;// }Object
type MergeDeep
type MergeDeep< Destination, Source, Options extends MergeDeepOptions = {}> = MergeDeepWithDefaultOptions< SimplifyDeepExcludeArray<Destination>, SimplifyDeepExcludeArray<Source>, Options>;
Merge two objects or two arrays/tuples recursively into a new type.
- Properties that only exist in one object are copied into the new object. - Properties that exist in both objects are merged if possible or replaced by the one of the source if not. - Top-level arrays and tuples are always spread. - By default, inner arrays and tuples are replaced. See option to change this behaviour. - By default, individual array/tuple elements are not affected. See option to change this behaviour.
Example 1
import type {MergeDeep} from 'type-fest';type Foo = {life: number;items: string[];a: {b: string; c: boolean; d: number[]};};interface Bar {name: string;items: number[];a: {b: number; d: boolean[]};}type FooBar = MergeDeep<Foo, Bar>;// {// life: number;// name: string;// items: number[];// a: {b: number; c: boolean; d: boolean[]};// }type FooBar = MergeDeep<Foo, Bar, {arrayMergeMode: 'spread'}>;// {// life: number;// name: string;// items: (string | number)[];// a: {b: number; c: boolean; d: (number | boolean)[]};// }Example 2
import type {MergeDeep} from 'type-fest';// Merge two arraystype ArrayMerge = MergeDeep<string[], number[]>; // => (string | number)[]// Merge two tuplestype TupleMerge = MergeDeep<[1, 2, 3], ['a', 'b']>; // => (1 | 2 | 3 | 'a' | 'b')[]// Merge an array into a tupletype TupleArrayMerge = MergeDeep<[1, 2, 3], string[]>; // => (string | 1 | 2 | 3)[]// Merge a tuple into an arraytype ArrayTupleMerge = MergeDeep<number[], ['a', 'b']>; // => (number | 'b' | 'a')[]Example 3
import type {MergeDeep, MergeDeepOptions} from 'type-fest';type Foo = {foo: 'foo'; fooBar: string[]};type Bar = {bar: 'bar'; fooBar: number[]};type FooBar = MergeDeep<Foo, Bar>;// { foo: "foo"; bar: "bar"; fooBar: number[]}type FooBarSpread = MergeDeep<Foo, Bar, {arrayMergeMode: 'spread'}>;// { foo: "foo"; bar: "bar"; fooBar: (string | number)[]}type FooBarArray = MergeDeep<Foo[], Bar[]>;// (Foo | Bar)[]type FooBarArrayDeep = MergeDeep<Foo[], Bar[], {recurseIntoArrays: true}>;// FooBar[]type FooBarArraySpreadDeep = MergeDeep<Foo[], Bar[], {recurseIntoArrays: true; arrayMergeMode: 'spread'}>;// FooBarSpread[]type FooBarTupleDeep = MergeDeep<[Foo, true, 42], [Bar, 'life'], {recurseIntoArrays: true}>;// [FooBar, 'life', 42]type FooBarTupleWithArrayDeep = MergeDeep<[Foo[], true], [Bar[], 'life', 42], {recurseIntoArrays: true}>;// [FooBar[], 'life', 42]Example 4
import type {MergeDeep, MergeDeepOptions} from 'type-fest';function mergeDeep<Destination, Source, Options extends MergeDeepOptions = {}>(destination: Destination,source: Source,options?: Options,): MergeDeep<Destination, Source, Options> {// Make your implementation ...}This type is marked as experimental because it depends on ConditionalSimplifyDeep which itself is experimental.
See Also
Array Object Utilities
Modifiers
@experimental
type MergeDeepOptions
type MergeDeepOptions = { /** Merge mode for array and tuple.
When we walk through the properties of the objects and the same key is found and both are array or tuple, a merge mode must be chosen: - `replace`: Replaces the destination value by the source value. This is the default mode. - `spread`: Spreads the destination and the source values.
See {@link MergeDeep} for usages and examples.
Note: Top-level arrays and tuples are always spread.
@default 'replace' */ arrayMergeMode?: ArrayMergeMode;
/** Whether to affect the individual elements of arrays and tuples.
If this option is set to `true` the following rules are applied: - If the source does not contain the key, the value of the destination is returned. - If the source contains the key and the destination does not contain the key, the value of the source is returned. - If both contain the key, try to merge according to the chosen {@link MergeDeepOptions.arrayMergeMode arrayMergeMode} or return the source if unable to merge.
@default false */ recurseIntoArrays?: boolean;};
MergeDeep options.
See Also
type MergeExclusive
type MergeExclusive<FirstType, SecondType> = FirstType | SecondType extends object ? | (Without<FirstType, SecondType> & SecondType) | (Without<SecondType, FirstType> & FirstType) : FirstType | SecondType;
Create a type that has mutually exclusive keys.
This type was inspired by [this comment](https://github.com/Microsoft/TypeScript/issues/14094#issuecomment-373782604).
This type works with a helper type, called
Without
.Without<FirstType, SecondType>
produces a type that has only keys fromFirstType
which are not present onSecondType
and sets the value type for these keys tonever
. This helper type is then used inMergeExclusive
to remove keys from eitherFirstType
orSecondType
.Example 1
import type {MergeExclusive} from 'type-fest';interface ExclusiveVariation1 {exclusive1: boolean;}interface ExclusiveVariation2 {exclusive2: string;}type ExclusiveOptions = MergeExclusive<ExclusiveVariation1, ExclusiveVariation2>;let exclusiveOptions: ExclusiveOptions;exclusiveOptions = {exclusive1: true};//=> WorksexclusiveOptions = {exclusive2: 'hi'};//=> WorksexclusiveOptions = {exclusive1: true, exclusive2: 'hi'};//=> ErrorObject
type MultidimensionalArray
type MultidimensionalArray< Element, Dimensions extends number> = number extends Dimensions ? Recursive<Element> : IsEqual<Dimensions, 0> extends true ? Element : Array<MultidimensionalArray<Element, Subtract<Dimensions, 1>>>;
Creates a type that represents a multidimensional array of the given type and dimension.
Use-cases: - Return a n-dimensional array from functions. - Declare a n-dimensional array by defining its dimensions rather than declaring
[]
repetitively. - Infer the dimensions of a n-dimensional array automatically from function arguments. - Avoid the need to know in advance the dimensions of a n-dimensional array allowing them to be dynamic.Example 1
import type {MultidimensionalArray} from 'type-fest';function emptyMatrix<T extends number>(dimensions: T): MultidimensionalArray<unknown, T> {const matrix: unknown[] = [];let subMatrix = matrix;for (let dimension = 1; dimension < dimensions; ++dimension) {console.log(`Initializing dimension #${dimension}`);subMatrix[0] = [];subMatrix = subMatrix[0] as unknown[];}return matrix as MultidimensionalArray<unknown, T>;}const matrix = emptyMatrix(3);matrix[0][0][0] = 42;Array
type MultidimensionalReadonlyArray
type MultidimensionalReadonlyArray< Element, Dimensions extends number> = number extends Dimensions ? Recursive<Element> : IsEqual<Dimensions, 0> extends true ? Element : ReadonlyArray<MultidimensionalReadonlyArray<Element, Subtract<Dimensions, 1>>>;
Creates a type that represents a multidimensional readonly array that of the given type and dimension.
Use-cases: - Return a n-dimensional array from functions. - Declare a n-dimensional array by defining its dimensions rather than declaring
[]
repetitively. - Infer the dimensions of a n-dimensional array automatically from function arguments. - Avoid the need to know in advance the dimensions of a n-dimensional array allowing them to be dynamic.Example 1
import type {MultidimensionalReadonlyArray} from 'type-fest';function emptyMatrix<T extends number>(dimensions: T): MultidimensionalReadonlyArray<unknown, T> {const matrix: unknown[] = [];let subMatrix = matrix;for (let dimension = 1; dimension < dimensions; ++dimension) {console.log(`Initializing dimension #${dimension}`);subMatrix[0] = [];if (dimension < dimensions - 1) {subMatrix = subMatrix[0] as unknown[];} else {subMatrix[0] = 42;}}return matrix as MultidimensionalReadonlyArray<unknown, T>;}const matrix = emptyMatrix(3);const answer = matrix[0][0][0]; // 42Array
type Negative
type Negative<T extends Numeric> = T extends Zero ? never : `${T}` extends `-${string}` ? T : never;
A negative
number
/bigint
(-∞ < x < 0
)Use-case: Validating and documenting parameters.
See Also
NegativeInteger
NonNegative
Numeric
type NegativeFloat
type NegativeFloat<T extends number> = Negative<Float<T>>;
A negative (
-∞ < x < 0
)number
that is not an integer. Equivalent toNegative<Float<T>>
.Use-case: Validating and documenting parameters.
See Also
Negative
Float
Numeric
type NegativeInfinity
type NegativeInfinity = -1e999;
Matches the hidden
-Infinity
type.Please upvote [this issue](https://github.com/microsoft/TypeScript/issues/32277) if you want to have this type as a built-in in TypeScript.
See Also
PositiveInfinity
Numeric
type NegativeInteger
type NegativeInteger<T extends number> = Negative<Integer<T>>;
A negative (
-∞ < x < 0
)number
that is an integer. Equivalent toNegative<Integer<T>>
.You can't pass a
bigint
as they are already guaranteed to be integers, instead useNegative<T>
.Use-case: Validating and documenting parameters.
See Also
Negative
Integer
Numeric
type NonEmptyObject
type NonEmptyObject<T extends object> = HasRequiredKeys<T> extends true ? T : RequireAtLeastOne<T, keyof T>;
Represents an object with at least 1 non-optional key.
This is useful when you need an object where all keys are optional, but there must be at least 1 key.
Example 1
import type {NonEmptyObject} from 'type-fest';type User = {name: string;surname: string;id: number;};type UpdateRequest<Entity extends object> = NonEmptyObject<Partial<Entity>>;const update1: UpdateRequest<User> = {name: 'Alice',surname: 'Acme',};// At least 1 key is required, therefore this will report a 2322 error:// Type '{}' is not assignable to type 'UpdateRequest<User>'const update2: UpdateRequest<User> = {};See Also
Use
IsEmptyObject
to check whether an object is empty.Object
type NonEmptyTuple
type NonEmptyTuple<T = unknown> = readonly [T, ...T[]];
Matches any non-empty tuple.
Example 1
import type {NonEmptyTuple} from 'type-fest';const sum = (...numbers: NonEmptyTuple<number>) => numbers.reduce((total, value) => total + value, 0);sum(1, 2, 3);//=> 6sum();//=> Error: Expected at least 1 arguments, but got 0.See Also
RequireAtLeastOne for objects
Array
type NonNegative
type NonNegative<T extends Numeric> = T extends Zero ? T : Negative<T> extends never ? T : never;
A non-negative
number
/bigint
(0 <= x < ∞
).Use-case: Validating and documenting parameters.
Example 1
import type {NonNegative} from 'type-fest';declare function setLength<T extends number>(length: NonNegative<T>): void;Numeric
See Also
NonNegativeInteger
Negative
type NonNegativeInteger
type NonNegativeInteger<T extends number> = NonNegative<Integer<T>>;
A non-negative (
0 <= x < ∞
)number
that is an integer. Equivalent toNonNegative<Integer<T>>
.You can't pass a
bigint
as they are already guaranteed to be integers, instead useNonNegative<T>
.Use-case: Validating and documenting parameters.
Example 1
import type {NonNegativeInteger} from 'type-fest';declare function setLength<T extends number>(length: NonNegativeInteger<T>): void;Numeric
See Also
NonNegative
Integer
type ObservableLike
type ObservableLike<ValueType = unknown> = { subscribe(observer?: Partial<Observer<ValueType>>): Unsubscribable; [Symbol.observable](): ObservableLike<ValueType>;};
Matches a value that is like an [Observable](https://github.com/tc39/proposal-observable).
Remarks
The TC39 Observable proposal defines 2 forms of
subscribe()
: 1. Three callback arguments:subscribe(observer: OnNext<ValueType>, onError?: OnError, onComplete?: OnComplete): Unsubscribable;
2. A singleobserver
argument: (as defined below)But
Observable
implementations have evolved to preferring case 2 and some implementations choose not to implement case 1. Therefore, anObservableLike
cannot be trusted to implement the first case. (xstream and hand built observerables often do not implement case 1)See Also
https://github.com/tc39/proposal-observable#observable
https://github.com/tc39/proposal-observable/blob/master/src/Observable.js#L246-L259
https://benlesh.com/posts/learning-observable-by-building-observable/
Observable
type Observer
type Observer<ValueType> = { next: OnNext<ValueType>; error: OnError; complete: OnComplete;};
Observable
type OmitDeep
type OmitDeep<T, PathUnion extends LiteralUnion<Paths<T>, string>> = SimplifyDeep< SharedUnionFieldsDeep< { [P in PathUnion]: OmitDeepWithOnePath<T, P> }[PathUnion] >, UnknownArray>;
Omit properties from a deeply-nested object.
It supports recursing into arrays.
It supports removing specific items from an array, replacing each removed item with unknown at the specified index.
Use-case: Remove unneeded parts of complex objects.
Use [
Omit
](https://www.typescriptlang.org/docs/handbook/utility-types.html#omittype-keys) if you only need one level deep.Example 1
import type {OmitDeep} from 'type-fest';type Info = {userInfo: {name: string;uselessInfo: {foo: string;};};};type UsefulInfo = OmitDeep<Info, 'userInfo.uselessInfo'>;// type UsefulInfo = {// userInfo: {// name: string;// };// };// Supports removing multiple pathstype Info1 = {userInfo: {name: string;uselessField: string;uselessInfo: {foo: string;};};};type UsefulInfo1 = OmitDeep<Info1, 'userInfo.uselessInfo' | 'userInfo.uselessField'>;// type UsefulInfo1 = {// userInfo: {// name: string;// };// };// Supports arraytype A = OmitDeep<[1, 'foo', 2], 1>;// type A = [1, unknown, 2];// Supports recursing into arraytype Info1 = {address: [{street: string},{street2: string,foo: string};];}type AddressInfo = OmitDeep<Info1, 'address.1.foo'>;// type AddressInfo = {// address: [// {// street: string;// },// {// street2: string;// };// ];// };Object Array
type OmitIndexSignature
type OmitIndexSignature<ObjectType> = { [KeyType in keyof ObjectType as {} extends Record<KeyType, unknown> ? never : KeyType]: ObjectType[KeyType];};
Omit any index signatures from the given object type, leaving only explicitly defined properties.
This is the counterpart of
PickIndexSignature
.Use-cases: - Remove overly permissive signatures from third-party types.
This type was taken from this [StackOverflow answer](https://stackoverflow.com/a/68261113/420747).
It relies on the fact that an empty object (
{}
) is assignable to an object with just an index signature, likeRecord<string, unknown>
, but not to an object with explicitly defined keys, likeRecord<'foo' | 'bar', unknown>
.(The actual value type,
unknown
, is irrelevant and could be any type. Only the key type matters.)const indexed: Record<string, unknown> = {}; // Allowedconst keyed: Record<'foo', unknown> = {}; // Error// => TS2739: Type '{}' is missing the following properties from type 'Record<"foo" | "bar", unknown>': foo, barInstead of causing a type error like the above, you can also use a [conditional type](https://www.typescriptlang.org/docs/handbook/2/conditional-types.html) to test whether a type is assignable to another:
type Indexed = {} extends Record<string, unknown>? '✅ `{}` is assignable to `Record<string, unknown>`': '❌ `{}` is NOT assignable to `Record<string, unknown>`';// => '✅ `{}` is assignable to `Record<string, unknown>`'type Keyed = {} extends Record<'foo' | 'bar', unknown>? "✅ `{}` is assignable to `Record<'foo' | 'bar', unknown>`": "❌ `{}` is NOT assignable to `Record<'foo' | 'bar', unknown>`";// => "❌ `{}` is NOT assignable to `Record<'foo' | 'bar', unknown>`"Using a [mapped type](https://www.typescriptlang.org/docs/handbook/2/mapped-types.html#further-exploration), you can then check for each
KeyType
ofObjectType
...import type {OmitIndexSignature} from 'type-fest';type OmitIndexSignature<ObjectType> = {[KeyType in keyof ObjectType // Map each key of `ObjectType`...]: ObjectType[KeyType]; // ...to its original value, i.e. `OmitIndexSignature<Foo> == Foo`.};...whether an empty object (
{}
) would be assignable to an object with thatKeyType
(Record<KeyType, unknown>
)...import type {OmitIndexSignature} from 'type-fest';type OmitIndexSignature<ObjectType> = {[KeyType in keyof ObjectType// Is `{}` assignable to `Record<KeyType, unknown>`?as {} extends Record<KeyType, unknown>? ... // ✅ `{}` is assignable to `Record<KeyType, unknown>`: ... // ❌ `{}` is NOT assignable to `Record<KeyType, unknown>`]: ObjectType[KeyType];};If
{}
is assignable, it means thatKeyType
is an index signature and we want to remove it. If it is not assignable,KeyType
is a "real" key and we want to keep it.import type {OmitIndexSignature} from 'type-fest';type OmitIndexSignature<ObjectType> = {[KeyType in keyof ObjectTypeas {} extends Record<KeyType, unknown>? never // => Remove this `KeyType`.: KeyType // => Keep this `KeyType` as it is.]: ObjectType[KeyType];};Example 1
import type {OmitIndexSignature} from 'type-fest';interface Example {// These index signatures will be removed.[x: string]: any[x: number]: any[x: symbol]: any[x: `head-${string}`]: string[x: `${string}-tail`]: string[x: `head-${string}-tail`]: string[x: `${bigint}`]: string[x: `embedded-${number}`]: string// These explicitly defined keys will remain.foo: 'bar';qux?: 'baz';}type ExampleWithoutIndexSignatures = OmitIndexSignature<Example>;// => { foo: 'bar'; qux?: 'baz' | undefined; }See Also
PickIndexSignature Object
type OnComplete
type OnComplete = () => void;
Observable
type OnError
type OnError = (error: unknown) => void;
Observable
type OnNext
type OnNext<ValueType> = (value: ValueType) => void;
Observable
type Opaque
type Opaque<Type, Token = unknown> = Type & TagContainer<Token>;
Note: The
Opaque
type is deprecated in favor ofTagged
.Attach a "tag" to an arbitrary type. This allows you to create distinct types, that aren't assignable to one another, for runtime values that would otherwise have the same type. (See examples.)
The generic type parameters can be anything.
Note that
Opaque
is somewhat of a misnomer here, in that, unlike [some alternative implementations](https://github.com/microsoft/TypeScript/issues/4895#issuecomment-425132582), the original, untagged type is not actually hidden. (E.g., functions that accept the untagged type can still be called with the "opaque" version -- but not vice-versa.)Also note that this implementation is limited to a single tag. If you want to allow multiple tags, use
Tagged
instead.[Read more about tagged types.](https://medium.com/@KevinBGreene/surviving-the-typescript-ecosystem-branding-and-type-tagging-6cf6e516523d)
There have been several discussions about adding similar features to TypeScript. Unfortunately, nothing has (yet) moved forward: - [Microsoft/TypeScript#202](https://github.com/microsoft/TypeScript/issues/202) - [Microsoft/TypeScript#15408](https://github.com/Microsoft/TypeScript/issues/15408) - [Microsoft/TypeScript#15807](https://github.com/Microsoft/TypeScript/issues/15807)
Example 1
import type {Opaque} from 'type-fest';type AccountNumber = Opaque<number, 'AccountNumber'>;type AccountBalance = Opaque<number, 'AccountBalance'>;// The `Token` parameter allows the compiler to differentiate between types, whereas "unknown" will not. For example, consider the following structures:type ThingOne = Opaque<string>;type ThingTwo = Opaque<string>;// To the compiler, these types are allowed to be cast to each other as they have the same underlying type. They are both `string & { __opaque__: unknown }`.// To avoid this behaviour, you would instead pass the "Token" parameter, like so.type NewThingOne = Opaque<string, 'ThingOne'>;type NewThingTwo = Opaque<string, 'ThingTwo'>;// Now they're completely separate types, so the following will fail to compile.function createNewThingOne (): NewThingOne {// As you can see, casting from a string is still allowed. However, you may not cast NewThingOne to NewThingTwo, and vice versa.return 'new thing one' as NewThingOne;}// This will fail to compile, as they are fundamentally different types.const thingTwo = createNewThingOne() as NewThingTwo;// Here's another example of opaque typing.function createAccountNumber(): AccountNumber {return 2 as AccountNumber;}function getMoneyForAccount(accountNumber: AccountNumber): AccountBalance {return 4 as AccountBalance;}// This will compile successfully.getMoneyForAccount(createAccountNumber());// But this won't, because it has to be explicitly passed as an `AccountNumber` type.getMoneyForAccount(2);// You can use opaque values like they aren't opaque too.const accountNumber = createAccountNumber();// This will compile successfully.const newAccountNumber = accountNumber + 2;// As a side note, you can (and should) use recursive types for your opaque types to make them stronger and hopefully easier to type.type Person = {id: Opaque<number, Person>;name: string;};Type
Deprecated
Use Tagged instead
type OptionalKeysOf
type OptionalKeysOf<BaseType extends object> = Exclude< { [Key in keyof BaseType]: BaseType extends Record<Key, BaseType[Key]> ? never : Key; }[keyof BaseType], undefined>;
Extract all optional keys from the given type.
This is useful when you want to create a new type that contains different type values for the optional keys only.
Example 1
import type {OptionalKeysOf, Except} from 'type-fest';interface User {name: string;surname: string;luckyNumber?: number;}const REMOVE_FIELD = Symbol('remove field symbol');type UpdateOperation<Entity extends object> = Except<Partial<Entity>, OptionalKeysOf<Entity>> & {[Key in OptionalKeysOf<Entity>]?: Entity[Key] | typeof REMOVE_FIELD;};const update1: UpdateOperation<User> = {name: 'Alice'};const update2: UpdateOperation<User> = {name: 'Bob',luckyNumber: REMOVE_FIELD};Utilities
type Or
type Or<A extends boolean, B extends boolean> = [A, B][number] extends false ? false : true extends [IsEqual<A, true>, IsEqual<B, true>][number] ? true : never;
Returns a boolean for whether either of two given types are true.
Use-case: Constructing complex conditional types where multiple conditions must be satisfied.
Example 1
import type {Or} from 'type-fest';Or<true, false>;//=> trueOr<false, false>;//=> falseSee Also
type OverrideProperties
type OverrideProperties< TOriginal, // This first bit where we use `Partial` is to enable autocomplete // and the second bit with the mapped type is what enforces that we don't try // to override properties that doesn't exist in the original type. TOverride extends Partial<Record<keyof TOriginal, unknown>> & { [Key in keyof TOverride]: Key extends keyof TOriginal ? TOverride[Key] : never; }> = Merge<TOriginal, TOverride>;
Override existing properties of the given type. Similar to
Merge
, but enforces that the original type has the properties you want to override.This is useful when you want to override existing properties with a different type and make sure that these properties really exist in the original.
Example 1
type Foo = {a: stringb: string}type Bar = OverrideProperties<Foo, {b: number}>//=> {a: string, b: number}type Baz = OverrideProperties<Foo, {c: number}>// Error, type '{ c: number; }' does not satisfy the constraint '{ c: never; }'type Fizz = OverrideProperties<Foo, {b: number; c: number}>// Error, type '{ b: number; c: number; }' does not satisfy the constraint '{ b: number; c: never; }'Object
type PackageJson
type PackageJson = JsonObject & PackageJson.NodeJsStandard & PackageJson.PackageJsonStandard & PackageJson.NonStandardEntryPoints & PackageJson.TypeScriptConfiguration & PackageJson.YarnConfiguration & PackageJson.JSPMConfiguration;
Type for [npm's
package.json
file](https://docs.npmjs.com/creating-a-package-json-file). Also includes types for fields used by other popular projects, like TypeScript and Yarn.File
type PartialDeep
type PartialDeep<T, Options extends PartialDeepOptions = {}> = T extends | BuiltIns | ((...arguments_: any[]) => unknown) | (new (...arguments_: any[]) => unknown) ? T : T extends Map<infer KeyType, infer ValueType> ? PartialMapDeep<KeyType, ValueType, Options> : T extends Set<infer ItemType> ? PartialSetDeep<ItemType, Options> : T extends ReadonlyMap<infer KeyType, infer ValueType> ? PartialReadonlyMapDeep<KeyType, ValueType, Options> : T extends ReadonlySet<infer ItemType> ? PartialReadonlySetDeep<ItemType, Options> : T extends object ? T extends ReadonlyArray<infer ItemType> // Test for arrays/tuples, per https://github.com/microsoft/TypeScript/issues/35156 ? Options['recurseIntoArrays'] extends true ? ItemType[] extends T // Test for arrays (non-tuples) specifically ? readonly ItemType[] extends T // Differentiate readonly and mutable arrays ? ReadonlyArray< PartialDeep< Options['allowUndefinedInNonTupleArrays'] extends false ? ItemType : ItemType | undefined, Options > > : Array< PartialDeep< Options['allowUndefinedInNonTupleArrays'] extends false ? ItemType : ItemType | undefined, Options > > : PartialObjectDeep<T, Options> // Tuples behave properly : T // If they don't opt into array testing, just use the original type : PartialObjectDeep<T, Options> : unknown;
Create a type from another type with all keys and nested keys set to optional.
Use-cases: - Merging a default settings/config object with another object, the second object would be a deep partial of the default object. - Mocking and testing complex entities, where populating an entire object with its keys would be redundant in terms of the mock or test.
Example 1
import type {PartialDeep} from 'type-fest';const settings: Settings = {textEditor: {fontSize: 14,fontColor: '#000000',fontWeight: 400},autocomplete: false,autosave: true};const applySavedSettings = (savedSettings: PartialDeep<Settings>) => {return {...settings, ...savedSettings};}settings = applySavedSettings({textEditor: {fontWeight: 500}});By default, this does not affect elements in array and tuple types. You can change this by passing
{recurseIntoArrays: true}
as the second type argument:import type {PartialDeep} from 'type-fest';type Settings = {languages: string[];}const partialSettings: PartialDeep<Settings, {recurseIntoArrays: true}> = {languages: [undefined]};See Also
Object Array Set Map
type PartialDeepOptions
type PartialDeepOptions = { /** Whether to affect the individual elements of arrays and tuples.
@default false */ readonly recurseIntoArrays?: boolean;
/** Allows `undefined` values in non-tuple arrays.
- When set to `true`, elements of non-tuple arrays can be `undefined`. - When set to `false`, only explicitly defined elements are allowed in non-tuple arrays, ensuring stricter type checking.
@default true
@example You can prevent `undefined` values in non-tuple arrays by passing `{recurseIntoArrays: true; allowUndefinedInNonTupleArrays: false}` as the second type argument:
``` import type {PartialDeep} from 'type-fest';
type Settings = { languages: string[]; };
declare const partialSettings: PartialDeep<Settings, {recurseIntoArrays: true; allowUndefinedInNonTupleArrays: false}>;
partialSettings.languages = [undefined]; // Error partialSettings.languages = []; // Ok ``` */ readonly allowUndefinedInNonTupleArrays?: boolean;};
See Also
type PartialOnUndefinedDeep
type PartialOnUndefinedDeep< T, Options extends PartialOnUndefinedDeepOptions = {}> = T extends Record<any, any> | undefined ? { [KeyType in keyof T as undefined extends T[KeyType] ? IfUnknown<T[KeyType], never, KeyType> : never]?: PartialOnUndefinedDeepValue<T[KeyType], Options>; } extends infer U // Make a partial type with all value types accepting undefined (and set them optional) ? Merge< { [KeyType in keyof T as KeyType extends LiteralKeyOf<U> ? never : KeyType]: PartialOnUndefinedDeepValue<T[KeyType], Options>; }, U > // Join all remaining keys not treated in U : never // Should not happen : T;
Create a deep version of another type where all keys accepting
undefined
type are set to optional.This utility type is recursive, transforming at any level deep. By default, it does not affect arrays and tuples items unless you explicitly pass
{recurseIntoArrays: true}
as the second type argument.Use-cases: - Make all properties of a type that can be undefined optional to not have to specify keys with undefined value.
Example 1
import type {PartialOnUndefinedDeep} from 'type-fest';interface Settings {optionA: string;optionB: number | undefined;subOption: {subOptionA: boolean;subOptionB: boolean | undefined;}};const testSettings: PartialOnUndefinedDeep<Settings> = {optionA: 'foo',// 👉 optionB is now optional and can be omittedsubOption: {subOptionA: true,// 👉 subOptionB is now optional as well and can be omitted},};Object
type PartialOnUndefinedDeepOptions
type PartialOnUndefinedDeepOptions = { /** Whether to affect the individual elements of arrays and tuples.
@default false */ readonly recurseIntoArrays?: boolean;};
See Also
PartialOnUndefinedDeep
type PascalCase
type PascalCase< Value, Options extends CamelCaseOptions = { preserveConsecutiveUppercase: true }> = CamelCase<Value, Options> extends string ? Capitalize<CamelCase<Value, Options>> : CamelCase<Value, Options>;
Converts a string literal to pascal-case.
Example 1
import type {PascalCase} from 'type-fest';// Simpleconst someVariable: PascalCase<'foo-bar'> = 'FooBar';// Advancedtype PascalCaseProps<T> = {[K in keyof T as PascalCase<K>]: T[K]};interface RawOptions {'dry-run': boolean;'full_family_name': string;foo: number;}const dbResult: CamelCasedProperties<ModelProps> = {DryRun: true,FullFamilyName: 'bar.js',Foo: 123};Change case Template literal
type PascalCasedProperties
type PascalCasedProperties< Value, Options extends CamelCaseOptions = { preserveConsecutiveUppercase: true }> = Value extends Function ? Value : Value extends Array<infer U> ? Value : { [K in keyof Value as PascalCase<K, Options>]: Value[K] };
Convert object properties to pascal case but not recursively.
This can be useful when, for example, converting some API types from a different style.
Example 1
import type {PascalCasedProperties} from 'type-fest';interface User {userId: number;userName: string;}const result: PascalCasedProperties<User> = {UserId: 1,UserName: 'Tom',};Change case Template literal Object
See Also
PascalCase
PascalCasedPropertiesDeep
type PascalCasedPropertiesDeep
type PascalCasedPropertiesDeep< Value, Options extends CamelCaseOptions = { preserveConsecutiveUppercase: true }> = Value extends Function | Date | RegExp ? Value : Value extends Array<infer U> ? Array<PascalCasedPropertiesDeep<U, Options>> : Value extends Set<infer U> ? Set<PascalCasedPropertiesDeep<U, Options>> : { [K in keyof Value as PascalCase<K, Options>]: PascalCasedPropertiesDeep< Value[K], Options >; };
Convert object properties to pascal case recursively.
This can be useful when, for example, converting some API types from a different style.
Example 1
import type {PascalCasedPropertiesDeep} from 'type-fest';interface User {userId: number;userName: string;}interface UserWithFriends {userInfo: User;userFriends: User[];}const result: PascalCasedPropertiesDeep<UserWithFriends> = {UserInfo: {UserId: 1,UserName: 'Tom',},UserFriends: [{UserId: 2,UserName: 'Jerry',},{UserId: 3,UserName: 'Spike',},],};Change case Template literal Object
See Also
PascalCase
PascalCasedProperties
type Paths
type Paths<T, Options extends PathsOptions = {}> = _Paths< T, { // Set default maxRecursionDepth to 10 maxRecursionDepth: Options['maxRecursionDepth'] extends number ? Options['maxRecursionDepth'] : DefaultPathsOptions['maxRecursionDepth']; // Set default bracketNotation to false bracketNotation: Options['bracketNotation'] extends boolean ? Options['bracketNotation'] : DefaultPathsOptions['bracketNotation']; }>;
Generate a union of all possible paths to properties in the given object.
It also works with arrays.
Use-case: You want a type-safe way to access deeply nested properties in an object.
Example 1
import type {Paths} from 'type-fest';type Project = {filename: string;listA: string[];listB: [{filename: string}];folder: {subfolder: {filename: string;};};};type ProjectPaths = Paths<Project>;//=> 'filename' | 'listA' | 'listB' | 'folder' | `listA.${number}` | 'listB.0' | 'listB.0.filename' | 'folder.subfolder' | 'folder.subfolder.filename'declare function open<Path extends ProjectPaths>(path: Path): void;open('filename'); // Passopen('folder.subfolder'); // Passopen('folder.subfolder.filename'); // Passopen('foo'); // TypeError// Also works with arraysopen('listA.1'); // Passopen('listB.0'); // Passopen('listB.1'); // TypeError. Because listB only has one element.Object Array
type PickDeep
type PickDeep<T, PathUnion extends Paths<T>> = T extends NonRecursiveType ? never : T extends UnknownArray ? UnionToIntersection< { [P in PathUnion]: InternalPickDeep<T, P>; }[PathUnion] > : T extends object ? Simplify< UnionToIntersection< { [P in PathUnion]: InternalPickDeep<T, P>; }[PathUnion] > > : never;
Pick properties from a deeply-nested object.
It supports recursing into arrays.
Use-case: Distill complex objects down to the components you need to target.
Example 1
import type {PickDeep, PartialDeep} from 'type-fest';type Configuration = {userConfig: {name: string;age: number;address: [{city1: string;street1: string;},{city2: string;street2: string;}]};otherConfig: any;};type NameConfig = PickDeep<Configuration, 'userConfig.name'>;// type NameConfig = {// userConfig: {// name: string;// }// };// Supports optional propertiestype User = PickDeep<PartialDeep<Configuration>, 'userConfig.name' | 'userConfig.age'>;// type User = {// userConfig?: {// name?: string;// age?: number;// };// };// Supports arraytype AddressConfig = PickDeep<Configuration, 'userConfig.address.0'>;// type AddressConfig = {// userConfig: {// address: [{// city1: string;// street1: string;// }];// };// }// Supports recurse into arraytype Street = PickDeep<Configuration, 'userConfig.address.1.street2'>;// type Street = {// userConfig: {// address: [// unknown,// {street2: string}// ];// };// }Object Array
type PickIndexSignature
type PickIndexSignature<ObjectType> = { [KeyType in keyof ObjectType as {} extends Record<KeyType, unknown> ? KeyType : never]: ObjectType[KeyType];};
Pick only index signatures from the given object type, leaving out all explicitly defined properties.
This is the counterpart of
OmitIndexSignature
.When you use a type that will iterate through an object that has indexed keys and explicitly defined keys you end up with a type where only the indexed keys are kept. This is because
keyof
of an indexed type always returnsstring | number | symbol
, because every key is possible in that object. With this type, you can save the indexed keys and reinject them later, like in the second example below.Example 1
import type {PickIndexSignature} from 'type-fest';declare const symbolKey: unique symbol;type Example = {// These index signatures will remain.[x: string]: unknown;[x: number]: unknown;[x: symbol]: unknown;[x: `head-${string}`]: string;[x: `${string}-tail`]: string;[x: `head-${string}-tail`]: string;[x: `${bigint}`]: string;[x: `embedded-${number}`]: string;// These explicitly defined keys will be removed.['snake-case-key']: string;[symbolKey]: string;foo: 'bar';qux?: 'baz';};type ExampleIndexSignature = PickIndexSignature<Example>;// {// [x: string]: unknown;// [x: number]: unknown;// [x: symbol]: unknown;// [x: `head-${string}`]: string;// [x: `${string}-tail`]: string;// [x: `head-${string}-tail`]: string;// [x: `${bigint}`]: string;// [x: `embedded-${number}`]: string;// }Example 2
import type {OmitIndexSignature, PickIndexSignature, Simplify} from 'type-fest';type Foo = {[x: string]: string;foo: string;bar: number;};// Imagine that you want a new type `Bar` that comes from `Foo`.// => {// [x: string]: string;// bar: number;// };type Bar = Omit<Foo, 'foo'>;// This is not working because `Omit` returns only indexed keys.// => {// [x: string]: string;// [x: number]: string;// }// One solution is to save the indexed signatures to new type.type FooIndexSignature = PickIndexSignature<Foo>;// => {// [x: string]: string;// }// Get a new type without index signatures.type FooWithoutIndexSignature = OmitIndexSignature<Foo>;// => {// foo: string;// bar: number;// }// At this point we can use Omit to get our new type.type BarWithoutIndexSignature = Omit<FooWithoutIndexSignature, 'foo'>;// => {// bar: number;// }// And finally we can merge back the indexed signatures.type BarWithIndexSignature = Simplify<BarWithoutIndexSignature & FooIndexSignature>;// => {// [x: string]: string;// bar: number;// }See Also
OmitIndexSignature Object
type PositiveInfinity
type PositiveInfinity = 1e999;
Matches the hidden
Infinity
type.Please upvote [this issue](https://github.com/microsoft/TypeScript/issues/32277) if you want to have this type as a built-in in TypeScript.
See Also
NegativeInfinity
Numeric
type Primitive
type Primitive = null | undefined | string | number | boolean | symbol | bigint;
Matches any [primitive value](https://developer.mozilla.org/en-US/docs/Glossary/Primitive).
Type
type Promisable
type Promisable<T> = T | PromiseLike<T>;
Create a type that represents either the value or the value wrapped in
PromiseLike
.Use-cases: - A function accepts a callback that may either return a value synchronously or may return a promised value. - This type could be the return type of
Promise#then()
,Promise#catch()
, andPromise#finally()
callbacks.Please upvote [this issue](https://github.com/microsoft/TypeScript/issues/31394) if you want to have this type as a built-in in TypeScript.
Example 1
import type {Promisable} from 'type-fest';async function logger(getLogEntry: () => Promisable<string>): Promise<void> {const entry = await getLogEntry();console.log(entry);}logger(() => 'foo');logger(() => Promise.resolve('bar'));Async
type ReadonlyDeep
type ReadonlyDeep<T> = T extends BuiltIns ? T : T extends new (...arguments_: any[]) => unknown ? T // Skip class constructors : T extends (...arguments_: any[]) => unknown ? {} extends ReadonlyObjectDeep<T> ? T : HasMultipleCallSignatures<T> extends true ? T : ((...arguments_: Parameters<T>) => ReturnType<T>) & ReadonlyObjectDeep<T> : T extends Readonly<ReadonlyMap<infer KeyType, infer ValueType>> ? ReadonlyMapDeep<KeyType, ValueType> : T extends Readonly<ReadonlySet<infer ItemType>> ? ReadonlySetDeep<ItemType> : // Identify tuples to avoid converting them to arrays inadvertently; special case `readonly [...never[]]`, as it emerges undesirably from recursive invocations of ReadonlyDeep below. T extends readonly [] | readonly [...never[]] ? readonly [] : T extends readonly [infer U, ...infer V] ? readonly [ReadonlyDeep<U>, ...ReadonlyDeep<V>] : T extends readonly [...infer U, infer V] ? readonly [...ReadonlyDeep<U>, ReadonlyDeep<V>] : T extends ReadonlyArray<infer ItemType> ? ReadonlyArray<ReadonlyDeep<ItemType>> : T extends object ? ReadonlyObjectDeep<T> : unknown;
Convert
object
s,Map
s,Set
s, andArray
s and all of their keys/elements into immutable structures recursively.This is useful when a deeply nested structure needs to be exposed as completely immutable, for example, an imported JSON module or when receiving an API response that is passed around.
Please upvote [this issue](https://github.com/microsoft/TypeScript/issues/13923) if you want to have this type as a built-in in TypeScript.
Example 1
// data.json{"foo": ["bar"]}// main.tsimport type {ReadonlyDeep} from 'type-fest';import dataJson = require('./data.json');const data: ReadonlyDeep<typeof dataJson> = dataJson;export default data;// test.tsimport data from './main';data.foo.push('bar');//=> error TS2339: Property 'push' does not exist on type 'readonly string[]'Note that types containing overloaded functions are not made deeply readonly due to a [TypeScript limitation](https://github.com/microsoft/TypeScript/issues/29732).
Object Array Set Map
type ReadonlyKeysOf
type ReadonlyKeysOf<T> = NonNullable< { [P in keyof T]: IsEqual< { [Q in P]: T[P] }, { readonly [Q in P]: T[P] } > extends true ? P : never; }[keyof T]>;
Extract all readonly keys from the given type.
This is useful when you want to create a new type that contains readonly keys only.
Example 1
import type {ReadonlyKeysOf} from 'type-fest';interface User {name: string;surname: string;readonly id: number;}type UpdateResponse<Entity extends object> = Pick<Entity, ReadonlyKeysOf<Entity>>;const update1: UpdateResponse<User> = {id: 123,};Utilities
type ReadonlyTuple
type ReadonlyTuple<Element, Length extends number> = number extends Length ? // Because `Length extends number` and `number extends Length`, then `Length` is not a specific finite number. readonly Element[] // It's not fixed length. : BuildTupleHelper<Element, Length, []>;
Create a type that represents a read-only tuple of the given type and length.
Use-cases: - Declaring fixed-length tuples with a large number of items. - Creating a range union (for example,
0 | 1 | 2 | 3 | 4
from the keys of such a type) without having to resort to recursive types. - Creating a tuple of coordinates with a static length, for example, length of 3 for a 3D vector.Example 1
import {ReadonlyTuple} from 'type-fest';type FencingTeam = ReadonlyTuple<string, 3>;const guestFencingTeam: FencingTeam = ['Josh', 'Michael', 'Robert'];const homeFencingTeam: FencingTeam = ['George', 'John'];//=> error TS2322: Type string[] is not assignable to type 'FencingTeam'guestFencingTeam.push('Sam');//=> error TS2339: Property 'push' does not exist on type 'FencingTeam'Utilities
type Replace
type Replace< Input extends string, Search extends string, Replacement extends string, Options extends ReplaceOptions = {}> = Input extends `${infer Head}${Search}${infer Tail}` ? Options['all'] extends true ? `${Head}${Replacement}${Replace<Tail, Search, Replacement, Options>}` : `${Head}${Replacement}${Tail}` : Input;
Represents a string with some or all matches replaced by a replacement.
Use-case: -
snake-case-path
todotted.path.notation
- Changing date/time format:01-08-2042
→01/08/2042
- Manipulation of type properties, for example, removal of prefixesExample 1
import {Replace} from 'type-fest';declare function replace<Input extends string,Search extends string,Replacement extends string>(input: Input,search: Search,replacement: Replacement): Replace<Input, Search, Replacement>;declare function replaceAll<Input extends string,Search extends string,Replacement extends string>(input: Input,search: Search,replacement: Replacement): Replace<Input, Search, Replacement, {all: true}>;// The return type is the exact string literal, not just `string`.replace('hello ?', '?', '🦄');//=> 'hello 🦄'replace('hello ??', '?', '❓');//=> 'hello ❓?'replaceAll('10:42:00', ':', '-');//=> '10-42-00'replaceAll('__userName__', '__', '');//=> 'userName'replaceAll('My Cool Title', ' ', '');//=> 'MyCoolTitle'String Template literal
type RequireAllOrNone
type RequireAllOrNone< ObjectType, KeysType extends keyof ObjectType = keyof ObjectType> = (RequireAll<ObjectType, KeysType> | RequireNone<KeysType>) & Omit<ObjectType, KeysType>;
Create a type that requires all of the given keys or none of the given keys. The remaining keys are kept as is.
Use-cases: - Creating interfaces for components with mutually-inclusive keys.
The caveat with
RequireAllOrNone
is that TypeScript doesn't always know at compile time every key that will exist at runtime. ThereforeRequireAllOrNone
can't do anything to prevent extra keys it doesn't know about.Example 1
import type {RequireAllOrNone} from 'type-fest';type Responder = {text?: () => string;json?: () => string;secure: boolean;};const responder1: RequireAllOrNone<Responder, 'text' | 'json'> = {secure: true};const responder2: RequireAllOrNone<Responder, 'text' | 'json'> = {text: () => '{"message": "hi"}',json: () => '{"message": "ok"}',secure: true};Object
type RequireAtLeastOne
type RequireAtLeastOne< ObjectType, KeysType extends keyof ObjectType = keyof ObjectType> = { // For each `Key` in `KeysType` make a mapped type: [Key in KeysType]-?: Required<Pick<ObjectType, Key>> & // 1. Make `Key`'s type required // 2. Make all other keys in `KeysType` optional Partial<Pick<ObjectType, Exclude<KeysType, Key>>>;}[KeysType] & // 3. Add the remaining keys not in `KeysType` Except<ObjectType, KeysType>;
Create a type that requires at least one of the given keys. The remaining keys are kept as is.
Example 1
import type {RequireAtLeastOne} from 'type-fest';type Responder = {text?: () => string;json?: () => string;secure?: boolean;};const responder: RequireAtLeastOne<Responder, 'text' | 'json'> = {json: () => '{"message": "ok"}',secure: true};Object
type RequiredDeep
type RequiredDeep< T, E extends ExcludeUndefined<T> = ExcludeUndefined<T>> = E extends BuiltIns ? E : E extends Map<infer KeyType, infer ValueType> ? Map<RequiredDeep<KeyType>, RequiredDeep<ValueType>> : E extends Set<infer ItemType> ? Set<RequiredDeep<ItemType>> : E extends ReadonlyMap<infer KeyType, infer ValueType> ? ReadonlyMap<RequiredDeep<KeyType>, RequiredDeep<ValueType>> : E extends ReadonlySet<infer ItemType> ? ReadonlySet<RequiredDeep<ItemType>> : E extends WeakMap<infer KeyType, infer ValueType> ? WeakMap<RequiredDeep<KeyType>, RequiredDeep<ValueType>> : E extends WeakSet<infer ItemType> ? WeakSet<RequiredDeep<ItemType>> : E extends Promise<infer ValueType> ? Promise<RequiredDeep<ValueType>> : E extends (...arguments_: any[]) => unknown ? {} extends RequiredObjectDeep<E> ? E : HasMultipleCallSignatures<E> extends true ? E : ((...arguments_: Parameters<E>) => ReturnType<E>) & RequiredObjectDeep<E> : E extends object ? E extends Array<infer ItemType> // Test for arrays/tuples, per https://github.com/microsoft/TypeScript/issues/35156 ? ItemType[] extends E // Test for arrays (non-tuples) specifically ? Array<RequiredDeep<ItemType>> // Recreate relevant array type to prevent eager evaluation of circular reference : RequiredObjectDeep<E> // Tuples behave properly : RequiredObjectDeep<E> : unknown;
Create a type from another type with all keys and nested keys set to required.
Use-cases: - Creating optional configuration interfaces where the underlying implementation still requires all options to be fully specified. - Modeling the resulting type after a deep merge with a set of defaults.
Example 1
import type {RequiredDeep} from 'type-fest';type Settings = {textEditor?: {fontSize?: number | undefined;fontColor?: string | undefined;fontWeight?: number | undefined;}autocomplete?: boolean | undefined;autosave?: boolean | undefined;};type RequiredSettings = RequiredDeep<Settings>;// type RequiredSettings = {// textEditor: {// fontSize: number;// fontColor: string;// fontWeight: number;// }// autocomplete: boolean;// autosave: boolean;// }Note that types containing overloaded functions are not made deeply required due to a [TypeScript limitation](https://github.com/microsoft/TypeScript/issues/29732).
Utilities Object Array Set Map
type RequiredKeysOf
type RequiredKeysOf<BaseType extends object> = Exclude< { [Key in keyof BaseType]: BaseType extends Record<Key, BaseType[Key]> ? Key : never; }[keyof BaseType], undefined>;
Extract all required keys from the given type.
This is useful when you want to create a new type that contains different type values for the required keys only or use the list of keys for validation purposes, etc...
Example 1
import type {RequiredKeysOf} from 'type-fest';declare function createValidation<Entity extends object, Key extends RequiredKeysOf<Entity> = RequiredKeysOf<Entity>>(field: Key, validator: (value: Entity[Key]) => boolean): ValidatorFn;interface User {name: string;surname: string;luckyNumber?: number;}const validator1 = createValidation<User>('name', value => value.length < 25);const validator2 = createValidation<User>('surname', value => value.length < 25);Utilities
type RequireExactlyOne
type RequireExactlyOne< ObjectType, KeysType extends keyof ObjectType = keyof ObjectType> = { [Key in KeysType]: Required<Pick<ObjectType, Key>> & Partial<Record<Exclude<KeysType, Key>, never>>;}[KeysType] & Omit<ObjectType, KeysType>;
Create a type that requires exactly one of the given keys and disallows more. The remaining keys are kept as is.
Use-cases: - Creating interfaces for components that only need one of the keys to display properly. - Declaring generic keys in a single place for a single use-case that gets narrowed down via
RequireExactlyOne
.The caveat with
RequireExactlyOne
is that TypeScript doesn't always know at compile time every key that will exist at runtime. ThereforeRequireExactlyOne
can't do anything to prevent extra keys it doesn't know about.Example 1
import type {RequireExactlyOne} from 'type-fest';type Responder = {text: () => string;json: () => string;secure: boolean;};const responder: RequireExactlyOne<Responder, 'text' | 'json'> = {// Adding a `text` key here would cause a compile error.json: () => '{"message": "ok"}',secure: true};Object
type RequireOneOrNone
type RequireOneOrNone< ObjectType, KeysType extends keyof ObjectType = keyof ObjectType> = (RequireExactlyOne<ObjectType, KeysType> | RequireNone<KeysType>) & Omit<ObjectType, KeysType>;
Create a type that requires exactly one of the given keys and disallows more, or none of the given keys. The remaining keys are kept as is.
Example 1
import type {RequireOneOrNone} from 'type-fest';type Responder = RequireOneOrNone<{text: () => string;json: () => string;secure: boolean;}, 'text' | 'json'>;const responder1: Responder = {secure: true};const responder2: Responder = {text: () => '{"message": "hi"}',secure: true};const responder3: Responder = {json: () => '{"message": "ok"}',secure: true};Object
type Schema
type Schema< ObjectType, ValueType, Options extends SchemaOptions = {}> = ObjectType extends string ? ValueType : ObjectType extends Map<unknown, unknown> ? ValueType : ObjectType extends Set<unknown> ? ValueType : ObjectType extends ReadonlyMap<unknown, unknown> ? ValueType : ObjectType extends ReadonlySet<unknown> ? ValueType : ObjectType extends Array<infer U> ? Options['recurseIntoArrays'] extends false | undefined ? ValueType : Array<Schema<U, ValueType>> : ObjectType extends (...arguments_: unknown[]) => unknown ? ValueType : ObjectType extends Date ? ValueType : ObjectType extends Function ? ValueType : ObjectType extends RegExp ? ValueType : ObjectType extends object ? SchemaObject<ObjectType, ValueType, Options> : ValueType;
Create a deep version of another object type where property values are recursively replaced into a given value type.
Use-cases: - Form validation: Define how each field should be validated. - Form settings: Define configuration for input fields. - Parsing: Define types that specify special behavior for specific fields.
Example 1
import type {Schema} from 'type-fest';interface User {id: string;name: {firstname: string;lastname: string;};created: Date;active: boolean;passwordHash: string;attributes: ['Foo', 'Bar']}type UserMask = Schema<User, 'mask' | 'hide' | 'show'>;const userMaskSettings: UserMask = {id: 'show',name: {firstname: 'show',lastname: 'mask',},created: 'show',active: 'show',passwordHash: 'hide',attributes: ['mask', 'show']}Object
type SchemaOptions
type SchemaOptions = { /** By default, this affects elements in array and tuple types. You can change this by passing `{recurseIntoArrays: false}` as the third type argument: - If `recurseIntoArrays` is set to `true` (default), array elements will be recursively processed as well. - If `recurseIntoArrays` is set to `false`, arrays will not be recursively processed, and the entire array will be replaced with the given value type.
@example ``` type UserMask = Schema<User, 'mask' | 'hide' | 'show', {recurseIntoArrays: false}>;
const userMaskSettings: UserMask = { id: 'show', name: { firstname: 'show', lastname: 'mask', }, created: 'show', active: 'show', passwordHash: 'hide', attributes: 'hide' } ```
@default true */ readonly recurseIntoArrays?: boolean | undefined;};
See Also
Schema
type ScreamingSnakeCase
type ScreamingSnakeCase<Value> = Value extends string ? IsScreamingSnakeCase<Value> extends true ? Value : Uppercase<SnakeCase<Value>> : Value;
Convert a string literal to screaming-snake-case.
This can be useful when, for example, converting a camel-cased object property to a screaming-snake-cased SQL column name.
Example 1
import type {ScreamingSnakeCase} from 'type-fest';const someVariable: ScreamingSnakeCase<'fooBar'> = 'FOO_BAR';Change case Template literal
type SetFieldType
type SetFieldType< BaseType, Keys extends keyof BaseType, NewType, Options extends SetFieldTypeOptions = { preservePropertyModifiers: true }> = Simplify< { [P in keyof BaseType]: P extends Keys ? NewType : BaseType[P]; } & // `Record` is used to remove property modifiers (Options['preservePropertyModifiers'] extends false ? Record<Keys, NewType> : unknown)>;
Create a type that changes the type of the given keys.
Use-cases: - Creating variations of a base model. - Fixing incorrect external types.
Example 1
import type {SetFieldType} from 'type-fest';type MyModel = {readonly id: number;readonly createdAt: Date;updatedAt?: Date;};type MyModelApi = SetFieldType<MyModel, 'createdAt' | 'updatedAt', string>;// {// readonly id: number;// readonly createdAt: string;// updatedAt?: string;// }// `preservePropertyModifiers` option can be set to `false` if you want to remove property modifiers for properties being updatedtype MyModelApi = SetFieldType<MyModel, 'createdAt' | 'updatedAt', string, {preservePropertyModifiers: false}>;// {// readonly id: number;// createdAt: string; // no longer readonly// updatedAt: string; // no longer optional// }Object
See Also
Merge
if you need to change multiple properties to different types.
type SetNonNullable
type SetNonNullable<BaseType, Keys extends keyof BaseType = keyof BaseType> = { [Key in keyof BaseType]: Key extends Keys ? NonNullable<BaseType[Key]> : BaseType[Key];};
Create a type that makes the given keys non-nullable, where the remaining keys are kept as is.
If no keys are given, all keys will be made non-nullable.
Use-case: You want to define a single model where the only thing that changes is whether or not some or all of the keys are non-nullable.
Example 1
import type {SetNonNullable} from 'type-fest';type Foo = {a: number | null;b: string | undefined;c?: boolean | null;}type SomeNonNullable = SetNonNullable<Foo, 'b' | 'c'>;// type SomeNonNullable = {// a: number | null;// b: string; // Can no longer be undefined.// c?: boolean; // Can no longer be null, but is still optional.// }type AllNonNullable = SetNonNullable<Foo>;// type AllNonNullable = {// a: number; // Can no longer be null.// b: string; // Can no longer be undefined.// c?: boolean; // Can no longer be null, but is still optional.// }Object
type SetOptional
type SetOptional<BaseType, Keys extends keyof BaseType> = BaseType extends unknown // To distribute `BaseType` when it's a union type. ? Simplify< // Pick just the keys that are readonly from the base type. Except<BaseType, Keys> & // Pick the keys that should be mutable from the base type and make them mutable. Partial<HomomorphicPick<BaseType, Keys & KeysOfUnion<BaseType>>> > : never;
Create a type that makes the given keys optional. The remaining keys are kept as is. The sister of the
SetRequired
type.Use-case: You want to define a single model where the only thing that changes is whether or not some of the keys are optional.
Example 1
import type {SetOptional} from 'type-fest';type Foo = {a: number;b?: string;c: boolean;}type SomeOptional = SetOptional<Foo, 'b' | 'c'>;// type SomeOptional = {// a: number;// b?: string; // Was already optional and still is.// c?: boolean; // Is now optional.// }Object
type SetParameterType
type SetParameterType< Function_ extends (...arguments_: any[]) => unknown, P extends Record<number, unknown>> = // Just using `Parameters<Fn>` isn't ideal because it doesn't handle the `this` fake parameter. Function_ extends ( this: infer ThisArgument, ...arguments_: infer Arguments ) => unknown ? // If a function did not specify the `this` fake parameter, it will be inferred to `unknown`. // We want to detect this situation just to display a friendlier type upon hovering on an IntelliSense-powered IDE. IsUnknown<ThisArgument> extends true ? ( ...arguments_: MergeObjectToArray<Arguments, P> ) => ReturnType<Function_> : ( this: ThisArgument, ...arguments_: MergeObjectToArray<Arguments, P> ) => ReturnType<Function_> : Function_;
Create a function that replaces some parameters with the given parameters.
The parameters that are not specified will be kept as-is.
Note: - This type will ignore the given function's generic type. - If you change the parameter type that return type depends on, the return type will not change: ``` const fn = (a: number) => a; //=> fn: (a: number) => number;
// We change type of
a
tostring
, but return type is stillnumber
. type Fn = SetParameterType<typeof fn, {0: string}>; //=> (a: string) => number; ```Use-case: - Define a wrapped function that receives something different while returning the same type. - Mocking and testing. - Overload function type. (See example)
Example 1
import type {SetParameterType} from 'type-fest';type HandleMessage = (data: Data, message: string, ...arguments: any[]) => void;type HandleOk = SetParameterType<HandleMessage, {0: SuccessData, 1: 'ok'}>;//=> type HandleOk = (data: SuccessData, message: 'ok') => void;// Another way to define the parameters to replace.type HandleError = SetParameterType<HandleMessage, [data: ErrorData, message: 'error']>;//=> type HandleError = (data: ErrorData, message: 'error') => void;// Change single parameter type.type HandleWarn = SetParameterType<HandleMessage, {1: 'warn'}>;//=> type HandleWarn = (data: Data, message: 'warn') => void;// Change rest parameter type.// Way 1: Input full parameter type.type HandleLog = SetParameterType<HandleMessage, [data: Data, message: 'log', ...arguments: string[]]>;//=> type HandleLog = (data: Data, message: 'log', ...arguments: string[]) => void;// Way 2: Input rest parameter type by Object index.type HandleLog2 = SetParameterType<HandleMessage, {2: string}>;//=> type HandleLog2 = (data: Data, message: string, ...arguments: string[]) => void;Function
type SetReadonly
type SetReadonly<BaseType, Keys extends keyof BaseType> = // `extends unknown` is always going to be the case and is used to convert any // union into a [distributive conditional // type](https://www.typescriptlang.org/docs/handbook/release-notes/typescript-2-8.html#distributive-conditional-types). BaseType extends unknown ? Simplify< Except<BaseType, Keys> & Readonly<HomomorphicPick<BaseType, Keys & KeysOfUnion<BaseType>>> > : never;
Create a type that makes the given keys readonly. The remaining keys are kept as is.
Use-case: You want to define a single model where the only thing that changes is whether or not some of the keys are readonly.
Example 1
import type {SetReadonly} from 'type-fest';type Foo = {a: number;readonly b: string;c: boolean;}type SomeReadonly = SetReadonly<Foo, 'b' | 'c'>;// type SomeReadonly = {// a: number;// readonly b: string; // Was already readonly and still is.// readonly c: boolean; // Is now readonly.// }Object
type SetRequired
type SetRequired< BaseType, Keys extends keyof BaseType> = BaseType extends UnknownArray ? SetArrayRequired<BaseType, Keys> extends infer ResultantArray ? IfArrayReadonly<BaseType, Readonly<ResultantArray>, ResultantArray> : never : Simplify< // Pick just the keys that are optional from the base type. Except<BaseType, Keys> & // Pick the keys that should be required from the base type and make them required. Required<HomomorphicPick<BaseType, Keys & KeysOfUnion<BaseType>>> >;
Create a type that makes the given keys required. The remaining keys are kept as is. The sister of the
SetOptional
type.Use-case: You want to define a single model where the only thing that changes is whether or not some of the keys are required.
Example 1
import type {SetRequired} from 'type-fest';type Foo = {a?: number;b: string;c?: boolean;}type SomeRequired = SetRequired<Foo, 'b' | 'c'>;// type SomeRequired = {// a?: number;// b: string; // Was already required and still is.// c: boolean; // Is now required.// }// Set specific indices in an array to be required.type ArrayExample = SetRequired<[number?, number?, number?], 0 | 1>;//=> [number, number, number?]Object
type SetRequiredDeep
type SetRequiredDeep< BaseType, KeyPaths extends Paths<BaseType>> = BaseType extends NonRecursiveType ? BaseType : SimplifyDeep< (BaseType extends UnknownArray ? {} : { [K in keyof BaseType as K extends | KeyPaths | StringToNumber<KeyPaths & string> ? K : never]-?: BaseType[K]; }) & { [K in keyof BaseType]: Extract< KeyPaths, `${K & (string | number)}.${string}` > extends never ? BaseType[K] : SetRequiredDeep< BaseType[K], KeyPaths extends `${K & (string | number)}.${infer Rest extends Paths< BaseType[K] >}` ? Rest : never >; } >;
Create a type that makes the given keys required. You can specify deeply nested key paths. The remaining keys are kept as is.
Use-case: Selectively make nested properties required in complex types like models.
Example 1
import type {SetRequiredDeep} from 'type-fest';type Foo = {a?: number;b?: string;c?: {d?: number}[]}type SomeRequiredDeep = SetRequiredDeep<Foo, 'a' | `c.${number}.d`>;// type SomeRequiredDeep = {// a: number; // Is now required// b?: string;// c: {// d: number // Is now required// }[]// }Object
type SetReturnType
type SetReturnType<Function_ extends (...arguments_: any[]) => any, TypeToReturn> = // Just using `Parameters<Fn>` isn't ideal because it doesn't handle the `this` fake parameter. Function_ extends ( this: infer ThisArgument, ...arguments_: infer Arguments ) => any ? // If a function did not specify the `this` fake parameter, it will be inferred to `unknown`. // We want to detect this situation just to display a friendlier type upon hovering on an IntelliSense-powered IDE. IsUnknown<ThisArgument> extends true ? (...arguments_: Arguments) => TypeToReturn : (this: ThisArgument, ...arguments_: Arguments) => TypeToReturn : // This part should be unreachable, but we make it meaningful just in case… (...arguments_: Parameters<Function_>) => TypeToReturn;
Create a function type with a return type of your choice and the same parameters as the given function type.
Use-case: You want to define a wrapped function that returns something different while receiving the same parameters. For example, you might want to wrap a function that can throw an error into one that will return
undefined
instead.Example 1
import type {SetReturnType} from 'type-fest';type MyFunctionThatCanThrow = (foo: SomeType, bar: unknown) => SomeOtherType;type MyWrappedFunction = SetReturnType<MyFunctionThatCanThrow, SomeOtherType | undefined>;//=> type MyWrappedFunction = (foo: SomeType, bar: unknown) => SomeOtherType | undefined;Function
type SharedUnionFields
type SharedUnionFields<Union> = Extract< Union, | NonRecursiveType | ReadonlyMap<unknown, unknown> | ReadonlySet<unknown> | UnknownArray> extends infer SkippedMembers ? Exclude<Union, SkippedMembers> extends infer RelevantMembers ? | SkippedMembers | (IsNever<RelevantMembers> extends true ? never : Simplify<Pick<RelevantMembers, keyof RelevantMembers>>) : never : never;
Create a type with shared fields from a union of object types.
Use-cases: - You want a safe object type where each key exists in the union object. - You want to focus on the common fields of the union type and don't want to have to care about the other fields.
Example 1
import type {SharedUnionFields} from 'type-fest';type Cat = {name: string;type: 'cat';catType: string;};type Dog = {name: string;type: 'dog';dogType: string;};function displayPetInfo(petInfo: Cat | Dog) {// typeof petInfo =>// {// name: string;// type: 'cat';// catType: string; // Needn't care about this field, because it's not a common pet info field.// } | {// name: string;// type: 'dog';// dogType: string; // Needn't care about this field, because it's not a common pet info field.// }// petInfo type is complex and have some needless fieldsconsole.log('name: ', petInfo.name);console.log('type: ', petInfo.type);}function displayPetInfo(petInfo: SharedUnionFields<Cat | Dog>) {// typeof petInfo =>// {// name: string;// type: 'cat' | 'dog';// }// petInfo type is simple and clearconsole.log('name: ', petInfo.name);console.log('type: ', petInfo.type);}See Also
SharedUnionFieldsDeep
Object Union
type SharedUnionFieldsDeep
type SharedUnionFieldsDeep< Union, Options extends SharedUnionFieldsDeepOptions = { recurseIntoArrays: false }> = // `Union extends` will convert `Union` // to a [distributive conditionaltype](https://www.typescriptlang.org/docs/handbook/release-notes/typescript-2-8.html#distributive-conditional-types). // But this is not what we want, so we need to wrap `Union` with `[]` to prevent it. [Union] extends [ NonRecursiveType | ReadonlyMap<unknown, unknown> | ReadonlySet<unknown> ] ? Union : [Union] extends [UnknownArray] ? Options['recurseIntoArrays'] extends true ? SetArrayAccess< SharedArrayUnionFieldsDeep<Union, Options>, IsArrayReadonly<Union> > : Union : [Union] extends [object] ? SharedObjectUnionFieldsDeep<Union, Options> : Union;
Create a type with shared fields from a union of object types, deeply traversing nested structures.
Use the to specify the behavior for arrays.
Use-cases: - You want a safe object type where each key exists in the union object. - You want to focus on the common fields of the union type and don't want to have to care about the other fields.
Example 1
import type {SharedUnionFieldsDeep} from 'type-fest';type Cat = {info: {name: string;type: 'cat';catType: string;};};type Dog = {info: {name: string;type: 'dog';dogType: string;};};function displayPetInfo(petInfo: (Cat | Dog)['info']) {// typeof petInfo =>// {// name: string;// type: 'cat';// catType: string; // Needn't care about this field, because it's not a common pet info field.// } | {// name: string;// type: 'dog';// dogType: string; // Needn't care about this field, because it's not a common pet info field.// }// petInfo type is complex and have some needless fieldsconsole.log('name: ', petInfo.name);console.log('type: ', petInfo.type);}function displayPetInfo(petInfo: SharedUnionFieldsDeep<Cat | Dog>['info']) {// typeof petInfo =>// {// name: string;// type: 'cat' | 'dog';// }// petInfo type is simple and clearconsole.log('name: ', petInfo.name);console.log('type: ', petInfo.type);}See Also
SharedUnionFields
Object Union
type Simplify
type Simplify<T> = { [KeyType in keyof T]: T[KeyType] } & {};
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.
Example 1
import type {Simplify} from 'type-fest';type PositionProps = {top: number;left: number;};type SizeProps = {width: number;height: number;};// In your editor, hovering over `Props` will show a flattened object with all the properties.type Props = Simplify<PositionProps & SizeProps>;Sometimes it is desired to pass a value as a function argument that has a different type. At first inspection it may seem assignable, and then you discover it is not because the
value
's type definition was defined as an interface. In the following example,fn
requires an argument of typeRecord<string, unknown>
. If the value is defined as a literal, then it is assignable. And if thevalue
is defined as type using theSimplify
utility the value is assignable. But if thevalue
is defined as an interface, it is not assignable because the interface is not sealed and elsewhere a non-string property could be added to the interface.If the type definition must be an interface (perhaps it was defined in a third-party npm package), then the
value
can be defined asconst value: Simplify<SomeInterface> = ...
. Thenvalue
will be assignable to thefn
argument. Or thevalue
can be cast asSimplify<SomeInterface>
if you can't re-declare thevalue
.Example 2
import type {Simplify} from 'type-fest';interface SomeInterface {foo: number;bar?: string;baz: number | undefined;}type SomeType = {foo: number;bar?: string;baz: number | undefined;};const literal = {foo: 123, bar: 'hello', baz: 456};const someType: SomeType = literal;const someInterface: SomeInterface = literal;function fn(object: Record<string, unknown>): void {}fn(literal); // Good: literal object type is sealedfn(someType); // Good: type is sealedfn(someInterface); // Error: Index signature for type 'string' is missing in type 'someInterface'. Because `interface` can be re-openedfn(someInterface as Simplify<SomeInterface>); // Good: transform an `interface` into a `type`https://github.com/microsoft/TypeScript/issues/15300
See Also
SimplifyDeep Object
type SimplifyDeep
type SimplifyDeep<Type, ExcludeType = never> = ConditionalSimplifyDeep< Type, ExcludeType | NonRecursiveType | Set<unknown> | Map<unknown, unknown>, object>;
Deeply simplifies an object type.
You can exclude certain types from being simplified by providing them in the second generic
ExcludeType
.Useful to flatten the type output to improve type hints shown in editors.
Example 1
import type {SimplifyDeep} from 'type-fest';type PositionX = {left: number;right: number;};type PositionY = {top: number;bottom: number;};type Properties1 = {height: number;position: PositionY;};type Properties2 = {width: number;position: PositionX;};type Properties = Properties1 & Properties2;// In your editor, hovering over `Props` will show the following://// type Properties = Properties1 & Properties2;type SimplifyDeepProperties = SimplifyDeep<Properties1 & Properties2>;// But if wrapped in SimplifyDeep, hovering over `SimplifyDeepProperties` will show a flattened object with all the properties://// SimplifyDeepProperties = {// height: number;// width: number;// position: {// top: number;// bottom: number;// left: number;// right: number;// };// };Example 2
import type {SimplifyDeep} from 'type-fest';// A complex type that you don't want or need to simplifytype ComplexType = {a: string;b: 'b';c: number;...};type PositionX = {left: number;right: number;};type PositionY = {top: number;bottom: number;};// You want to simplify all other typestype Properties1 = {height: number;position: PositionY;foo: ComplexType;};type Properties2 = {width: number;position: PositionX;foo: ComplexType;};type SimplifyDeepProperties = SimplifyDeep<Properties1 & Properties2, ComplexType>;// If wrapped in `SimplifyDeep` and set `ComplexType` to exclude, hovering over `SimplifyDeepProperties` will// show a flattened object with all the properties except `ComplexType`://// SimplifyDeepProperties = {// height: number;// width: number;// position: {// top: number;// bottom: number;// left: number;// right: number;// };// foo: ComplexType;// };See Also
Simplify Object
type SingleKeyObject
type SingleKeyObject<ObjectType> = IsUnion<keyof ObjectType> extends true ? never : IfEmptyObject<ObjectType, never, ObjectType>;
Create a type that only accepts an object with a single key.
Example 1
import type {SingleKeyObject} from 'type-fest';const someFunction = <T>(parameter: SingleKeyObject<T>) => {};someFunction({value: true});someFunction({value: true,otherKey: true});// Error: Argument of type '{value: boolean; otherKey: boolean}' is not assignable to parameter of type 'never'.ts(2345)Object
type SnakeCase
type SnakeCase<Value> = DelimiterCase<Value, '_'>;
Convert a string literal to snake-case.
This can be useful when, for example, converting a camel-cased object property to a snake-cased SQL column name.
Example 1
import type {SnakeCase} from 'type-fest';// Simpleconst someVariable: SnakeCase<'fooBar'> = 'foo_bar';// Advancedtype SnakeCasedProperties<T> = {[K in keyof T as SnakeCase<K>]: T[K]};interface ModelProps {isHappy: boolean;fullFamilyName: string;foo: number;}const dbResult: SnakeCasedProperties<ModelProps> = {'is_happy': true,'full_family_name': 'Carla Smith',foo: 123};Change case Template literal
type SnakeCasedProperties
type SnakeCasedProperties<Value> = DelimiterCasedProperties<Value, '_'>;
Convert object properties to snake case but not recursively.
This can be useful when, for example, converting some API types from a different style.
Example 1
import type {SnakeCasedProperties} from 'type-fest';interface User {userId: number;userName: string;}const result: SnakeCasedProperties<User> = {user_id: 1,user_name: 'Tom',};Change case Template literal Object
See Also
SnakeCase
SnakeCasedPropertiesDeep
type SnakeCasedPropertiesDeep
type SnakeCasedPropertiesDeep<Value> = DelimiterCasedPropertiesDeep<Value, '_'>;
Convert object properties to snake case recursively.
This can be useful when, for example, converting some API types from a different style.
Example 1
import type {SnakeCasedPropertiesDeep} from 'type-fest';interface User {userId: number;userName: string;}interface UserWithFriends {userInfo: User;userFriends: User[];}const result: SnakeCasedPropertiesDeep<UserWithFriends> = {user_info: {user_id: 1,user_name: 'Tom',},user_friends: [{user_id: 2,user_name: 'Jerry',},{user_id: 3,user_name: 'Spike',},],};Change case Template literal Object
See Also
SnakeCase
SnakeCasedProperties
type Split
type Split< S extends string, Delimiter extends string> = S extends `${infer Head}${Delimiter}${infer Tail}` ? [Head, ...Split<Tail, Delimiter>] : S extends Delimiter ? [] : [S];
Represents an array of strings split using a given character or character set.
Use-case: Defining the return type of a method like
String.prototype.split
.Example 1
import type {Split} from 'type-fest';declare function split<S extends string, D extends string>(string: S, separator: D): Split<S, D>;type Item = 'foo' | 'bar' | 'baz' | 'waldo';const items = 'foo,bar,baz,waldo';let array: Item[];array = split(items, ',');String Template literal
type Spread
type Spread< FirstType extends Spreadable, SecondType extends Spreadable> = FirstType extends TupleOrArray ? SecondType extends TupleOrArray ? SpreadTupleOrArray<FirstType, SecondType> : Simplify<SpreadObject<FirstType, SecondType>> : Simplify<SpreadObject<FirstType, SecondType>>;
Mimic the type inferred by TypeScript when merging two objects or two arrays/tuples using the spread syntax.
Example 1
import type {Spread} from 'type-fest';type Foo = {a: number;b?: string;};type Bar = {b?: number;c: boolean;};const foo = {a: 1, b: '2'};const bar = {c: false};const fooBar = {...foo, ...bar};type FooBar = Spread<Foo, Bar>;// type FooBar = {// a: number;// b?: string | number | undefined;// c: boolean;// }const baz = (argument: FooBar) => {// Do something}baz(fooBar);Example 2
import type {Spread} from 'type-fest';const foo = [1, 2, 3];const bar = ['4', '5', '6'];const fooBar = [...foo, ...bar];type FooBar = Spread<typeof foo, typeof bar>;// FooBar = (string | number)[]const baz = (argument: FooBar) => {// Do something};baz(fooBar);Object
type Stringified
type Stringified<ObjectType> = { [KeyType in keyof ObjectType]: string };
Create a type with the keys of the given type changed to
string
type.Use-case: Changing interface values to strings in order to use them in a form model.
Example 1
import type {Stringified} from 'type-fest';type Car = {model: string;speed: number;}const carForm: Stringified<Car> = {model: 'Foo',speed: '101'};Object
type StringKeyOf
type StringKeyOf<BaseType> = `${Extract<keyof BaseType, string | number>}`;
Get keys of the given type as strings.
Number keys are converted to strings.
Use-cases: - Get string keys from a type which may have number keys. - Makes it possible to index using strings retrieved from template types.
Example 1
import type {StringKeyOf} from 'type-fest';type Foo = {1: number,stringKey: string,};type StringKeysOfFoo = StringKeyOf<Foo>;//=> '1' | 'stringKey'Object
type StringRepeat
type StringRepeat<Input extends string, Count extends number> = number extends Count ? Input extends '' ? '' : string : IsNegative<Count> extends true ? never : Count extends 0 ? '' : string extends Input ? string : StringRepeat<Input, Subtract<Count, 1>> extends infer R extends string ? `${Input}${R}` : never;
Returns a new string which contains the specified number of copies of a given string, just like
String#repeat()
.Example 1
import {StringRepeat} from 'type-fest';declare function stringRepeat<Input extends string,Count extends number>(input: Input, count: Count): StringRepeat<Input, Count>;// The return type is the exact string literal, not just `string`.stringRepeat('foo', 2);//=> 'foofoo'stringRepeat('=', 3);//=> '==='String Template literal
type StringSlice
type StringSlice< S extends string, Start extends number = 0, End extends number = StringToArray<S>['length']> = string extends S ? string[] : ArraySlice< StringToArray<S>, Start, End > extends infer R extends readonly string[] ? Join<R, ''> : never;
Returns a string slice of a given range, just like
String#slice()
.Example 1
import type {StringSlice} from 'type-fest';StringSlice<'abcde', 0, 2>;//=> 'ab'StringSlice<'abcde', 1>;//=> 'bcde'StringSlice<'abcde', 0, -1>;//=> 'abcd'StringSlice<'abcde', -2, -1>;//=> 'd'String
See Also
{ArraySlice}
type StructuredCloneable
type StructuredCloneable = | StructuredCloneablePrimitive | StructuredCloneableData | StructuredCloneableCollection;
Matches a value that can be losslessly cloned using
structuredClone
.Note: - Custom error types will be cloned as the base
Error
typeExample 1
import type {StructuredCloneable} from 'type-fest';class CustomClass {}// @ts-expect-errorconst error: StructuredCloneable = {custom: new CustomClass(),};structuredClone(error);//=> {custom: {}}const good: StructuredCloneable = {number: 3,date: new Date(),map: new Map<string, number>(),}good.map.set('key', 1);structuredClone(good);//=> {number: 3, date: Date(2022-10-17 22:22:35.920), map: Map {'key' -> 1}}Structured clone
See Also
https://developer.mozilla.org/en-US/docs/Web/API/Web_Workers_API/Structured_clone_algorithm
type Subtract
type Subtract<A extends number, B extends number> = number extends A | B ? number : [ IsEqual<A, PositiveInfinity>, IsEqual<A, NegativeInfinity>, IsEqual<B, PositiveInfinity>, IsEqual<B, NegativeInfinity> ] extends infer R extends [boolean, boolean, boolean, boolean] ? Or< And<IsEqual<R[0], true>, IsEqual<R[2], false>>, And<IsEqual<R[3], true>, IsEqual<R[1], false>> > extends true ? PositiveInfinity : Or< And<IsEqual<R[1], true>, IsEqual<R[3], false>>, And<IsEqual<R[2], true>, IsEqual<R[0], false>> > extends true ? NegativeInfinity : true extends R[number] ? number : [IsNegative<A>, IsNegative<B>] extends infer R ? [false, false] extends R ? BuildTuple<A> extends infer R ? R extends [...BuildTuple<B>, ...infer R] ? R['length'] : number : never : LessThan<A, B> extends true ? number : [false, true] extends R ? Sum<A, NumberAbsolute<B>> : Subtract<NumberAbsolute<B>, NumberAbsolute<A>> : never : never;
Returns the difference between two numbers.
Note: - A or B can only support
-999
~999
. - If the result is negative, you can only getnumber
.Example 1
import type {Subtract} from 'type-fest';Subtract<333, 222>;//=> 111Subtract<111, -222>;//=> 333Subtract<-111, 222>;//=> numberSubtract<PositiveInfinity, 9999>;//=> PositiveInfinitySubtract<PositiveInfinity, PositiveInfinity>;//=> numberNumeric
type Sum
type Sum<A extends number, B extends number> = number extends A | B ? number : [ IsEqual<A, PositiveInfinity>, IsEqual<A, NegativeInfinity>, IsEqual<B, PositiveInfinity>, IsEqual<B, NegativeInfinity> ] extends infer R extends [boolean, boolean, boolean, boolean] ? Or< And<IsEqual<R[0], true>, IsEqual<R[3], false>>, And<IsEqual<R[2], true>, IsEqual<R[1], false>> > extends true ? PositiveInfinity : Or< And<IsEqual<R[1], true>, IsEqual<R[2], false>>, And<IsEqual<R[3], true>, IsEqual<R[0], false>> > extends true ? NegativeInfinity : true extends R[number] ? number : ([IsNegative<A>, IsNegative<B>] extends infer R ? [false, false] extends R ? [...BuildTuple<A>, ...BuildTuple<B>]['length'] : [true, true] extends R ? number : TupleMax< [NumberAbsolute<A>, NumberAbsolute<B>] > extends infer Max_ ? TupleMin< [NumberAbsolute<A>, NumberAbsolute<B>] > extends infer Min_ extends number ? Max_ extends A | B ? Subtract<Max_, Min_> : number : never : never : never) & number : never;
Returns the sum of two numbers.
Note: - A or B can only support
-999
~999
. - A and B can only be small integers, less than 1000. - If the result is negative, you can only getnumber
.Example 1
import type {Sum} from 'type-fest';Sum<111, 222>;//=> 333Sum<-111, 222>;//=> 111Sum<111, -222>;//=> numberSum<PositiveInfinity, -9999>;//=> PositiveInfinitySum<PositiveInfinity, NegativeInfinity>;//=> numberNumeric
type Tagged
type Tagged<Type, TagName extends PropertyKey, TagMetadata = never> = Type & Tag<TagName, TagMetadata>;
Attach a "tag" to an arbitrary type. This allows you to create distinct types, that aren't assignable to one another, for distinct concepts in your program that should not be interchangeable, even if their runtime values have the same type. (See examples.)
A type returned by
Tagged
can be passed toTagged
again, to create a type with multiple tags.[Read more about tagged types.](https://medium.com/@KevinBGreene/surviving-the-typescript-ecosystem-branding-and-type-tagging-6cf6e516523d)
A tag's name is usually a string (and must be a string, number, or symbol), but each application of a tag can also contain an arbitrary type as its "metadata". See GetTagMetadata for examples and explanation.
A type
A
returned byTagged
is assignable to another typeB
returned byTagged
if and only if: - the underlying (untagged) type ofA
is assignable to the underlying type ofB
; -A
contains at least all the tagsB
has; - and the metadata type for each ofA
's tags is assignable to the metadata type ofB
's corresponding tag.There have been several discussions about adding similar features to TypeScript. Unfortunately, nothing has (yet) moved forward: - [Microsoft/TypeScript#202](https://github.com/microsoft/TypeScript/issues/202) - [Microsoft/TypeScript#4895](https://github.com/microsoft/TypeScript/issues/4895) - [Microsoft/TypeScript#33290](https://github.com/microsoft/TypeScript/pull/33290)
Example 1
import type {Tagged} from 'type-fest';type AccountNumber = Tagged<number, 'AccountNumber'>;type AccountBalance = Tagged<number, 'AccountBalance'>;function createAccountNumber(): AccountNumber {// As you can see, casting from a `number` (the underlying type being tagged) is allowed.return 2 as AccountNumber;}function getMoneyForAccount(accountNumber: AccountNumber): AccountBalance {return 4 as AccountBalance;}// This will compile successfully.getMoneyForAccount(createAccountNumber());// But this won't, because it has to be explicitly passed as an `AccountNumber` type!// Critically, you could not accidentally use an `AccountBalance` as an `AccountNumber`.getMoneyForAccount(2);// You can also use tagged values like their underlying, untagged type.// I.e., this will compile successfully because an `AccountNumber` can be used as a regular `number`.// In this sense, the underlying base type is not hidden, which differentiates tagged types from opaque types in other languages.const accountNumber = createAccountNumber() + 2;Example 2
import type {Tagged} from 'type-fest';// You can apply multiple tags to a type by using `Tagged` repeatedly.type Url = Tagged<string, 'URL'>;type SpecialCacheKey = Tagged<Url, 'SpecialCacheKey'>;// You can also pass a union of tag names, so this is equivalent to the above, although it doesn't give you the ability to assign distinct metadata to each tag.type SpecialCacheKey2 = Tagged<string, 'URL' | 'SpecialCacheKey'>;Type
type TaggedUnion
type TaggedUnion< TagKey extends string, UnionMembers extends Record<string, Record<string, unknown>>> = { [Name in keyof UnionMembers]: { [Key in TagKey]: Name } & UnionMembers[Name];}[keyof UnionMembers];
Create a union of types that share a common discriminant property.
Use-case: A shorter way to declare tagged unions with multiple members.
Example 1
import type {TaggedUnion} from 'type-fest';type Tagged<Fields extends Record<string, unknown> = TaggedUnion<'type', Fields>// The TaggedUnion utility reduces the amount of boilerplate needed to create a tagged union with multiple members, making the code more concise.type EventMessage = Tagged<{OpenExternalUrl: {url: string;id: number;language: string;};ToggleBackButtonVisibility: {visible: boolean;};PurchaseButtonPressed: {price: number;time: Date;};NavigationStateChanged: {navigation?: string;};}>;// Here is the same type created without this utility.type EventMessage =| {type: 'OpenExternalUrl';url: string;id: number;language: string;}| {type: 'ToggleBackButtonVisibility'; visible: boolean}| {type: 'PurchaseButtonPressed'; price: number; time: Date}| {type: 'NavigationStateChanged'; navigation?: string};Utilities
type Trim
type Trim<V extends string> = TrimLeft<TrimRight<V>>;
Remove leading and trailing spaces from a string.
Example 1
import type {Trim} from 'type-fest';Trim<' foo '>//=> 'foo'String Template literal
type TsConfigJson
type TsConfigJson = { /** Instructs the TypeScript compiler how to compile `.ts` files. */ compilerOptions?: TsConfigJson.CompilerOptions;
/** Instructs the TypeScript compiler how to watch files. */ watchOptions?: TsConfigJson.WatchOptions;
/** Auto type (.d.ts) acquisition options for this project. */ typeAcquisition?: TsConfigJson.TypeAcquisition;
/** Enable Compile-on-Save for this project. */ compileOnSave?: boolean;
/** Path to base configuration file to inherit from. */ extends?: string | string[];
/** If no `files` or `include` property is present in a `tsconfig.json`, the compiler defaults to including all files in the containing directory and subdirectories except those specified by `exclude`. When a `files` property is specified, only those files and those specified by `include` are included. */ files?: string[];
/** Specifies a list of files to be excluded from compilation. The `exclude` property only affects the files included via the `include` property and not the `files` property.
Glob patterns require TypeScript version 2.0 or later. */ exclude?: string[];
/** Specifies a list of glob patterns that match files to be included in compilation.
If no `files` or `include` property is present in a `tsconfig.json`, the compiler defaults to including all files in the containing directory and subdirectories except those specified by `exclude`. */ include?: string[];
/** Referenced projects. */ references?: TsConfigJson.References[];};
Type for [TypeScript's
tsconfig.json
file](https://www.typescriptlang.org/docs/handbook/tsconfig-json.html) (TypeScript 3.7).File
type TupleToUnion
type TupleToUnion<ArrayType> = ArrayType extends readonly unknown[] ? ArrayType[number] : never;
Convert a tuple/array into a union type of its elements.
This can be useful when you have a fixed set of allowed values and want a type defining only the allowed values, but do not want to repeat yourself.
Example 1
import type {TupleToUnion} from 'type-fest';const destinations = ['a', 'b', 'c'] as const;type Destination = TupleToUnion<typeof destinations>;//=> 'a' | 'b' | 'c'function verifyDestination(destination: unknown): destination is Destination {return destinations.includes(destination as any);}type RequestBody = {deliverTo: Destination;};function verifyRequestBody(body: unknown): body is RequestBody {const deliverTo = (body as any).deliverTo;return typeof body === 'object' && body !== null && verifyDestination(deliverTo);}Alternatively, you may use
typeof destinations[number]
. Ifdestinations
is a tuple, there is no difference. However ifdestinations
is a string, the resulting type will the union of the characters in the string. Other types ofdestinations
may result in a compile error. In comparison, TupleToUnion will returnnever
if a tuple is not provided.Example 2
const destinations = ['a', 'b', 'c'] as const;type Destination = typeof destinations[number];//=> 'a' | 'b' | 'c'const erroringType = new Set(['a', 'b', 'c']);type ErroringType = typeof erroringType[number];//=> Type 'Set<string>' has no matching index signature for type 'number'. ts(2537)const numberBool: { [n: number]: boolean } = { 1: true };type NumberBool = typeof numberBool[number];//=> booleanArray
type TypedArray
type TypedArray = | Int8Array | Uint8Array | Uint8ClampedArray | Int16Array | Uint16Array | Int32Array | Uint32Array | Float32Array | Float64Array | BigInt64Array | BigUint64Array;
Matches any [typed array](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray), like
Uint8Array
orFloat64Array
.Array
type UndefinedOnPartialDeep
type UndefinedOnPartialDeep<T> = // Handle built-in type and function T extends BuiltIns | Function ? T : // Handle tuple and array T extends readonly unknown[] ? UndefinedOnPartialList<T> : // Handle map and readonly map T extends Map<infer K, infer V> ? Map<K, UndefinedOnPartialDeep<V>> : T extends ReadonlyMap<infer K, infer V> ? ReadonlyMap<K, UndefinedOnPartialDeep<V>> : // Handle set and readonly set T extends Set<infer K> ? Set<UndefinedOnPartialDeep<K>> : T extends ReadonlySet<infer K> ? ReadonlySet<UndefinedOnPartialDeep<K>> : // Handle object T extends Record<any, any> ? { [KeyType in keyof T]: undefined extends T[KeyType] ? UndefinedOnPartialDeep<T[KeyType]> | undefined : UndefinedOnPartialDeep<T[KeyType]>; } : T;
Create a deep version of another type where all optional keys are set to also accept
undefined
.Note: This is only needed when the [
exactOptionalPropertyTypes
](https://www.typescriptlang.org/tsconfig#exactOptionalPropertyTypes) TSConfig setting is enabled.Use-cases: - When
exactOptionalPropertyTypes
is enabled, an object like{a: undefined}
is not assignable to the type{a?: number}
. You can useUndefinedOnPartialDeep<{a?: number}>
to make it assignable.Example 1
import type {UndefinedOnPartialDeep} from 'type-fest';interface Settings {optionA: string;optionB?: number;subOption: {subOptionA: boolean;subOptionB?: boolean;}};const testSettingsA: Settings = {optionA: 'foo',optionB: undefined, // TypeScript error if `exactOptionalPropertyTypes` is true.subOption: {subOptionA: true,subOptionB: undefined, // TypeScript error if `exactOptionalPropertyTypes` is true},};const testSettingsB: UndefinedOnPartialDeep<Settings> = {optionA: 'foo',optionB: undefined, // 👉 `optionB` can be set to undefined now.subOption: {subOptionA: true,subOptionB: undefined, // 👉 `subOptionB` can be set to undefined now.},};
type UnionToIntersection
type UnionToIntersection<Union> = // `extends unknown` is always going to be the case and is used to convert the // `Union` into a [distributive conditional // type](https://www.typescriptlang.org/docs/handbook/release-notes/typescript-2-8.html#distributive-conditional-types). ( Union extends unknown ? // The union type is used as the only argument to a function since the union // of function arguments is an intersection. (distributedUnion: Union) => void : // This won't happen. never ) extends // Infer the `Intersection` type since TypeScript represents the positional // arguments of unions of functions as an intersection of the union. (mergedIntersection: infer Intersection) => void ? // The `& Union` is to allow indexing by the resulting type Intersection & Union : never;
Convert a union type to an intersection type using [distributive conditional types](https://www.typescriptlang.org/docs/handbook/release-notes/typescript-2-8.html#distributive-conditional-types).
Inspired by [this Stack Overflow answer](https://stackoverflow.com/a/50375286/2172153).
Example 1
import type {UnionToIntersection} from 'type-fest';type Union = {the(): void} | {great(arg: string): void} | {escape: boolean};type Intersection = UnionToIntersection<Union>;//=> {the(): void; great(arg: string): void; escape: boolean};A more applicable example which could make its way into your library code follows.
Example 2
import type {UnionToIntersection} from 'type-fest';class CommandOne {commands: {a1: () => undefined,b1: () => undefined,}}class CommandTwo {commands: {a2: (argA: string) => undefined,b2: (argB: string) => undefined,}}const union = [new CommandOne(), new CommandTwo()].map(instance => instance.commands);type Union = typeof union;//=> {a1(): void; b1(): void} | {a2(argA: string): void; b2(argB: string): void}type Intersection = UnionToIntersection<Union>;//=> {a1(): void; b1(): void; a2(argA: string): void; b2(argB: string): void}Type
type UnionToTuple
type UnionToTuple<T, L = LastOfUnion<T>> = IsNever<T> extends false ? [...UnionToTuple<Exclude<T, L>>, L] : [];
Convert a union type into an unordered tuple type of its elements.
"Unordered" means the elements of the tuple are not guaranteed to be in the same order as in the union type. The arrangement can appear random and may change at any time.
This can be useful when you have objects with a finite set of keys and want a type defining only the allowed keys, but do not want to repeat yourself.
Example 1
import type {UnionToTuple} from 'type-fest';type Numbers = 1 | 2 | 3;type NumbersTuple = UnionToTuple<Numbers>;//=> [1, 2, 3]Example 2
import type {UnionToTuple} from 'type-fest';const pets = {dog: '🐶',cat: '🐱',snake: '🐍',};type Pet = keyof typeof pets;//=> 'dog' | 'cat' | 'snake'const petList = Object.keys(pets) as UnionToTuple<Pet>;//=> ['dog', 'cat', 'snake']Array
type UnknownArray
type UnknownArray = readonly unknown[];
Represents an array with
unknown
value.Use case: You want a type that all arrays can be assigned to, but you don't care about the value.
Example 1
import type {UnknownArray} from 'type-fest';type IsArray<T> = T extends UnknownArray ? true : false;type A = IsArray<['foo']>;//=> truetype B = IsArray<readonly number[]>;//=> truetype C = IsArray<string>;//=> falseType Array
type UnknownRecord
type UnknownRecord = Record<PropertyKey, unknown>;
Represents an object with
unknown
value. You probably want this instead of{}
.Use case: You have an object whose keys and values are unknown to you.
Example 1
import type {UnknownRecord} from 'type-fest';function toJson(object: UnknownRecord) {return JSON.stringify(object);}toJson({hello: 'world'});//=> '{"hello":"world"}'function isObject(value: unknown): value is UnknownRecord {return typeof value === 'object' && value !== null;}isObject({hello: 'world'});//=> trueisObject('hello');//=> falseType Object
type Unsubscribable
type Unsubscribable = { unsubscribe(): void;};
Remarks
The TC39 observable proposal defines a
closed
property, but some implementations (such as xstream) do not as of 10/08/2021. As well, some guidance on making anObservable
to not includeclosed
property.See Also
https://github.com/tc39/proposal-observable/blob/master/src/Observable.js#L129-L130
https://github.com/staltz/xstream/blob/6c22580c1d84d69773ee4b0905df44ad464955b3/src/index.ts#L79-L85
https://github.com/benlesh/symbol-observable#making-an-object-observable
Observable
type UnwrapOpaque
type UnwrapOpaque<OpaqueType extends TagContainer<unknown>> = OpaqueType extends Tag< PropertyKey, any> ? RemoveAllTags<OpaqueType> : OpaqueType extends Opaque<infer Type, OpaqueType[typeof tag]> ? Type : OpaqueType;
Note: The
UnwrapOpaque
type is deprecated in favor ofUnwrapTagged
.Revert an opaque or tagged type back to its original type by removing the readonly
[tag]
.Why is this necessary?
1. Use an
Opaque
type as object keys 2. Prevent TS4058 error: "Return type of exported function has or is using name X from external module Y but cannot be named"Example 1
import type {Opaque, UnwrapOpaque} from 'type-fest';type AccountType = Opaque<'SAVINGS' | 'CHECKING', 'AccountType'>;const moneyByAccountType: Record<UnwrapOpaque<AccountType>, number> = {SAVINGS: 99,CHECKING: 0.1};// Without UnwrapOpaque, the following expression would throw a type error.const money = moneyByAccountType.SAVINGS; // TS error: Property 'SAVINGS' does not exist// Attempting to pass an non-Opaque type to UnwrapOpaque will raise a type error.type WontWork = UnwrapOpaque<string>;// Using a Tagged type will work too.type WillWork = UnwrapOpaque<Tagged<number, 'AccountNumber'>>; // numberType
Deprecated
Use UnwrapTagged instead
type UnwrapTagged
type UnwrapTagged<TaggedType extends Tag<PropertyKey, any>> = RemoveAllTags<TaggedType>;
Revert a tagged type back to its original type by removing all tags.
Why is this necessary?
1. Use a
Tagged
type as object keys 2. Prevent TS4058 error: "Return type of exported function has or is using name X from external module Y but cannot be named"Example 1
import type {Tagged, UnwrapTagged} from 'type-fest';type AccountType = Tagged<'SAVINGS' | 'CHECKING', 'AccountType'>;const moneyByAccountType: Record<UnwrapTagged<AccountType>, number> = {SAVINGS: 99,CHECKING: 0.1};// Without UnwrapTagged, the following expression would throw a type error.const money = moneyByAccountType.SAVINGS; // TS error: Property 'SAVINGS' does not exist// Attempting to pass an non-Tagged type to UnwrapTagged will raise a type error.type WontWork = UnwrapTagged<string>;Type
type ValueOf
type ValueOf< ObjectType, ValueType extends keyof ObjectType = keyof ObjectType> = ObjectType[ValueType];
Create a union of the given object's values, and optionally specify which keys to get the values from.
Please upvote [this issue](https://github.com/microsoft/TypeScript/issues/31438) if you want to have this type as a built-in in TypeScript.
Example 1
// data.json{'foo': 1,'bar': 2,'biz': 3}// main.tsimport type {ValueOf} from 'type-fest';import data = require('./data.json');export function getData(name: string): ValueOf<typeof data> {return data[name];}export function onlyBar(name: string): ValueOf<typeof data, 'bar'> {return data[name];}// file.tsimport {getData, onlyBar} from './main';getData('foo');//=> 1onlyBar('foo');//=> TypeError ...onlyBar('bar');//=> 2Object
type Words
type Words< Sentence extends string, LastCharacter extends string = '', CurrentWord extends string = ''> = Sentence extends `${infer FirstCharacter}${infer RemainingCharacters}` ? FirstCharacter extends WordSeparators ? // Skip word separator [...SkipEmptyWord<CurrentWord>, ...Words<RemainingCharacters>] : LastCharacter extends '' ? // Fist char of word Words<RemainingCharacters, FirstCharacter, FirstCharacter> : // Case change: non-numeric to numeric, push word [false, true] extends [IsNumeric<LastCharacter>, IsNumeric<FirstCharacter>] ? [ ...SkipEmptyWord<CurrentWord>, ...Words<RemainingCharacters, FirstCharacter, FirstCharacter> ] : // Case change: numeric to non-numeric, push word [true, false] extends [IsNumeric<LastCharacter>, IsNumeric<FirstCharacter>] ? [ ...SkipEmptyWord<CurrentWord>, ...Words<RemainingCharacters, FirstCharacter, FirstCharacter> ] : // No case change: concat word [true, true] extends [IsNumeric<LastCharacter>, IsNumeric<FirstCharacter>] ? Words< RemainingCharacters, FirstCharacter, `${CurrentWord}${FirstCharacter}` > : // Case change: lower to upper, push word [true, true] extends [ IsLowerCase<LastCharacter>, IsUpperCase<FirstCharacter> ] ? [ ...SkipEmptyWord<CurrentWord>, ...Words<RemainingCharacters, FirstCharacter, FirstCharacter> ] : // Case change: upper to lower, brings back the last character, push word [true, true] extends [ IsUpperCase<LastCharacter>, IsLowerCase<FirstCharacter> ] ? [ ...RemoveLastCharacter<CurrentWord, LastCharacter>, ...Words< RemainingCharacters, FirstCharacter, `${LastCharacter}${FirstCharacter}` > ] : // No case change: concat word Words< RemainingCharacters, FirstCharacter, `${CurrentWord}${FirstCharacter}` > : [...SkipEmptyWord<CurrentWord>];
Split a string (almost) like Lodash's
_.words()
function.- Split on each word that begins with a capital letter. - Split on each WordSeparators. - Split on numeric sequence.
Example 1
import type {Words} from 'type-fest';type Words0 = Words<'helloWorld'>;//=> ['hello', 'World']type Words1 = Words<'helloWORLD'>;//=> ['hello', 'WORLD']type Words2 = Words<'hello-world'>;//=> ['hello', 'world']type Words3 = Words<'--hello the_world'>;//=> ['hello', 'the', 'world']type Words4 = Words<'lifeIs42'>;//=> ['life', 'Is', '42']Change case Template literal
type Writable
type Writable< BaseType, Keys extends keyof BaseType = keyof BaseType> = BaseType extends ReadonlyMap<infer KeyType, infer ValueType> ? Map<KeyType, ValueType> : BaseType extends ReadonlySet<infer ItemType> ? Set<ItemType> : BaseType extends readonly unknown[] ? // Handle array WritableArray<BaseType> : // Handle object Simplify< // Pick just the keys that are not writable from the base type. Except< BaseType, Keys > & // Pick the keys that should be writable from the base type and make them writable by removing the `readonly` modifier from the key. { -readonly [KeyType in keyof Pick<BaseType, Keys>]: Pick< BaseType, Keys >[KeyType]; } >;
Create a type that strips
readonly
from the given type. Inverse ofReadonly<T>
.The 2nd argument will be ignored if the input type is not an object.
Note: This type can make readonly
Set
andMap
writable. This behavior is different fromReadonly<T>
(as of TypeScript 5.2.2). See: https://github.com/microsoft/TypeScript/issues/29655This can be used to [store and mutate options within a class](https://github.com/sindresorhus/pageres/blob/4a5d05fca19a5fbd2f53842cbf3eb7b1b63bddd2/source/index.ts#L72), [edit
readonly
objects within tests](https://stackoverflow.com/questions/50703834), [construct areadonly
object within a function](https://github.com/Microsoft/TypeScript/issues/24509), or to define a single model where the only thing that changes is whether or not some of the keys are writable.Example 1
import type {Writable} from 'type-fest';type Foo = {readonly a: number;readonly b: readonly string[]; // To show that only the mutability status of the properties, not their values, are affected.readonly c: boolean;};const writableFoo: Writable<Foo> = {a: 1, b: ['2'], c: true};writableFoo.a = 3;writableFoo.b[0] = 'new value'; // Will still fail as the value of property "b" is still a readonly type.writableFoo.b = ['something']; // Will work as the "b" property itself is no longer readonly.type SomeWritable = Writable<Foo, 'b' | 'c'>;// type SomeWritable = {// readonly a: number;// b: readonly string[]; // It's now writable. The type of the property remains unaffected.// c: boolean; // It's now writable.// }// Also supports arrayconst readonlyArray: readonly number[] = [1, 2, 3];readonlyArray.push(4); // Will fail as the array itself is readonly.const writableArray: Writable<typeof readonlyArray> = readonlyArray as Writable<typeof readonlyArray>;writableArray.push(4); // Will work as the array itself is now writable.Object
type WritableDeep
type WritableDeep<T> = T extends BuiltIns ? T : T extends (...arguments_: any[]) => unknown ? {} extends WritableObjectDeep<T> ? T : HasMultipleCallSignatures<T> extends true ? T : ((...arguments_: Parameters<T>) => ReturnType<T>) & WritableObjectDeep<T> : T extends ReadonlyMap<unknown, unknown> ? WritableMapDeep<T> : T extends ReadonlySet<unknown> ? WritableSetDeep<T> : T extends readonly unknown[] ? WritableArrayDeep<T> : T extends object ? WritableObjectDeep<T> : unknown;
Create a deeply mutable version of an
object
/ReadonlyMap
/ReadonlySet
/ReadonlyArray
type. The inverse ofReadonlyDeep<T>
. UseWritable<T>
if you only need one level deep.This can be used to [store and mutate options within a class](https://github.com/sindresorhus/pageres/blob/4a5d05fca19a5fbd2f53842cbf3eb7b1b63bddd2/source/index.ts#L72), [edit
readonly
objects within tests](https://stackoverflow.com/questions/50703834), [construct areadonly
object within a function](https://github.com/Microsoft/TypeScript/issues/24509), or to define a single model where the only thing that changes is whether or not some of the keys are writable.Example 1
import type {WritableDeep} from 'type-fest';type Foo = {readonly a: number;readonly b: readonly string[]; // To show that mutability is deeply affected.readonly c: boolean;};const writableDeepFoo: WritableDeep<Foo> = {a: 1, b: ['2'], c: true};writableDeepFoo.a = 3;writableDeepFoo.b[0] = 'new value';writableDeepFoo.b = ['something'];Note that types containing overloaded functions are not made deeply writable due to a [TypeScript limitation](https://github.com/microsoft/TypeScript/issues/29732).
See Also
Writable Object Array Set Map
type WritableKeysOf
type WritableKeysOf<T> = NonNullable< { [P in keyof T]: IsEqual< { [Q in P]: T[P] }, { readonly [Q in P]: T[P] } > extends false ? P : never; }[keyof T]>;
Extract all writable keys from the given type.
This is useful when you want to create a new type that contains writable keys only.
Example 1
import type {WritableKeysOf} from 'type-fest';interface User {name: string;surname: string;readonly id: number;}type UpdateRequest<Entity extends object> = Pick<Entity, WritableKeysOf<Entity>>;const update1: UpdateRequest<User> = {name: 'Alice',surname: 'Acme',};Utilities
Namespaces
namespace PackageJson
namespace PackageJson {}
interface NonStandardEntryPoints
interface NonStandardEntryPoints {}
property browser
browser?: string | Partial<Record<string, string | false>>;
A hint to JavaScript bundlers or component tools when packaging modules for client side use.
property esnext
esnext?: | string | { [moduleName: string]: string | undefined; main?: string; browser?: string; };
A module ID with untranspiled code that is the primary entry point to the program.
property module
module?: string;
An ECMAScript module ID that is the primary entry point to the program.
property sideEffects
sideEffects?: boolean | string[];
Denote which files in your project are "pure" and therefore safe for Webpack to prune if unused.
[Read more.](https://webpack.js.org/guides/tree-shaking/)
interface PackageJsonStandard
interface PackageJsonStandard {}
Type for [npm's
package.json
file](https://docs.npmjs.com/creating-a-package-json-file). Containing standard npm properties.
property author
author?: Person;
property bin
bin?: string | Partial<Record<string, string>>;
The executable files that should be installed into the
PATH
.
property bugs
bugs?: BugsLocation;
The URL to the package's issue tracker and/or the email address to which issues should be reported.
property bundledDependencies
bundledDependencies?: string[];
Package names that are bundled when the package is published.
property bundleDependencies
bundleDependencies?: string[];
Alias of
bundledDependencies
.
property config
config?: JsonObject;
Is used to set configuration parameters used in package scripts that persist across upgrades.
property contributors
contributors?: Person[];
A list of people who contributed to the package.
property cpu
cpu?: Array< LiteralUnion< | 'arm' | 'arm64' | 'ia32' | 'mips' | 'mipsel' | 'ppc' | 'ppc64' | 's390' | 's390x' | 'x32' | 'x64' | '!arm' | '!arm64' | '!ia32' | '!mips' | '!mipsel' | '!ppc' | '!ppc64' | '!s390' | '!s390x' | '!x32' | '!x64', string >>;
CPU architectures the module runs on.
property dependencies
dependencies?: Dependency;
The dependencies of the package.
property description
description?: string;
Package description, listed in
npm search
.
property devDependencies
devDependencies?: Dependency;
Additional tooling dependencies that are not required for the package to work. Usually test, build, or documentation tooling.
property directories
directories?: DirectoryLocations;
Indicates the structure of the package.
property engines
engines?: { [EngineName in 'npm' | 'node' | string]?: string;};
Engines that this package runs on.
property engineStrict
engineStrict?: boolean;
Deprecated
property exports
exports?: Exports;
Subpath exports to define entry points of the package.
[Read more.](https://nodejs.org/api/packages.html#subpath-exports)
property files
files?: string[];
The files included in the package.
property funding
funding?: | string | { /** The type of funding. */ type?: LiteralUnion< | 'github' | 'opencollective' | 'patreon' | 'individual' | 'foundation' | 'corporation', string >;
/** The URL to the funding page. */ url: string; };
Describes and notifies consumers of a package's monetary support information.
[Read more.](https://github.com/npm/rfcs/blob/latest/accepted/0017-add-funding-support.md)
property homepage
homepage?: LiteralUnion<'.', string>;
The URL to the package's homepage.
property imports
imports?: Imports;
Subpath imports to define internal package import maps that only apply to import specifiers from within the package itself.
[Read more.](https://nodejs.org/api/packages.html#subpath-imports)
property keywords
keywords?: string[];
Keywords associated with package, listed in
npm search
.
property license
license?: string;
The license for the package.
property licenses
licenses?: Array<{ type?: string; url?: string;}>;
The licenses for the package.
property main
main?: string;
The module ID that is the primary entry point to the program.
property maintainers
maintainers?: Person[];
A list of people who maintain the package.
property man
man?: string | string[];
Filenames to put in place for the
man
program to find.
property name
name?: string;
The name of the package.
property optionalDependencies
optionalDependencies?: Dependency;
Dependencies that are skipped if they fail to install.
property os
os?: Array< LiteralUnion< | 'aix' | 'darwin' | 'freebsd' | 'linux' | 'openbsd' | 'sunos' | 'win32' | '!aix' | '!darwin' | '!freebsd' | '!linux' | '!openbsd' | '!sunos' | '!win32', string >>;
Operating systems the module runs on.
property peerDependencies
peerDependencies?: Dependency;
Dependencies that will usually be required by the package user directly or via another dependency.
property peerDependenciesMeta
peerDependenciesMeta?: Partial<Record<string, { optional: true }>>;
Indicate peer dependencies that are optional.
property preferGlobal
preferGlobal?: boolean;
If set to
true
, a warning will be shown if package is installed locally. Useful if the package is primarily a command-line application that should be installed globally.Deprecated
property private
private?: boolean;
If set to
true
, then npm will refuse to publish it.
property publishConfig
publishConfig?: PublishConfig;
A set of config values that will be used at publish-time. It's especially handy to set the tag, registry or access, to ensure that a given package is not tagged with 'latest', published to the global public registry or that a scoped module is private by default.
property repository
repository?: | string | { type: string; url: string;
/** Relative path to package.json if it is placed in non-root directory (for example if it is part of a monorepo).
[Read more.](https://github.com/npm/rfcs/blob/latest/implemented/0010-monorepo-subdirectory-declaration.md) */ directory?: string; };
Location for the code repository.
property scripts
scripts?: Scripts;
Script commands that are run at various times in the lifecycle of the package. The key is the lifecycle event, and the value is the command to run at that point.
property type
type?: 'module' | 'commonjs';
Resolution algorithm for importing ".js" files from the package's scope.
[Read more.](https://nodejs.org/api/esm.html#esm_package_json_type_field)
property version
version?: string;
Package version, parseable by [
node-semver
](https://github.com/npm/node-semver).
property workspaces
workspaces?: WorkspacePattern[] | WorkspaceConfig;
Used to configure [npm workspaces](https://docs.npmjs.com/cli/using-npm/workspaces) / [Yarn workspaces](https://classic.yarnpkg.com/docs/workspaces/).
Workspaces allow you to manage multiple packages within the same repository in such a way that you only need to run your install command once in order to install all of them in a single pass.
Please note that the top-level
private
property ofpackage.json
**must** be set totrue
in order to use workspaces.
type BugsLocation
type BugsLocation = | string | { /** The URL to the package's issue tracker. */ url?: string;
/** The email address to which issues should be reported. */ email?: string; };
type Dependency
type Dependency = Partial<Record<string, string>>;
Dependencies of the package. The version range is a string which has one or more space-separated descriptors. Dependencies can also be identified with a tarball or Git URL.
type DirectoryLocations
type DirectoryLocations = { [directoryType: string]: JsonValue | undefined;
/** Location for executable scripts. Sugar to generate entries in the `bin` property by walking the folder. */ bin?: string;
/** Location for Markdown files. */ doc?: string;
/** Location for example scripts. */ example?: string;
/** Location for the bulk of the library. */ lib?: string;
/** Location for man pages. Sugar to generate a `man` array by walking the folder. */ man?: string;
/** Location for test files. */ test?: string;};
type ExportConditions
type ExportConditions = { // eslint-disable-line @typescript-eslint/consistent-indexed-object-style [condition: string]: Exports;};
A mapping of conditions and the paths to which they resolve.
type Exports
type Exports = null | string | Array<string | ExportConditions> | ExportConditions;
Entry points of a module, optionally with conditions and subpath exports.
type Imports
type Imports = { // eslint-disable-line @typescript-eslint/consistent-indexed-object-style [key: `#${string}`]: Exports;};
Import map entries of a module, optionally with conditions and subpath imports.
type JSPMConfiguration
type JSPMConfiguration = { /** JSPM configuration. */ jspm?: PackageJson;};
type NodeJsStandard
type NodeJsStandard = { /** Defines which package manager is expected to be used when working on the current project. It can set to any of the [supported package managers](https://nodejs.org/api/corepack.html#supported-package-managers), and will ensure that your teams use the exact same package manager versions without having to install anything else than Node.js.
__This field is currently experimental and needs to be opted-in; check the [Corepack](https://nodejs.org/api/corepack.html) page for details about the procedure.__
@example ```json { "packageManager": "<package manager name>@<version>" } ``` */ packageManager?: string;};
Type for [
package.json
file used by the Node.js runtime](https://nodejs.org/api/packages.html#nodejs-packagejson-field-definitions).
type Person
type Person = | string | { name: string; url?: string; email?: string; };
A person who has been involved in creating or maintaining the package.
type PublishConfig
type PublishConfig = { /** Additional, less common properties from the [npm docs on `publishConfig`](https://docs.npmjs.com/cli/v7/configuring-npm/package-json#publishconfig). */ [additionalProperties: string]: JsonValue | undefined;
/** When publishing scoped packages, the access level defaults to restricted. If you want your scoped package to be publicly viewable (and installable) set `--access=public`. The only valid values for access are public and restricted. Unscoped packages always have an access level of public. */ access?: 'public' | 'restricted';
/** The base URL of the npm registry.
Default: `'https://registry.npmjs.org/'` */ registry?: string;
/** The tag to publish the package under.
Default: `'latest'` */ tag?: string;};
type Scripts
type Scripts = { /** Run **before** the package is published (Also run on local `npm install` without any arguments). */ prepublish?: string;
/** Run both **before** the package is packed and published, and on local `npm install` without any arguments. This is run **after** `prepublish`, but **before** `prepublishOnly`. */ prepare?: string;
/** Run **before** the package is prepared and packed, **only** on `npm publish`. */ prepublishOnly?: string;
/** Run **before** a tarball is packed (on `npm pack`, `npm publish`, and when installing git dependencies). */ prepack?: string;
/** Run **after** the tarball has been generated and moved to its final destination. */ postpack?: string;
/** Run **after** the package is published. */ publish?: string;
/** Run **after** the package is published. */ postpublish?: string;
/** Run **before** the package is installed. */ preinstall?: string;
/** Run **after** the package is installed. */ install?: string;
/** Run **after** the package is installed and after `install`. */ postinstall?: string;
/** Run **before** the package is uninstalled and before `uninstall`. */ preuninstall?: string;
/** Run **before** the package is uninstalled. */ uninstall?: string;
/** Run **after** the package is uninstalled. */ postuninstall?: string;
/** Run **before** bump the package version and before `version`. */ preversion?: string;
/** Run **before** bump the package version. */ version?: string;
/** Run **after** bump the package version. */ postversion?: string;
/** Run with the `npm test` command, before `test`. */ pretest?: string;
/** Run with the `npm test` command. */ test?: string;
/** Run with the `npm test` command, after `test`. */ posttest?: string;
/** Run with the `npm stop` command, before `stop`. */ prestop?: string;
/** Run with the `npm stop` command. */ stop?: string;
/** Run with the `npm stop` command, after `stop`. */ poststop?: string;
/** Run with the `npm start` command, before `start`. */ prestart?: string;
/** Run with the `npm start` command. */ start?: string;
/** Run with the `npm start` command, after `start`. */ poststart?: string;
/** Run with the `npm restart` command, before `restart`. Note: `npm restart` will run the `stop` and `start` scripts if no `restart` script is provided. */ prerestart?: string;
/** Run with the `npm restart` command. Note: `npm restart` will run the `stop` and `start` scripts if no `restart` script is provided. */ restart?: string;
/** Run with the `npm restart` command, after `restart`. Note: `npm restart` will run the `stop` and `start` scripts if no `restart` script is provided. */ postrestart?: string;} & Partial<Record<string, string>>;
type TypeScriptConfiguration
type TypeScriptConfiguration = { /** Location of the bundled TypeScript declaration file. */ types?: string;
/** Version selection map of TypeScript. */ typesVersions?: Partial<Record<string, Partial<Record<string, string[]>>>>;
/** Location of the bundled TypeScript declaration file. Alias of `types`. */ typings?: string;};
type WorkspaceConfig
type WorkspaceConfig = { /** An array of workspace pattern strings which contain the workspace packages. */ packages?: WorkspacePattern[];
/** Designed to solve the problem of packages which break when their `node_modules` are moved to the root workspace directory - a process known as hoisting. For these packages, both within your workspace, and also some that have been installed via `node_modules`, it is important to have a mechanism for preventing the default Yarn workspace behavior. By adding workspace pattern strings here, Yarn will resume non-workspace behavior for any package which matches the defined patterns.
[Supported](https://classic.yarnpkg.com/blog/2018/02/15/nohoist/) by Yarn. [Not supported](https://github.com/npm/rfcs/issues/287) by npm. */ nohoist?: WorkspacePattern[];};
An alternative configuration for workspaces.
type WorkspacePattern
type WorkspacePattern = string;
A workspace pattern points to a directory or group of directories which contain packages that should be included in the workspace installation process.
The patterns are handled with [minimatch](https://github.com/isaacs/minimatch).
Example 1
docs
→ Include the docs directory and install its dependencies.packages/*
→ Include all nested directories within the packages directory, likepackages/cli
andpackages/core
.
type YarnConfiguration
type YarnConfiguration = { /** If your package only allows one version of a given dependency, and you’d like to enforce the same behavior as `yarn install --flat` on the command-line, set this to `true`.
Note that if your `package.json` contains `"flat": true` and other packages depend on yours (e.g. you are building a library rather than an app), those other packages will also need `"flat": true` in their `package.json` or be installed with `yarn install --flat` on the command-line. */ flat?: boolean;
/** Selective version resolutions. Allows the definition of custom package versions inside dependencies without manual edits in the `yarn.lock` file. */ resolutions?: Dependency;};
namespace TsConfigJson
namespace TsConfigJson {}
type CompilerOptions
type CompilerOptions = { /** The character set of the input files.
@default 'utf8' @deprecated This option will be removed in TypeScript 5.5. */ charset?: string;
/** Enables building for project references.
@default true */ composite?: boolean;
/** Generates corresponding d.ts files.
@default false */ declaration?: boolean;
/** Specify output directory for generated declaration files. */ declarationDir?: string;
/** Show diagnostic information.
@default false */ diagnostics?: boolean;
/** Reduce the number of projects loaded automatically by TypeScript.
@default false */ disableReferencedProjectLoad?: boolean;
/** Enforces using indexed accessors for keys declared using an indexed type.
@default false */ noPropertyAccessFromIndexSignature?: boolean;
/** Emit a UTF-8 Byte Order Mark (BOM) in the beginning of output files.
@default false */ emitBOM?: boolean;
/** Only emit `.d.ts` declaration files.
@default false */ emitDeclarationOnly?: boolean;
/** Differentiate between undefined and not present when type checking.
@default false */ exactOptionalPropertyTypes?: boolean;
/** Enable incremental compilation.
@default `composite` */ incremental?: boolean;
/** Specify file to store incremental compilation information.
@default '.tsbuildinfo' */ tsBuildInfoFile?: string;
/** Emit a single file with source maps instead of having a separate file.
@default false */ inlineSourceMap?: boolean;
/** Emit the source alongside the sourcemaps within a single file.
Requires `--inlineSourceMap` to be set.
@default false */ inlineSources?: boolean;
/** Specify what JSX code is generated.
@default 'preserve' */ jsx?: CompilerOptions.JSX;
/** Specifies the object invoked for `createElement` and `__spread` when targeting `'react'` JSX emit.
@default 'React' */ reactNamespace?: string;
/** Specify the JSX factory function to use when targeting React JSX emit, e.g. `React.createElement` or `h`.
@default 'React.createElement' */ jsxFactory?: string;
/** Specify the JSX Fragment reference used for fragments when targeting React JSX emit e.g. 'React.Fragment' or 'Fragment'.
@default 'React.Fragment' */ jsxFragmentFactory?: string;
/** Specify module specifier used to import the JSX factory functions when using `jsx: react-jsx*`.
@default 'react' */ jsxImportSource?: string;
/** Print names of files part of the compilation.
@default false */ listFiles?: boolean;
/** Specifies the location where debugger should locate map files instead of generated locations. */ mapRoot?: string;
/** Specify module code generation: 'None', 'CommonJS', 'AMD', 'System', 'UMD', 'ES6', 'ES2015' or 'ESNext'. Only 'AMD' and 'System' can be used in conjunction with `--outFile`. 'ES6' and 'ES2015' values may be used when targeting 'ES5' or lower.
@default ['ES3', 'ES5'].includes(target) ? 'CommonJS' : 'ES6' */ module?: CompilerOptions.Module;
/** Specifies module resolution strategy: 'node' (Node) or 'classic' (TypeScript pre 1.6).
@default ['AMD', 'System', 'ES6'].includes(module) ? 'classic' : 'node' */ moduleResolution?: CompilerOptions.ModuleResolution;
/** Specifies the end of line sequence to be used when emitting files: 'crlf' (Windows) or 'lf' (Unix).
@default 'LF' */ newLine?: CompilerOptions.NewLine;
/** Disable full type checking (only critical parse and emit errors will be reported).
@default false */ noCheck?: boolean;
/** Do not emit output.
@default false */ noEmit?: boolean;
/** Do not generate custom helper functions like `__extends` in compiled output.
@default false */ noEmitHelpers?: boolean;
/** Do not emit outputs if any type checking errors were reported.
@default false */ noEmitOnError?: boolean;
/** Warn on expressions and declarations with an implied 'any' type.
@default false */ noImplicitAny?: boolean;
/** Raise error on 'this' expressions with an implied any type.
@default false */ noImplicitThis?: boolean;
/** Report errors on unused locals.
@default false */ noUnusedLocals?: boolean;
/** Report errors on unused parameters.
@default false */ noUnusedParameters?: boolean;
/** Do not include the default library file (lib.d.ts).
@default false */ noLib?: boolean;
/** Do not add triple-slash references or module import targets to the list of compiled files.
@default false */ noResolve?: boolean;
/** Disable strict checking of generic signatures in function types.
@default false @deprecated This option will be removed in TypeScript 5.5. */ noStrictGenericChecks?: boolean;
/** @deprecated use `skipLibCheck` instead. */ skipDefaultLibCheck?: boolean;
/** Skip type checking of declaration files.
@default false */ skipLibCheck?: boolean;
/** Concatenate and emit output to single file. */ outFile?: string;
/** Redirect output structure to the directory. */ outDir?: string;
/** Do not erase const enum declarations in generated code.
@default false */ preserveConstEnums?: boolean;
/** Do not resolve symlinks to their real path; treat a symlinked file like a real one.
@default false */ preserveSymlinks?: boolean;
/** Keep outdated console output in watch mode instead of clearing the screen.
@default false */ preserveWatchOutput?: boolean;
/** Stylize errors and messages using color and context (experimental).
@default true // Unless piping to another program or redirecting output to a file. */ pretty?: boolean;
/** Do not emit comments to output.
@default false */ removeComments?: boolean;
/** Specifies the root directory of input files.
Use to control the output directory structure with `--outDir`. */ rootDir?: string;
/** Unconditionally emit imports for unresolved files.
@default false */ isolatedModules?: boolean;
/** Require sufficient annotation on exports so other tools can trivially generate declaration files.
@default false */ isolatedDeclarations?: boolean;
/** Generates corresponding '.map' file.
@default false */ sourceMap?: boolean;
/** Specifies the location where debugger should locate TypeScript files instead of source locations. */ sourceRoot?: string;
/** Suppress excess property checks for object literals.
@default false @deprecated This option will be removed in TypeScript 5.5. */ suppressExcessPropertyErrors?: boolean;
/** Suppress noImplicitAny errors for indexing objects lacking index signatures.
@default false @deprecated This option will be removed in TypeScript 5.5. */ suppressImplicitAnyIndexErrors?: boolean;
/** Do not emit declarations for code that has an `@internal` annotation. */ stripInternal?: boolean;
/** Specify ECMAScript target version.
@default 'es3' */ target?: CompilerOptions.Target;
/** Default catch clause variables as `unknown` instead of `any`.
@default false */ useUnknownInCatchVariables?: boolean;
/** Watch input files.
@default false @deprecated Use watchOptions instead. */ watch?: boolean;
/** Specify the polling strategy to use when the system runs out of or doesn't support native file watchers.
@deprecated Use watchOptions.fallbackPolling instead. */ fallbackPolling?: CompilerOptions.FallbackPolling;
/** Specify the strategy for watching directories under systems that lack recursive file-watching functionality.
@default 'useFsEvents' @deprecated Use watchOptions.watchDirectory instead. */ watchDirectory?: CompilerOptions.WatchDirectory;
/** Specify the strategy for watching individual files.
@default 'useFsEvents' @deprecated Use watchOptions.watchFile instead. */ watchFile?: CompilerOptions.WatchFile;
/** Enables experimental support for ES7 decorators.
@default false */ experimentalDecorators?: boolean;
/** Emit design-type metadata for decorated declarations in source.
@default false */ emitDecoratorMetadata?: boolean;
/** Do not report errors on unused labels.
@default false */ allowUnusedLabels?: boolean;
/** Report error when not all code paths in function return a value.
@default false */ noImplicitReturns?: boolean;
/** Add `undefined` to a type when accessed using an index.
@default false */ noUncheckedIndexedAccess?: boolean;
/** Report error if failed to find a source file for a side effect import.
@default false */ noUncheckedSideEffectImports?: boolean;
/** Report errors for fallthrough cases in switch statement.
@default false */ noFallthroughCasesInSwitch?: boolean;
/** Ensure overriding members in derived classes are marked with an override modifier.
@default false */ noImplicitOverride?: boolean;
/** Do not report errors on unreachable code.
@default false */ allowUnreachableCode?: boolean;
/** Disallow inconsistently-cased references to the same file.
@default true */ forceConsistentCasingInFileNames?: boolean;
/** Emit a v8 CPU profile of the compiler run for debugging.
@default 'profile.cpuprofile' */ generateCpuProfile?: string;
/** Generates an event trace and a list of types. */ generateTrace?: boolean;
/** Base directory to resolve non-relative module names. */ baseUrl?: string;
/** Specify path mapping to be computed relative to baseUrl option. */ paths?: Record<string, string[]>;
/** List of TypeScript language server plugins to load. */ plugins?: CompilerOptions.Plugin[];
/** Specify list of root directories to be used when resolving modules. */ rootDirs?: string[];
/** Specify list of directories for type definition files to be included. */ typeRoots?: string[];
/** Type declaration files to be included in compilation. */ types?: string[];
/** Enable tracing of the name resolution process.
@default false */ traceResolution?: boolean;
/** Allow javascript files to be compiled.
@default false */ allowJs?: boolean;
/** Do not truncate error messages.
@default false */ noErrorTruncation?: boolean;
/** Allow default imports from modules with no default export. This does not affect code emit, just typechecking.
@default module === 'system' || esModuleInterop */ allowSyntheticDefaultImports?: boolean;
/** Do not emit `'use strict'` directives in module output.
@default false @deprecated This option will be removed in TypeScript 5.5. */ noImplicitUseStrict?: boolean;
/** Enable to list all emitted files.
@default false */ listEmittedFiles?: boolean;
/** Disable size limit for JavaScript project.
@default false */ disableSizeLimit?: boolean;
/** List of library files to be included in the compilation. */ lib?: CompilerOptions.Lib[];
/** Enable strict null checks.
@default false */ strictNullChecks?: boolean;
/** The maximum dependency depth to search under `node_modules` and load JavaScript files. Only applicable with `--allowJs`.
@default 0 */ maxNodeModuleJsDepth?: number;
/** Import emit helpers (e.g. `__extends`, `__rest`, etc..) from tslib.
@default false */ importHelpers?: boolean;
/** Specify emit/checking behavior for imports that are only used for types.
@default 'remove' @deprecated Use `verbatimModuleSyntax` instead. */ importsNotUsedAsValues?: CompilerOptions.ImportsNotUsedAsValues;
/** Parse in strict mode and emit `'use strict'` for each source file.
@default false */ alwaysStrict?: boolean;
/** Enable all strict type checking options.
@default false */ strict?: boolean;
/** Enable stricter checking of of the `bind`, `call`, and `apply` methods on functions.
@default false */ strictBindCallApply?: boolean;
/** Provide full support for iterables in `for-of`, spread, and destructuring when targeting `ES5` or `ES3`.
@default false */ downlevelIteration?: boolean;
/** Report errors in `.js` files.
@default false */ checkJs?: boolean;
/** Built-in iterators are instantiated with a `TReturn` type of undefined instead of `any`.
@default false */ strictBuiltinIteratorReturn?: boolean;
/** Disable bivariant parameter checking for function types.
@default false */ strictFunctionTypes?: boolean;
/** Ensure non-undefined class properties are initialized in the constructor.
@default false */ strictPropertyInitialization?: boolean;
/** Emit `__importStar` and `__importDefault` helpers for runtime Babel ecosystem compatibility and enable `--allowSyntheticDefaultImports` for typesystem compatibility.
@default false */ esModuleInterop?: boolean;
/** Allow accessing UMD globals from modules.
@default false */ allowUmdGlobalAccess?: boolean;
/** Resolve `keyof` to string valued property names only (no numbers or symbols).
@default false @deprecated This option will be removed in TypeScript 5.5. */ keyofStringsOnly?: boolean;
/** Emit ECMAScript standard class fields.
@default false */ useDefineForClassFields?: boolean;
/** Generates a sourcemap for each corresponding `.d.ts` file.
@default false */ declarationMap?: boolean;
/** Include modules imported with `.json` extension.
@default false */ resolveJsonModule?: boolean;
/** Have recompiles in '--incremental' and '--watch' assume that changes within a file will only affect files directly depending on it.
@default false */ assumeChangesOnlyAffectDirectDependencies?: boolean;
/** Output more detailed compiler performance information after building.
@default false */ extendedDiagnostics?: boolean;
/** Print names of files that are part of the compilation and then stop processing.
@default false */ listFilesOnly?: boolean;
/** Disable preferring source files instead of declaration files when referencing composite projects.
@default true if composite, false otherwise */ disableSourceOfProjectReferenceRedirect?: boolean;
/** Opt a project out of multi-project reference checking when editing.
@default false */ disableSolutionSearching?: boolean;
/** Print names of files which TypeScript sees as a part of your project and the reason they are part of the compilation.
@default false */ explainFiles?: boolean;
/** Preserve unused imported values in the JavaScript output that would otherwise be removed.
@default true @deprecated Use `verbatimModuleSyntax` instead. */ preserveValueImports?: boolean;
/** List of file name suffixes to search when resolving a module. */ moduleSuffixes?: string[];
/** Control what method is used to detect module-format JS files.
@default 'auto' */ moduleDetection?: CompilerOptions.ModuleDetection;
/** Allows TypeScript files to import each other with a TypeScript-specific extension like .ts, .mts, or .tsx.
@default false */ allowImportingTsExtensions?: boolean;
/** Forces TypeScript to consult the exports field of package.json files if it ever reads from a package in node_modules.
@default false */ resolvePackageJsonExports?: boolean;
/** Forces TypeScript to consult the imports field of package.json files when performing a lookup that starts with # from a file whose ancestor directory contains a package.json.
@default false */ resolvePackageJsonImports?: boolean;
/** Suppress errors for file formats that TypeScript does not understand.
@default false */ allowArbitraryExtensions?: boolean;
/** List of additional conditions that should succeed when TypeScript resolves from package.json. */ customConditions?: string[];
/** Anything that uses the type modifier is dropped entirely.
@default false */ verbatimModuleSyntax?: boolean;
/** Suppress deprecation warnings */ ignoreDeprecations?: CompilerOptions.IgnoreDeprecations;};
type References
type References = { /** A normalized path on disk. */ path: string;
/** The path as the user originally wrote it. */ originalPath?: string;
/** True if the output of this reference should be prepended to the output of this project.
Only valid for `--outFile` compilations. @deprecated This option will be removed in TypeScript 5.5. */ prepend?: boolean;
/** True if it is intended that this reference form a circularity. */ circular?: boolean;};
type TypeAcquisition
type TypeAcquisition = { /** Enable auto type acquisition. */ enable?: boolean;
/** Specifies a list of type declarations to be included in auto type acquisition. For example, `['jquery', 'lodash']`. */ include?: string[];
/** Specifies a list of type declarations to be excluded from auto type acquisition. For example, `['jquery', 'lodash']`. */ exclude?: string[];
/** Disable infering what types should be added based on filenames in a project. */ disableFilenameBasedTypeAcquisition?: boolean;};
Auto type (.d.ts) acquisition options for this project.
type WatchOptions
type WatchOptions = { /** Specify the strategy for watching individual files.
@default 'UseFsEvents' */ watchFile?: WatchOptions.WatchFileKind | Lowercase<WatchOptions.WatchFileKind>;
/** Specify the strategy for watching directories under systems that lack recursive file-watching functionality.
@default 'UseFsEvents' */ watchDirectory?: | WatchOptions.WatchDirectoryKind | Lowercase<WatchOptions.WatchDirectoryKind>;
/** Specify the polling strategy to use when the system runs out of or doesn't support native file watchers. */ fallbackPolling?: | WatchOptions.PollingWatchKind | Lowercase<WatchOptions.PollingWatchKind>;
/** Enable synchronous updates on directory watchers for platforms that don't support recursive watching natively. */ synchronousWatchDirectory?: boolean;
/** Specifies a list of directories to exclude from watch */ excludeDirectories?: string[];
/** Specifies a list of files to exclude from watch */ excludeFiles?: string[];};
namespace TsConfigJson.CompilerOptions
namespace TsConfigJson.CompilerOptions {}
type FallbackPolling
type FallbackPolling = | 'fixedPollingInterval' | 'priorityPollingInterval' | 'dynamicPriorityPolling' | 'fixedInterval' | 'priorityInterval' | 'dynamicPriority' | 'fixedChunkSize';
type IgnoreDeprecations
type IgnoreDeprecations = '5.0';
type ImportsNotUsedAsValues
type ImportsNotUsedAsValues = 'remove' | 'preserve' | 'error';
type JSX
type JSX = 'preserve' | 'react' | 'react-jsx' | 'react-jsxdev' | 'react-native';
type Lib
type Lib = | 'ES5' | 'ES6' | 'ES7' | 'ES2015' | 'ES2015.Collection' | 'ES2015.Core' | 'ES2015.Generator' | 'ES2015.Iterable' | 'ES2015.Promise' | 'ES2015.Proxy' | 'ES2015.Reflect' | 'ES2015.Symbol.WellKnown' | 'ES2015.Symbol' | 'ES2016' | 'ES2016.Array.Include' | 'ES2017' | 'ES2017.ArrayBuffer' | 'ES2017.Date' | 'ES2017.Intl' | 'ES2017.Object' | 'ES2017.SharedMemory' | 'ES2017.String' | 'ES2017.TypedArrays' | 'ES2018' | 'ES2018.AsyncGenerator' | 'ES2018.AsyncIterable' | 'ES2018.Intl' | 'ES2018.Promise' | 'ES2018.Regexp' | 'ES2019' | 'ES2019.Array' | 'ES2019.Object' | 'ES2019.String' | 'ES2019.Symbol' | 'ES2020' | 'ES2020.BigInt' | 'ES2020.Promise' | 'ES2020.String' | 'ES2020.Symbol.WellKnown' | 'ES2020.SharedMemory' | 'ES2020.Intl' | 'ES2021' | 'ES2021.Intl' | 'ES2021.Promise' | 'ES2021.String' | 'ES2021.WeakRef' | 'ES2022' | 'ES2022.Array' | 'ES2022.Error' | 'ES2022.Intl' | 'ES2022.Object' | 'ES2022.RegExp' | 'ES2022.String' | 'ES2023' | 'ES2023.Array' | 'ES2023.Collection' | 'ES2023.Intl' | 'ES2024' | 'ES2024.ArrayBuffer' | 'ES2024.Collection' | 'ES2024.Object' | 'ES2024.Promise' | 'ES2024.Regexp' | 'ES2024.SharedMemory' | 'ES2024.String' | 'ESNext' | 'ESNext.Array' | 'ESNext.AsyncIterable' | 'ESNext.BigInt' | 'ESNext.Collection' | 'ESNext.Decorators' | 'ESNext.Disposable' | 'ESNext.Intl' | 'ESNext.Iterator' | 'ESNext.Promise' | 'ESNext.String' | 'ESNext.Symbol' | 'ESNext.WeakRef' | 'DOM' | 'DOM.Iterable' | 'ScriptHost' | 'WebWorker' | 'WebWorker.AsyncIterable' | 'WebWorker.ImportScripts' | 'WebWorker.Iterable' // Lowercase alternatives | 'es5' | 'es6' | 'es7' | 'es2015' | 'es2015.collection' | 'es2015.core' | 'es2015.generator' | 'es2015.iterable' | 'es2015.promise' | 'es2015.proxy' | 'es2015.reflect' | 'es2015.symbol.wellknown' | 'es2015.symbol' | 'es2016' | 'es2016.array.include' | 'es2017' | 'es2017.arraybuffer' | 'es2017.date' | 'es2017.intl' | 'es2017.object' | 'es2017.sharedmemory' | 'es2017.string' | 'es2017.typedarrays' | 'es2018' | 'es2018.asyncgenerator' | 'es2018.asynciterable' | 'es2018.intl' | 'es2018.promise' | 'es2018.regexp' | 'es2019' | 'es2019.array' | 'es2019.object' | 'es2019.string' | 'es2019.symbol' | 'es2020' | 'es2020.bigint' | 'es2020.promise' | 'es2020.string' | 'es2020.symbol.wellknown' | 'es2020.sharedmemory' | 'es2020.intl' | 'es2021' | 'es2021.intl' | 'es2021.promise' | 'es2021.string' | 'es2021.weakref' | 'es2022' | 'es2022.array' | 'es2022.error' | 'es2022.intl' | 'es2022.object' | 'es2022.regexp' | 'es2022.string' | 'es2023' | 'es2023.array' | 'es2023.collection' | 'es2023.intl' | 'es2024' | 'es2024.arraybuffer' | 'es2024.collection' | 'es2024.object' | 'es2024.promise' | 'es2024.regexp' | 'es2024.sharedmemory' | 'es2024.string' | 'esnext' | 'esnext.array' | 'esnext.asynciterable' | 'esnext.bigint' | 'esnext.collection' | 'esnext.decorators' | 'esnext.disposable' | 'esnext.intl' | 'esnext.iterator' | 'esnext.promise' | 'esnext.string' | 'esnext.symbol' | 'esnext.weakref' | 'dom' | 'dom.iterable' | 'scripthost' | 'webworker' | 'webworker.asynciterable' | 'webworker.importscripts' | 'webworker.iterable';
type Module
type Module = | 'CommonJS' | 'AMD' | 'System' | 'UMD' | 'ES6' | 'ES2015' | 'ES2020' | 'ES2022' | 'ESNext' | 'Node16' | 'NodeNext' | 'Preserve' | 'None' // Lowercase alternatives | 'commonjs' | 'amd' | 'system' | 'umd' | 'es6' | 'es2015' | 'es2020' | 'es2022' | 'esnext' | 'node16' | 'nodenext' | 'preserve' | 'none';
type ModuleDetection
type ModuleDetection = 'auto' | 'legacy' | 'force';
type ModuleResolution
type ModuleResolution = | 'classic' | 'node' | 'node10' | 'node16' | 'nodenext' | 'bundler' // Pascal-cased alternatives | 'Classic' | 'Node' | 'Node10' | 'Node16' | 'NodeNext' | 'Bundler';
type NewLine
type NewLine = | 'CRLF' | 'LF' // Lowercase alternatives | 'crlf' | 'lf';
type Plugin
type Plugin = { /** Plugin name. */ name: string;};
type Target
type Target = | 'ES3' | 'ES5' | 'ES6' | 'ES2015' | 'ES2016' | 'ES2017' | 'ES2018' | 'ES2019' | 'ES2020' | 'ES2021' | 'ES2022' | 'ES2023' | 'ES2024' | 'ESNext' // Lowercase alternatives | 'es3' | 'es5' | 'es6' | 'es2015' | 'es2016' | 'es2017' | 'es2018' | 'es2019' | 'es2020' | 'es2021' | 'es2022' | 'es2023' | 'es2024' | 'esnext';
type WatchDirectory
type WatchDirectory = | 'useFsEvents' | 'fixedPollingInterval' | 'dynamicPriorityPolling' | 'fixedChunkSizePolling';
type WatchFile
type WatchFile = | 'fixedPollingInterval' | 'priorityPollingInterval' | 'dynamicPriorityPolling' | 'useFsEvents' | 'useFsEventsOnParentDirectory' | 'fixedChunkSizePolling';
namespace TsConfigJson.WatchOptions
namespace TsConfigJson.WatchOptions {}
type PollingWatchKind
type PollingWatchKind = | 'FixedInterval' | 'PriorityInterval' | 'DynamicPriority' | 'FixedChunkSize';
type WatchDirectoryKind
type WatchDirectoryKind = | 'UseFsEvents' | 'FixedPollingInterval' | 'DynamicPriorityPolling' | 'FixedChunkSizePolling';
type WatchFileKind
type WatchFileKind = | 'FixedPollingInterval' | 'PriorityPollingInterval' | 'DynamicPriorityPolling' | 'FixedChunkSizePolling' | 'UseFsEvents' | 'UseFsEventsOnParentDirectory';
Package Files (148)
- index.d.ts
- source/and.d.ts
- source/array-indices.d.ts
- source/array-slice.d.ts
- source/array-splice.d.ts
- source/array-tail.d.ts
- source/array-values.d.ts
- source/arrayable.d.ts
- source/async-return-type.d.ts
- source/asyncify.d.ts
- source/basic.d.ts
- source/camel-case.d.ts
- source/camel-cased-properties-deep.d.ts
- source/camel-cased-properties.d.ts
- source/conditional-except.d.ts
- source/conditional-keys.d.ts
- source/conditional-pick-deep.d.ts
- source/conditional-pick.d.ts
- source/delimiter-case.d.ts
- source/delimiter-cased-properties-deep.d.ts
- source/delimiter-cased-properties.d.ts
- source/distributed-omit.d.ts
- source/distributed-pick.d.ts
- source/empty-object.d.ts
- source/entries.d.ts
- source/entry.d.ts
- source/exact.d.ts
- source/except.d.ts
- source/find-global-type.d.ts
- source/fixed-length-array.d.ts
- source/get.d.ts
- source/global-this.d.ts
- source/greater-than-or-equal.d.ts
- source/greater-than.d.ts
- source/has-optional-keys.d.ts
- source/has-readonly-keys.d.ts
- source/has-required-keys.d.ts
- source/has-writable-keys.d.ts
- source/if-any.d.ts
- source/if-empty-object.d.ts
- source/if-never.d.ts
- source/if-null.d.ts
- source/if-unknown.d.ts
- source/includes.d.ts
- source/int-closed-range.d.ts
- source/int-range.d.ts
- source/invariant-of.d.ts
- source/is-any.d.ts
- source/is-equal.d.ts
- source/is-float.d.ts
- source/is-integer.d.ts
- source/is-literal.d.ts
- source/is-never.d.ts
- source/is-null.d.ts
- source/is-tuple.d.ts
- source/is-unknown.d.ts
- source/iterable-element.d.ts
- source/join.d.ts
- source/jsonifiable.d.ts
- source/jsonify.d.ts
- source/kebab-case.d.ts
- source/kebab-cased-properties-deep.d.ts
- source/kebab-cased-properties.d.ts
- source/keys-of-union.d.ts
- source/last-array-element.d.ts
- source/less-than-or-equal.d.ts
- source/less-than.d.ts
- source/literal-to-primitive-deep.d.ts
- source/literal-to-primitive.d.ts
- source/literal-union.d.ts
- source/merge-deep.d.ts
- source/merge-exclusive.d.ts
- source/merge.d.ts
- source/multidimensional-array.d.ts
- source/multidimensional-readonly-array.d.ts
- source/non-empty-object.d.ts
- source/non-empty-tuple.d.ts
- source/numeric.d.ts
- source/observable-like.d.ts
- source/omit-deep.d.ts
- source/omit-index-signature.d.ts
- source/optional-keys-of.d.ts
- source/or.d.ts
- source/override-properties.d.ts
- source/package-json.d.ts
- source/partial-deep.d.ts
- source/partial-on-undefined-deep.d.ts
- source/pascal-case.d.ts
- source/pascal-cased-properties-deep.d.ts
- source/pascal-cased-properties.d.ts
- source/paths.d.ts
- source/pick-deep.d.ts
- source/pick-index-signature.d.ts
- source/primitive.d.ts
- source/promisable.d.ts
- source/readonly-deep.d.ts
- source/readonly-keys-of.d.ts
- source/readonly-tuple.d.ts
- source/replace.d.ts
- source/require-all-or-none.d.ts
- source/require-at-least-one.d.ts
- source/require-exactly-one.d.ts
- source/require-one-or-none.d.ts
- source/required-deep.d.ts
- source/required-keys-of.d.ts
- source/schema.d.ts
- source/screaming-snake-case.d.ts
- source/set-field-type.d.ts
- source/set-non-nullable.d.ts
- source/set-optional.d.ts
- source/set-parameter-type.d.ts
- source/set-readonly.d.ts
- source/set-required-deep.d.ts
- source/set-required.d.ts
- source/set-return-type.d.ts
- source/shared-union-fields-deep.d.ts
- source/shared-union-fields.d.ts
- source/simplify-deep.d.ts
- source/simplify.d.ts
- source/single-key-object.d.ts
- source/snake-case.d.ts
- source/snake-cased-properties-deep.d.ts
- source/snake-cased-properties.d.ts
- source/split.d.ts
- source/spread.d.ts
- source/string-key-of.d.ts
- source/string-repeat.d.ts
- source/string-slice.d.ts
- source/stringified.d.ts
- source/structured-cloneable.d.ts
- source/subtract.d.ts
- source/sum.d.ts
- source/tagged-union.d.ts
- source/tagged.d.ts
- source/trim.d.ts
- source/tsconfig-json.d.ts
- source/tuple-to-union.d.ts
- source/typed-array.d.ts
- source/undefined-on-partial-deep.d.ts
- source/union-to-intersection.d.ts
- source/union-to-tuple.d.ts
- source/unknown-array.d.ts
- source/unknown-record.d.ts
- source/value-of.d.ts
- source/words.d.ts
- source/writable-deep.d.ts
- source/writable-keys-of.d.ts
- source/writable.d.ts
Dependencies (0)
No dependencies.
Dev Dependencies (5)
Peer Dependencies (0)
No peer dependencies.
Badge
To add a badge like this oneto your package's README, use the codes available below.
You may also use Shields.io to create a custom badge linking to https://www.jsdocs.io/package/type-fest
.
- Markdown[![jsDocs.io](https://img.shields.io/badge/jsDocs.io-reference-blue)](https://www.jsdocs.io/package/type-fest)
- HTML<a href="https://www.jsdocs.io/package/type-fest"><img src="https://img.shields.io/badge/jsDocs.io-reference-blue" alt="jsDocs.io"></a>
- Updated .
Package analyzed in 8621 ms. - Missing or incorrect documentation? Open an issue for this package.