Except - Create a type from an object type without certain keys.
Writable - Create a type that strips readonly from the given type. Inverse of Readonly<T>.
WritableDeep - Create a deeply mutable version of an object/ReadonlyMap/ReadonlySet/ReadonlyArray type. The inverse of ReadonlyDeep<T>. Use Writable<T> if you only need one level deep.
Merge - Merge two types into a new type. Keys of the second type overrides keys of the first type.
ObjectMerge - Merge two object types into a new object type, where keys from the second override keys from the first.
MergeDeep - Merge two objects or two arrays/tuples recursively into a new type.
MergeExclusive - Create a type that has mutually exclusive keys.
OverrideProperties - Override existing properties of the given type. Similar to Merge, but enforces that the original type has the properties you want to override.
RenameKeys - Rename keys in an object type according to a map of old-to-new names.
RequireAtLeastOne - Create a type that requires at least one of the given keys, while keeping the remaining keys as is.
RequireExactlyOne - Create a type that requires exactly one of the given keys and disallows more, while keeping the remaining keys as is.
RequireAllOrNone - Create a type that requires all of the given keys or none of the given keys, while keeping the remaining keys as is.
RequireOneOrNone - Create a type that requires exactly one of the given keys or none of the given keys, while keeping the remaining keys as is.
SingleKeyObject - Create a type that only accepts an object with a single key.
RequiredDeep - Create a deeply required version of another type.
PickDeep - Pick properties from a deeply-nested object.
OmitDeep - Omit properties from a deeply-nested object.
OmitIndexSignature - Omit any index signatures from the given object type, leaving only explicitly defined properties.
PickIndexSignature - Pick only index signatures from the given object type, leaving out all explicitly defined properties.
PartialDeep - Create a deeply optional version of another type.
PartialOnUndefinedDeep - Create a deep version of another type where all keys accepting undefined type are set to optional.
UndefinedOnPartialDeep - Create a deep version of another type where all optional keys are set to also accept undefined.
UnwrapPartial - Revert the Partial modifier on an object type.
UnwrapRequired - Revert the Required modifier on an object type.
ReadonlyDeep - Create a deeply immutable version of another type.
LiteralUnion - Create a union type by combining primitive types and literal types without sacrificing auto-completion in IDEs for the literal type part of the union.
UnwrapTagged - Get the untagged portion of a tagged type created with Tagged.
InvariantOf - Create an invariant type, which is a type that does not accept supertypes and subtypes.
SetOptional - Create a type that makes the given keys optional, while keeping the remaining keys as is.
SetReadonly - Create a type that makes the given keys readonly, while keeping the remaining keys as is.
SetRequired - Create a type that makes the given keys required, while keeping the remaining keys as is.
SetRequiredDeep - Create a type that makes the given keys required, with support for deeply nested key paths, while keeping the remaining keys as is.
SetNonNullable - Create a type that makes the given keys non-nullable, while keeping the remaining keys as is.
SetNonNullableDeep - Create a type that makes the specified keys non-nullable (removes null and undefined), supports deeply nested key paths, and leaves all other keys unchanged.
NonNullableDeep - Recursively removes null and undefined from the specified type.
ValueOf - Create a union of the given object's values, and optionally specify which keys to get the values from.
ConditionalKeys - Extract the keys from a type where the value type of the key extends the given Condition.
ConditionalPick - Pick keys from the shape that matches the given Condition.
ConditionalPickDeep - Pick keys recursively from the shape that matches the given condition.
ConditionalExcept - Exclude keys from a shape that matches the given Condition.
LiteralToPrimitiveDeep - Like LiteralToPrimitive except it converts literal types inside an object or array deeply.
Stringified - Create a type with the keys of the given type changed to string type.
IterableElement - Get the element type of an Iterable/AsyncIterable. For example, Array, Set, Map, generator, stream, etc.
Entry - Create a type that describes a single key-value pair produced when calling a collection’s entries method.
Entries - Create a type that describes the key-value pairs produced when calling a collection’s entries method.
SetReturnType - Create a function type with a return type of your choice and the same parameters as the given function type.
SetParameterType - Create a function that replaces some parameters with the given parameters.
Simplify - Useful to flatten the type output to improve type hints shown in editors. And also to transform an interface into a type to aide with assignability.
StructuredCloneable - Matches a value that can be losslessly cloned using structuredClone.
Async
Promisable - Create a type that represents either the value or the value wrapped in PromiseLike.
AsyncReturnType - Unwrap the return type of a function that returns a Promise.
Asyncify - Create an async version of the given function type, by boxing the return type in Promise while keeping the same parameter types.
String
Trim - Remove leading and trailing spaces from a string.
Split - Represents an array of strings split using a given character or character set.
Words - Split a string similar to Lodash's _.words() function.
Replace - Represents a string with some or all matches replaced by a replacement.
StringSlice - Returns a string slice of a given range, just like String#slice().
StringRepeat - Returns a new string which contains the specified number of copies of a given string, just like String#repeat().
RemovePrefix - Remove the specified prefix from the start of a string.
RemoveSuffix - Remove the specified suffix from the end of a string.
StringToArray - Returns an array of the characters of the specified string.
StringLength - Returns the length of the given string.
Array
Arrayable - Create a type that represents either the value or an array of the value.
Includes - Returns a boolean for whether the given array includes the given item.
Join - Join an array of strings and/or numbers using the given string as a delimiter.
ArraySlice - Returns an array slice of a given range, just like Array#slice().
ArrayElement - Extracts the element type of an array or tuple.
LastArrayElement - Extract the type of the last element of an array.
FixedLengthArray - Create a type that represents an array of the given type and length. The Array prototype methods that manipulate its length are excluded from the resulting type.
MultidimensionalArray - Create a type that represents a multidimensional array of the given type and dimension.
MultidimensionalReadonlyArray - Create a type that represents a multidimensional readonly array of the given type and dimension.
ReadonlyTuple - Create a type that represents a read-only tuple of the given type and length.
TupleToUnion - Convert a tuple/array into a union type of its elements.
UnionToTuple - Convert a union type into an unordered tuple type of its elements.
TupleToObject - Transforms a tuple into an object, mapping each tuple index to its corresponding type as a key-value pair.
TupleOf - Create a tuple type of the specified length with elements of the specified type.
SplitOnRestElement - Splits an array into three parts, where the first contains all elements before the rest element, the second is the rest element itself, and the third contains all elements after the rest element.
ExtendsStrict - A customizable version of extends for checking whether one type is assignable to another.
ExtractStrict - A stricter version of Extract<T, U> that ensures every member of U can successfully extract something from T.
ExcludeStrict - A stricter version of Exclude<T, U> that ensures every member of U can successfully exclude something from T.
ExcludeExactly - A stricter version of Exclude<T, U> that excludes types only when they are exactly identical.
ExtractExactly - A stricter version of Extract<T, U> that extracts types only when they are exactly identical.
Declined types
If we decline a type addition, we will make sure to document the better solution here.
Diff and Spread - The pull request author didn't provide any real-world use-cases and the PR went stale. If you think this type is useful, provide some real-world use-cases and we might reconsider.
Dictionary - You only save a few characters (Dictionary<number> vs Record<string, number>) from Record, which is more flexible and well-known. Also, you shouldn't use an object as a dictionary. We have Map in JavaScript now.
ExtractProperties and ExtractMethods - The types violate the single responsibility principle. Instead, refine your types into more granular type hierarchies.
Url2Json - Inferring search parameters from a URL string is a cute idea, but not very useful in practice, since search parameters are usually dynamic and defined separately.
Nullish - The type only saves a couple of characters, not everyone knows what "nullish" means, and I'm also trying to get away from null.
TitleCase - It's not solving a common need and is a better fit for a separate package.
PackageJsonExtras - There are too many possible configurations that can be put into package.json. If you would like to extend PackageJson to support an additional configuration in your project, please see the Extending existing types section below.
Alternative type names
If you know one of our types by a different name, add it here for discovery.
PackageJson - There are a lot of tools that place extra configurations inside the package.json file. You can extend PackageJson to support these additional configurations.
import type {PackageJson as BasePackageJson} from 'type-fest';import type {Linter} from 'eslint';type PackageJson = BasePackageJson & {eslintConfig?: Linter.Config};
Related
typed-query-selector - Enhances document.querySelector and document.querySelectorAll with a template literal type that matches element types returned from an HTML element query selector.
interface User { id: number; name: string; age: number;}class UserApiService { async fetchUser(userId: number): Promise<User> { // Fetch the user data from the database. // The actual implementation might look like this: // const response = await fetch('/api/user/${userId}'); // const data = response.json(); // return data; return { id: 1, name: 'John Doe', age: 30 }; }}type FetchedUser = Awaited<ReturnType<UserApiService['fetchUser']>>;async function handleUserData(apiService: UserApiService, userId: number) { try { const user: FetchedUser = await apiService.fetchUser(userId); // After fetching user data, you can perform various actions such as updating the user interface, // caching the data for future use, or making additional API requests as needed. } catch (error) { // Error handling }}const userApiService = new UserApiService();handleUserData(userApiService, 1);