Warning: session_start(): open(/tmp/sess_be97b302f45cf63eec41eddfd2532180, 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 All Properties Optional Except One In Typescript - LearnShareIT

How To Make All Properties Optional Except One In Typescript

Make all properties Optional except one in TypeScript

Knowing how to make all properties optional except one in Typescript will make you handle type easier. So how to do it? Let’s go into detail now.

Make all properties optional except one in Typescript

Use Partial utility types

The Partial utility type will help you construct a type and make all properties optional. The Partial utility types will return a type with all properties of the given type.

Example:

// Create type
type inforDetail = {
    name: string,
    date: number,
    address: string,
    hair: string,
    favoriteSport: string
};
  
const emptyObj: Partial<inforDetail> = {};
console.log(emptyObj);

Output:

[LOG]: {}

As you see here, I create inforDetail type. Then I create an empty object and set the type inforDetail through Partial utility types. So that in my object, I won’t need to pass any properties because it is all optional. I will get an error if I do not pass in any value in normal type.

Example:

// Create type
type inforDetail = {
    name: string,
    date: number,
    address: string,
    hair: string,
    favoriteSport: string
};
  
const errorObj: inforDetail = {}; // The error here

Error:

Type '{}' is missing the following properties from type 'inforDetail': name, date, address, hair, favoriteSport

The error happens because if set simply inforDetail type, all properties will be required.

Make all properties optional except one

You can make all properties optional except one in Typescript by using Partial utility types and then add new property after that property is required.

Example:

// Create type
interface inforDetail {
    name: string,
    date: number,
    address: string,
    hair: string,
    favoriteSport: string
}
  
// Create a new type that have require property
interface newType extends Partial<inforDetail> {
    id: number;
}
  
const idRequire: newType = {
    id: 1,
};
  
const idRequire2: newType = {
    name: "Thomas",
    id: 2,
};
  
console.log(idRequire);
console.log(idRequire2);

Output:

[LOG]: { "id": 1 } 
[LOG]: { "name": "Thomas", "id": 2
}

Here I create a new interface that extends all types with optional by Partial utility type, then I add new id property, which becomes required property. If I do not pass in the id property, I will get the error.

Example:

// Create type
interface inforDetail {
    name: string,
    date: number,
    address: string,
    hair: string,
    favoriteSport: string
}
  
// Create a new type that requires property
interface newType extends Partial<inforDetail> {
    id: number;
}
  
const idRequire: newType = {}; // The error here

Error:

Property 'id' is missing in type '{}' but required in type 'newType'.

Summary

In this article, I showed you how to make all properties optional except one in Typescript. You can use Partial utility types to set all properties in the original type to become optional, then pass in new property I set to require by default.

Maybe you are interested:

Leave a Reply

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