【发布时间】:2020-09-06 14:43:37
【问题描述】:
这里我有以下代码
const selectOptions = {
mode: isTag ? 'tags' : 'combobox',
... //other stuff
}
return (<Select {...selectOptions}>...</Select>);
mode 被 TypeScript 推断为字符串,但 Select 只接受 type ModeOption = "default" | "multiple" | "tags" | "combobox",因此出现错误。
我目前的方法是mode: (isTag ? 'tags' : 'combobox') as ModeOption。但是我想避免使用 as,有没有更好的方法?如何定义一个对象字面量只有几个属性的类型?
【问题讨论】:
-
你可以访问 Select 的 props 类型吗?然后你可以做例如
const selectOptions: SelectProps = { ... }.
标签: typescript antd