Open In App

Typescript Required<Type> Utility Type

In this article, we are going to learn about the Required<Type> in Typescript. TypeScript is a popular programming language used for building scalable and robust applications. One of the features of Typescript is Required<Type> which is a built-in utility type that allows you to create a new type that makes all properties of the original type required. It means that any property of the original type that was previously optional becomes mandatory in the new type.

Syntax:



type Required<Type> = {
      [Property in keyof Type]-?: Type[Property];
};

Where – 

 



Approach: Let us see how we can use the Required<Type>  step-by-step:

Step 1: Define a new interface or type that includes some optional properties:

interface Person {
      name?: string;
      age?: number;
      address?: string;
}

Step 2: Define a new type that uses the Required<Type> utility type to make all properties of the Person interface required:

type RequiredPerson = Required<Person>;

Step 3: Now, RequiredPerson is a new type that includes all properties of the Person interface, but with the ? optional modifier removed. This means that all properties of RequiredPerson are now mandatory:

const person: RequiredPerson = {
      name: 'John',
      age: 30,
      address: '123 Main St.',
}; // This is valid

const invalidPerson: RequiredPerson = {
      name: 'Jane',
}; // This is invalid because age and address are missing

Step 4: Now, when all the properties are mandatory, we can use them for our requirements.

Example 1: In this example, we will convert an interface with optional properties to a required interface. First of all, we will create an interface Person containing name, age, and address. Here we have made the age and address properties optional. Now with the help of Required<Type> we can create a new interface that makes all the properties required, as we can see in the example below.




/* Define a type for a person object with 
optional `age` and `address` properties */
type Person = {
    name: string;
    age?: number;
    address?: string;
};
  
/* Use the `Required` utility type to create 
a new type where all properties of `Person` 
are required */
type RequiredPerson = Required<Person>;
  
/* Create an instance of the `RequiredPerson`
type with all required properties */
const person: RequiredPerson = {
    name: "John Doe",
    age: 30,
    address: "123 Main St",
};
  
// Log the `person` object to the console
console.log(person);

Output:

 

Example 2: In this example, we define an AddProps type with properties num1 and num2, here num2 is an optional property. Now we defined a function addNumbers that takes argument props of type Required<AddProps>. Now with the help of the Required utility type, all properties in the AddProps type are required when passed to the addNumbers function, then we call the addNumbers function and get the desired result.




/* Define a type for the properties of a 
function that adds two numbers, where 
`num2` is optional */
type AddProps = {
    num1: number;
    num2?: number;
};
  
/* Define a function that accepts an
object with all properties of 
`AddProps` set to required */
function addNumbers(props: Required<AddProps>) {
    /* Destructure the `num1` and `num2`
    properties from the `props` object */
    const { num1, num2 } = props;
  
    // Add the two numbers and return the result
    return num1 + num2;
}
  
/* Call the `addNumbers` function with an 
object that has all properties of 
`AddProps` set to required */
const result1 = addNumbers({ num1: 5, num2: 10 });
  
// Log the result of the function to the console
console.log(result1); // Output: 15

Output:

 

Conclusion: In this article, we have seen what is Required<Type> and its syntax. Since it can be used to create new types with all properties required. By using the Required<Type> type, you can ensure that all required properties are present and avoid unexpected runtime errors.


Article Tags :