Required<T>
Constructs a type with all properties of T set to required, removing optional modifiers from every property.
Definition
type Required<T> = {
[P in keyof T]-?: T[P];
};Examples
interface Props {
name?: string;
age?: number;
email?: string;
}
type StrictProps = Required<Props>;
// { name: string; age: number; email: string; }
const props: StrictProps = {
name: "Alice",
age: 30,
email: "[email protected]",
};interface UserInput {
username?: string;
password?: string;
}
function validateUser(input: UserInput): Required<UserInput> {
if (!input.username) throw new Error("Username is required");
if (!input.password) throw new Error("Password is required");
return input as Required<UserInput>;
}
const validated = validateUser({ username: "bob", password: "secret" });interface AppConfig {
apiUrl?: string;
retries?: number;
verbose?: boolean;
}
function initApp(config: Required<AppConfig>) {
console.log(`Connecting to ${config.apiUrl} with ${config.retries} retries`);
}
initApp({ apiUrl: "https://api.example.com", retries: 3, verbose: true });Common Use Cases
- 1Enforcing all fields after validation
- 2Converting partial inputs to strict internal types
- 3Ensuring complete configuration before initialization
- 4Type guards that narrow from optional to required
- 5API response types where all fields are guaranteed
Understanding Required<T>
Required<T> is the inverse of Partial<T>. While Partial makes every property optional, Required strips the optional modifier from all properties of T, making every field mandatory. The -? syntax in its definition is TypeScript's way of removing the optional modifier.
This utility type is especially useful at validation boundaries. When data enters your system—from user input, API responses, or configuration files—the shape is often loosely typed with optional fields. After validation confirms that all necessary fields are present, you can express this guarantee in the type system by returning Required<T>.
Consider a multi-step form wizard. During the editing phase, every field might be optional since the user hasn't filled them all in yet. But at the final submission step, you validate that all fields are present and return a Required<FormData>. Downstream code then benefits from knowing every field exists without needing additional null checks.
Required is also valuable when working with third-party types. If a library exports an interface with optional fields but your application always provides all of them, wrapping the type in Required communicates this intent clearly and catches missing fields at compile time.
Like Partial, Required only operates on top-level properties. Nested optional properties within sub-objects remain untouched. If you need deep required behavior, you'll need a custom recursive type such as DeepRequired.
Related Types
Partial<T>Constructs a type with all properties of T set to optional, allowing you to provide only a subset of the properties.
Readonly<T>Constructs a type with all properties of T set to readonly, preventing reassignment of properties after creation.
NonNullable<T>Constructs a type by excluding null and undefined from T, ensuring the resulting type is always a definite value.
More Mapping Types
Explore TypeScript Types
Browse our complete reference of 30 TypeScript utility types with definitions, examples, and explanations.