Possible to extend types in Typescript?

前端 未结 4 936
隐瞒了意图╮
隐瞒了意图╮ 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条回答
  •  被撕碎了的回忆
    2020-12-02 07:29

    you can intersect types:

    type TypeA = {
        nameA: string;
    };
    type TypeB = {
        nameB: string;
    };
    export type TypeC = TypeA & TypeB;
    

    somewhere in you code you can now do:

    const some: TypeC = {
        nameB: 'B',
        nameA: 'A',
    };
    

提交回复
热议问题