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 *