【问题标题】:Migrating from Flow to Typescript: $Keys equivalent in TS?从 Flow 迁移到 Typescript:TS 中的 $Keys 等价物?
【发布时间】:2019-07-02 21:05:29
【问题描述】:

Flow 有方便的$Keys 实用函数,可以从对象中提取键并创建字符串文字的联合。所以你可以这样做:

const foo = {
   input: Input,
   button: Button,
   select: Select
}

type FormType = $Keys<typeof foo>      // FormType now is: 'input' | 'button' | 'select'

Typescript 有等效的功能吗?我知道keyof,但它只能获取interface 的键,而Flow 可以获取对象的键。

【问题讨论】:

    标签: typescript flowtype


    【解决方案1】:

    等价于keyof类型运算符:

    type FormType = keyof typeof foo
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2010-09-27
      • 2020-05-24
      • 2017-04-12
      • 1970-01-01
      • 2022-09-26
      • 2010-10-16
      • 1970-01-01
      • 2018-12-07
      相关资源
      最近更新 更多