【发布时间】:2020-04-30 04:12:04
【问题描述】:
我有一个映射数组的函数。它看起来像这样:
// opt is either `statusA` | `statusB`
options.map((opt: keyof StatusType) => {
const activeStatus = statusCollection[opt]; //typescript doesnt scream `of any type` error
...
其中,StatusType 看起来像:
type Keyys = 'statusA' | 'statusB';
export type StatusType = {
[key in Keyys]: boolean;
};
我这样做是因为我希望能够从对象中检索计算属性,以存储在上面的“activeStatus”变量中。
如果选项始终为statusType 类型,这将正常工作,但是,它是一个通用函数,我希望能够传入其他集合。我将如何定义 type 来替换 'StatusType' 使其更通用并正确利用 keyof 功能。
【问题讨论】:
标签: javascript typescript types