从流量迁移到打字稿:$键在TS相同呢?

问题描述 投票:1回答:1

流程有得心应手$Keys效用函数,可以从一个对象中提取键,并创建字符串文字的联合。所以,你可以这样做:

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

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

是否打字稿具有同等功能?我知道keyof,但只得到一个interface的钥匙,当流量可以得到一个对象的键。

typescript flowtype
1个回答
3
投票

相当于是keyof型操作符:

type FormType = keyof typeof foo
© www.soinside.com 2019 - 2024. All rights reserved.