Warning: session_start(): open(/tmp/sess_e889615c980b72b2fdd023cf6672ec68, O_RDWR) failed: Disk quota exceeded (122) in /home/wvyrfnwn/learnshareit.com/wp-content/plugins/learnpress/inc/class-lp-page-controller.php on line 1007

Warning: session_start(): Failed to read session data: files (path: /tmp) in /home/wvyrfnwn/learnshareit.com/wp-content/plugins/learnpress/inc/class-lp-page-controller.php on line 1007

Warning: ftp_nlist() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 420

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_nlist() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 420

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 719

Warning: ftp_nlist() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 420

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_nlist() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 420

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 719

Warning: ftp_mkdir() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 562

Warning: ftp_nlist() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 420

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230
How to make an optional property required in TypeScript - LearnShareIT

How to make an optional property required in TypeScript

This post shares the method to make an optional property required in TypeScript. To make an optional property required, you must create a new utility type by extending an interface or a type alias. We will go through some examples in this article to give you a better understanding. Keep reading for detailed instructions.

Method to make an optional property Required in TypeScript

To make an optional property required, you must create a new utility type by extending an interface or a type alias and override the specific optional property you want to set to required. 

To illustrate, take a look at this example:

// Define the 'Car' interface with 4 properties
interface Car {
    model: string;
    brand: string;
    modelYear: number;
    isAvailable?: boolean;
}

// Set the 'isAvailable' property required
interface CarAvailableStatus extends Car {
    isAvailable: boolean;
}

// Initialize an object of type 'CarAvailableStatus'
const newCar: CarAvailableStatus = {
    model: "LX570",
    brand: "Lexus",
    modelYear: 2023,
    isAvailable: true,
};

console.log(newCar);

Output:

{
  "model": "LX570",
  "brand": "Lexus",
  "modelYear": 2023,
  "isAvailable": true
}

In the above example, we create the ‘CarAvailableStatus’ interface, which extends the ‘Car’ interface and then overrides the ‘isAvilable’ property, which is an optional property, setting it to required.

At this point, the object that is initialized based on the ‘CarAvailableStatus’ type must have all four properties: ‘model’, ‘brand’, ‘modelYear’, and ‘isAvailable’.

Indeed, if you omit any required properties, a type-checking error will arise. Try omitting the ‘isAvilable’ property to see if it’s actually a required property:

// Define the 'Car' interface with 4 properties
interface Car {
    model: string;
    brand: string;
    modelYear: number;
    isAvailable?: boolean;
}

// Set the 'isAvailable' property required
interface CarAvailableStatus extends Car {
    isAvailable: boolean;
}

// Initialize an object of type 'CarAvailableStatus'
const newCar: CarAvailableStatus = {
    model: "LX570",
    brand: "Lexus",
    modelYear: 2023,
};

console.log(newCar);

Error:

Property 'isAvailable' is missing in type '{ model: string; brand: string; modelYear: number; }' but required in type 'CarAvailableStatus'.

The above error message tells us that the ‘isAvilable’ property is required. However, we do not declare it in the ‘newCar’ object. This also means we had succeeded in making the ‘isAvilable’ property – which was optional before, required.

Summary

To sum up, we have gone through the method of how to make an optional property required in TypeScript. To make an optional property required, you must create a new utility type by extending an interface or a type alias. That’s all we want to convey. Hopefully, the information in this tutorial will be helpful to you.

Leave a Reply

Your email address will not be published. Required fields are marked *