Sometimes, we want to extend types in TypeScript.
In this article, we’ll look at how to extend types in TypeScript.
How to extend types in TypeScript?
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.