Syntax
Partial<Type>
Required<Type>Examples
Partial<T> for Update Functions
A very common real-world use case - allowing partial updates to an object.
interface User {
id: number;
name: string;
email: string;
}
function updateUser(id: number, updates: Partial<User>): void {
// updates can include ANY subset of User's properties
console.log(`Updating user ${id}:`, updates);
}
updateUser(1, { name: "New Name" }); // OK - only updating name
updateUser(1, { email: "new@example.com" }); // OK - only updating emailRequired<T> to Enforce Completeness
Ensuring an object has every property populated, even ones that started optional.
interface Config {
host?: string;
port?: number;
timeout?: number;
}
function startServer(config: Required<Config>): void {
// every property is guaranteed to be present here, not optional
console.log(`Starting on ${config.host}:${config.port}`);
}
// startServer({ host: "localhost" }); // Error - port and timeout are missing
startServer({ host: "localhost", port: 3000, timeout: 5000 }); // OKBest practices
- Use Partial<T> for update/patch functions where the caller should be able to supply any subset of an object's fields
- Use Required<T> at boundaries where a fully-populated object is genuinely necessary, even if the original type allows optional fields elsewhere
- Avoid using Partial<T> for creating brand-new objects when most fields are actually required - it can hide missing-field bugs that a stricter type would catch
- Combine Partial<T> with a spread merge pattern ({ ...existing, ...updates }) for a clean, type-safe update implementation
At a glance
- Purpose
- Static types for JavaScript
- File extension
- .ts ยท .tsx
- Runs in
- Compiled to JavaScript
- Usually used with
- JavaScript and its ecosystem
Specifications & further reading
Related TypeScript documentation
Pick<T> and Omit<T>
Pick<T, Keys> constructs a new type by selecting only the specified properties from T. Omit<T, Keys> does the reverse, constructing a new type with all properties of T except the specified ones. Both are extremely common for deriving smaller, focused types from a larger base type without duplicating the definition.Record<K, V>
Record<Keys, ValueType> constructs an object type with a specific set of keys, all mapped to the same value type. It is the concise, standard way to type dictionary-like objects, and is often clearer than writing an equivalent index signature by hand.Readonly<T>
Readonly<T> constructs a new type where every property of T is marked readonly, preventing reassignment after the object is created. It is a shallow transformation - nested objects within a Readonly<T> are not automatically deep-frozen, only the top-level properties are protected from reassignment.ReturnType<T> and Parameters<T>
ReturnType<T> extracts the return type of a function type, and Parameters<T> extracts its parameter types as a tuple. Both are especially useful for deriving types from functions you do not directly control, like third-party library functions, without manually duplicating their signatures.
Pick<T, Keys> constructs a new type by selecting only the specified properties from T. Omit<T, Keys> does the reverse, constructing a new type with all properties of T except the specified ones. Both are extremely common for deriving smaller, focused types from a larger base type without duplicating the definition.Record<K, V>
Record<Keys, ValueType> constructs an object type with a specific set of keys, all mapped to the same value type. It is the concise, standard way to type dictionary-like objects, and is often clearer than writing an equivalent index signature by hand.Readonly<T>
Readonly<T> constructs a new type where every property of T is marked readonly, preventing reassignment after the object is created. It is a shallow transformation - nested objects within a Readonly<T> are not automatically deep-frozen, only the top-level properties are protected from reassignment.ReturnType<T> and Parameters<T>
ReturnType<T> extracts the return type of a function type, and Parameters<T> extracts its parameter types as a tuple. Both are especially useful for deriving types from functions you do not directly control, like third-party library functions, without manually duplicating their signatures.