【问题标题】:typescript shot form notation of used Enum in interface接口中使用的枚举的打字稿拍摄形式表示法
【发布时间】:2021-03-03 17:40:55
【问题描述】:

有没有办法缩短这段代码? 基本上我不想写 A.a、A.b 等。一些我只想写 A 和类型脚本的方式会理解它需要是枚举值之一。

谢谢

enum A {
  a: 'one',
  b: 'two',
  c: 'three'
}

interface check {
  type: A.a|A.b|A.c
}

【问题讨论】:

    标签: javascript typescript enums interface


    【解决方案1】:

    枚举应按以下方式使用

    enum A {
      a: 'one',
      b: 'two',
      c: 'three'
    }
    
    interface check {
     type: A
    }
    

    这样,check.type 可以有值 'one', 'two', 'three'

    【讨论】:

    • 我做过一次,但我遇到了一些错误,所以我认为它不能那样工作。但它现在正在工作,我不知道我一开始做错了什么。
    猜你喜欢
    • 2018-09-23
    • 2020-05-09
    • 2017-02-03
    • 2020-01-29
    • 2019-12-27
    • 2020-12-09
    • 2020-04-30
    • 2017-03-06
    • 2017-06-08
    相关资源
    最近更新 更多