Possible to extend types in Typescript?

前端 未结 4 926
隐瞒了意图╮
隐瞒了意图╮ 2020-12-02 06:55

Say I have the following type:

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

I now want to extend this type, i

4条回答
  •  -上瘾入骨i
    2020-12-02 07:33

    What you are trying to achieve is equivalent to

    interface Event {
       name: string;
       dateCreated: string;
       type: string;
    }
    
    interface UserEvent extends Event {
       UserId: string; 
    }
    

    The way you defined the types does not allow for specifying inheritance, however you can achieve something similar using intersection types, as artem pointed out.

提交回复
热议问题