Skip to main content

What does Required<T> do?

1. What Required<T> does

Required<T> is a built-in Utility Type that makes all properties of type T required (that is, it removes ?).


Example

javascript
interface User { id: number; name?: string; email?: string; } type FullUser = Required<User>;

Now FullUser looks like this:

javascript
{ id: number; name: string; // no longer optional email: string; // no longer optional }

All fields are now required, regardless of whether they were ?.


2. How Required<T> is implemented

Inside TypeScript it is defined roughly like this:

javascript
type Required<T> = { [P in keyof T]-?: T[P]; };

Explanation:

  • keyof T walks over all keys of type T;
  • -? is an operator that removes the ? modifier (makes the property required);
  • T[P] keeps the original value type.

3. How Required<T> is the opposite of Partial<T>

UtilityWhat it doesExample result
Partial<T>Makes all properties optional{ id?: number; name?: string }
Required<T>Makes all properties required{ id: number; name: string }

A comparison example

javascript
interface Profile { username?: string; age?: number; } // Partial loosens the requirements: type OptionalProfile = Partial<Profile>; // { username?: string; age?: number } // Required tightens the requirements: type StrictProfile = Required<Profile>; // { username: string; age: number }

4. How Required<T> affects optional properties

It forcibly removes the ? mark from all properties of the interface. Even if a property was optional, it becomes required when Required is applied.


Example

javascript
interface Config { port?: number; host?: string; } type FullConfig = Required<Config>; const cfg: FullConfig = { port: 8080, host: "localhost", }; // everything must be specified

If you try to skip a field:

javascript
const bad: FullConfig = { port: 8080 }; // Error: property "host" is missing

5. Partial and Required can be combined

Sometimes you need to "bring back" the requirement for only some of the fields:

javascript
interface User { id: number; name?: string; email?: string; } // All fields optional type OptionalUser = Partial<User>; // Only name is required again type PartiallyRequired = Required<Pick<User, "name">> & Partial<User>;

Now name is required, and the rest are optional.


6. A practical example

Usage in form validation:

javascript
interface FormData { name?: string; email?: string; age?: number; } function validate(data: Required<FormData>) { // all fields are guaranteed to be present console.log(data.name.toUpperCase()); } validate({ name: "Tim", email: "tim@mail.com", age: 30 }); // OK validate({ name: "Tim" }); // Error - email and age are required

Summary

QuestionAnswer
What Required<T> doesMakes all properties required, removing ?
How it differs from Partial<T>It is the opposite effect - Partial makes everything optional, Required makes everything required
How it affects optional propertiesTurns them into required properties (? is removed from every key)

Short Answer

Interview ready
Premium

A concise answer to help you respond confidently on this topic during an interview.