Typescript - Allowed values for a property

前端 未结 3 473
北荒
北荒 2021-01-03 19:20

What it the best way in Typescript to only allow a number of value for a property ?

class Foo {
    public type:string         


        
3条回答
  •  渐次进展
    2021-01-03 19:53

    const TYPES = ['a', 'b', 'c'] as const; // TS3.4 syntax
    type yourType = typeof TYPES[number]; // 'a'|'b'|'c';
    

提交回复
热议问题