Extending union type alias in typescript?

前端 未结 3 1920
隐瞒了意图╮
隐瞒了意图╮ 2020-12-16 15:54

I\'m trying to limit some string fields to be only of certain values at compile time. The problem is that those values should be extendable. Here\'s a simplified example:

3条回答
  •  温柔的废话
    2020-12-16 16:14

    To achieve what you need, you can use intersection via the & symbol.

    type Foobar = 'FOO' | 'BAR';
    type FoobarBaz = Foobar | & 'BAZ'; // or: 'BAZ' | & Foobar
    

提交回复
热议问题