英文:
Get the values of the typescript interface
问题
同事们!
帮我解决以下问题。
有几个具有预定义值的接口。例如:
interface ButtonTheme {
readonly Black: 'BLACK';
readonly White: 'WHITE';
readonly WhiteOutlined: 'WHITE-OUTLINED';
readonly Yellow: 'YELLOW';
}
interface ButtonType {
readonly Checkout: 'CHECKOUT';
readonly Pay: 'PAY';
readonly Simple: 'SIMPLE';
}
interface ButtonWidth {
readonly Auto: 'AUTO';
readonly Max: 'MAX';
readonly MaxImportant: 'MAX_IMPORTANT';
}
我需要一个包装器,它将创建一个由接口属性值组成的新类型。为了使其工作:
type Theme = Values<ButtonTheme>;
// 'BLACK' | 'WHITE' | 'WHITE-OUTLINED' | 'YELLOW'
type Type = Values<ButtonType>;
// 'CHECKOUT' | 'PAY' | 'SIMPLE'
type Width = Values<ButtonWidth>;
// 'AUTO' | 'MAX' | 'MAX_IMPORTANT'
请帮助创建类型 Values<T>
。
以前我通过 enum
解决了这个问题,但在当前情况下无法使用。
我在文档中找到了一种解决方案,但它只涉及键,而不涉及值。
英文:
colleagues!
Help me to solve the following problem.
There are several interfaces with predefined values. For example:
interface ButtonTheme {
readonly Black: 'BLACK';
readonly White: 'WHITE';
readonly WhiteOutlined: 'WHITE-OUTLINED';
readonly Yellow: 'YELLOW';
}
interface ButtonType {
readonly Checkout: 'CHECKOUT';
readonly Pay: 'PAY';
readonly Simple: 'SIMPLE';
}
interface ButtonWidth {
readonly Auto: 'AUTO';
readonly Max: 'MAX';
readonly MaxImportant: 'MAX_IMPORTANT';
}
I need a wrapper that will create a new type consisting of interface property values. To make it work:
type Theme = Values<ButtonTheme>;
// 'BLACK' | 'WHITE' | 'WHITE-OUTLINED' | 'YELLOW'
type Type = Values<ButtonType>;
// 'CHECKOUT' | 'PAY' | 'SIMPLE'
type Width = Values<ButtonWidth>;
// 'AUTO' | 'MAX' | 'MAX_IMPORTANT'
Help to create a type Values<T>, please
Previously I solved this problem via enum
, but in the current circumstances it cannot be used.
I found one solution in the documentation, but it refers only to keys, but not to values.
答案1
得分: 1
type Values
英文:
As always, there is a fairly simple solution to a complex question:
type Values<T> = T[keyof T];
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论