【发布时间】:2019-07-08 03:57:59
【问题描述】:
我需要使用过滤器函数找到一个键数组以传递给我的 UI 组件。但是 TS warn 总是会引发类型错误。
我试过了
['is_in_building', 'has_outside_access'].filter((v: keyof Unit) => Boolean(unit[v]));
但还是不行。
is_in_building、has_outside_access 确实存在于 Unit 类型中。而且filter函数的第一个参数v不能是别的东西。
type.ts
type Unit = {
is_in_building: boolean,
has_outside_access: boolean,
other_keys: boolean,
}
App.tsx
<Checkbox.Group
value={['is_in_building', 'has_outside_access'].filter(
v => Boolean(unit[v]) //ts error
)}
>
<Checkbox value="is_in_building">In Building</Checkbox>
<Checkbox value="has_outside_access">Outside Access</Checkbox>
</Checkbox.Group>
错误:(303, 20) TS7053:元素隐式具有“任何”类型,因为“字符串”类型的表达式不能用于索引类型“增强单元”。 在“EnhancedUnit”类型上找不到带有“字符串”类型参数的索引签名。
谁能给我一些建议?
【问题讨论】:
-
什么是增强单元?
-
这是单位。我简化了一些代码
标签: reactjs typescript antd