The Required<T> Utility
What you'll learn: How to transform all optional properties in a type into required ones using TypeScript's Required<T> utility.
The Core Idea
The Required<T> utility type takes any type T and returns a new type where every optional property becomes required. It's the exact opposite of Partial<T> — instead of making everything optional, it strips away all the ? modifiers.
Think of it like a contract that was previously lenient (some fields were optional) becoming strict (all fields must now be provided).
Basic Syntax
interface User {
id: number;
name?: string;
email?: string;
}
type CompleteUser = Required<User>;
// CompleteUser is now:
// {
// id: number;
// name: string; // no longer optional
// email: string; // no longer optional
// }
When You'd Use It
Sometimes you have a type with optional properties for flexibility, but in certain contexts you need all fields to be present:
interface Config {
apiUrl?: string;
timeout?: number;
retries?: number;
}
function validateConfig(config: Required<Config>): boolean {
// Now config.apiUrl, config.timeout, and config.retries
// are all guaranteed to exist
return config.apiUrl.length > 0 && config.timeout > 0;
}
How It Works
Required<T> removes the ? modifier from every property. It doesn't change the property types themselves — just their required/optional status.
Key Takeaway: Required<T> transforms optional properties into required ones, ensuring all fields must be provided — useful when you need strict completeness guarantees in specific contexts.