Create typescript interface as union of other interfaces

前端 未结 1 924
小蘑菇
小蘑菇 2021-01-02 01:14

I have a list of interfaces that extends one basic interface.

I have also some functions that can accept any of these interfaces.

I would like to create a ne

相关标签:
1条回答
  • 2021-01-02 01:51

    So, after some digging in the Typescript documentation I've found what I was looking for: type aliases.

    I can declare a new type like type C = A | B;

    Then in function I use type guards to access the right thing:

    function isA(param: C): param is A {
        return param.type === A;
    }
    
    function X(param1: C) {
      if (isA(param1)) //things related to A
      else //things related to B
    };
    
    0 讨论(0)
提交回复
热议问题