Categories
TypeScript Answers

How to extend types in TypeScript?

Spread the love

To extend types in TypeScript, we can use the extends keyword.

For instance, we write

type Event = {
  name: string;
  dateCreated: string;
  type: string;
};

interface UserEvent extends Event {
  UserId: string;
}

to create the UserEvent interface that extends the Event type.

We extend it by adding the string UserId field in UserEvent and inheriting the rest from Event.

Conclusion

To extend types in TypeScript, we can use the extends keyword.

By John Au-Yeung

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

Leave a Reply

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