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
Runtime & versions
Compiled to JavaScript. Feature availability depends on the language and runtime version used by your project; consult the official reference below.
TypeScript Handbook