[英]Create string literal type from object type's string values
We are currently using TS 2.3 (can't upgrade to 2.4 at the moment so using enums for this isn't an option). 我们目前正在使用TS 2.3(目前无法升级至2.4,因此无法使用枚举)。
We have an object type for something that looks like this 我们有一个看起来像这样的对象类型
type myObject = {
keyOn: 'valueOne';
keyTwo: 'valueTwo';
keyThree: 'valueThree;
}
Ideally, I would like to be able to create a type that is made up of the string literals that are the values on that object type. 理想情况下,我希望能够创建一个由字符串文字组成的类型,该字符串文字是该对象类型上的值。 So something like this: 所以像这样:
type valueLiterals = 'valueOne' | 'valueTwo' | 'valueThree'
Similar to what you would be able to produce for the keys by using 类似于您将可以使用生成的密钥
type objectKeys = keyOf myObject
Is there some version of a valueof
or similar that would allow automatic generation of the valueLiterals
type? 是否有一些版本valueof
或类似,将允许自动生成的valueLiterals
类型?
您可以对keyof T
使用类型查询:
type values = myObject [keyof myObject]
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.