Categories
TypeScript Answers

How to make all properties within a TypeScript interface optional?

Spread the love

To make all properties within a TypeScript interface optional, we can use the Partial type.

For instance, we write

interface Asset {
  id: string;
  internalId: string;
  usage: number;
}

interface AssetDraft extends Partial<Asset> {}

to create the AssetDraft interface that inherits from Partial<Asset>.

Partial<Asset> is a type that makes the properties in the Asset interface optional.

Conclusion

To make all properties within a TypeScript interface optional, we can use the Partial type.

By John Au-Yeung

Web developer specializing in React, Vue, and front end development.

Leave a Reply

Your email address will not be published.