When it comes to the keyword extends
, it can only be utilized for interfaces and classes specifically.
If you are looking to define a type with additional properties, consider utilizing an intersection type:
type UserEvent = Event & {UserId: string}
UPDATE: With TypeScript 2.2, there is now the possibility of extending an object-like type within an interface, given that the type meets certain criteria. Check out this link for more information:
type Event = {
name: string;
dateCreated: string;
type: string;
}
interface UserEvent extends Event {
UserId: string;
}
Keep in mind that the UserEvent
must be declared as an interface rather than a type
if you intend to use the extends
syntax.
Unfortunately, using extend
with arbitrary types is still not supported. This means that it won't work if Event
is a type parameter lacking any constraints.