Model completed configuration
A builder or parser has produced a fully populated configuration object and the next API should reject incomplete values.
TypeScript · Type Alias
Required<Type> transforms an object type so all of its properties are required. It is the inverse counterpart to Partial<Type>.
Required<Type> creates a new object type from Type in which optional properties are treated as required. It changes the type-level property modifiers, so values assigned to the resulting type must include every property that Type declares.
Required<Type>| Name | Type | Description |
|---|---|---|
Type | type | The object type whose properties should all become required. |
A type containing all properties of Type with required property modifiers.
interface Props {
title?: string;
count?: number;
}
const complete: Required<Props> = {
title: "Dashboard",
count: 0,
};
console.log(complete);Every property must be supplied because Required<Props> removes optionality from title and count.
A builder or parser has produced a fully populated configuration object and the next API should reject incomplete values.
A shared optional input type is narrowed into a guaranteed-complete internal representation after validation.
A component or service contract must require every declared field even though an external-facing type permits omissions.
Required<Type> constructs a type whose properties are all required. For an interface with optional members, assigning an object to Required<Interface> requires values for every member.
Follow-up: How does Required differ from NonNullable? NonNullable removes null and undefined from a type, whereas Required changes property optionality.
It operates on the properties declared by Type and removes their optional property modifier. It does not create runtime defaults or insert missing values into an object.
Follow-up: What happens at runtime? Nothing: Required is erased during compilation, so validation or defaulting must be implemented separately.
Required was released in TypeScript 2.8 and is available as a global utility type. Its purpose is to make all properties of the supplied type required.
Follow-up: Why is it useful in a large codebase? It can express a completed configuration or normalized object after a separate construction step.
PartialReadonlyPickRecordNonNullable