TypeScript
/Intermediate
Required<T>
Definition
A built-in utility type that constructs a new type where all properties of the given Type `T` are set to required, removing any `?` modifiers.
Explain Like I'm New
The exact opposite of `Partial`. It looks at a form where everything is optional, and forcefully stamps 'REQUIRED' over every single field.
Real World Example
When a user provides an optional configuration object `ConfigOptions`, but inside your library's core logic, you merge it with defaults to guarantee every setting exists. Your internal functions will type the merged config as `Required<ConfigOptions>`.
Common Use Cases
- •Default prop merging
- •Strict internal function signatures
Interactive Example
interface Props { a?: number; b?: string; } const obj1: Props = { a: 5 }; // Valid, 'b' is optional // We enforce that ALL properties must be present const obj2: Required<Props> = { a: 5 }; // ERROR: Property 'b' is missing! const obj3: Required<Props> = { a: 5, b: "hello" }; // Valid!
Interview Questions
basic
- What is the opposite of `Partial`?
intermediate
- How does `Required` handle properties that were originally typed as `string | undefined`?