【发布时间】:2021-08-03 22:37:42
【问题描述】:
我有这种类型:
export type InfoItemProps = (
| { inputType: 'dropdown-menu'; options: string[] }
| { inputType: 'text' }
| { inputType: 'date' }
| { inputType: 'text-aria' }
| { inputType: 'phone-number' }
) & {
name: string;
labelText: string;
value: string;
editValue: string;
editMode: boolean;
onEditValueChange: (inputName: string, value: string) => void;
};
但每当我尝试访问选项时,它都会抱怨:
Property 'options' does not exist on type
'{ inputType: "text"; } & { name: string; labelText: string; value: string;
editValue: string; editMode: boolean; onEditValueChange: (inputName: string, value: string) => void; }'.
发生了什么事?如何解决?
【问题讨论】:
-
您只能在条件语句/开关中访问选项,例如
const info: InfoItemProps.....if(info.inputType === 'dropdown-menu') { // you can access options here } -
请给你看 tsconfig。你的代码对我有用
标签: reactjs typescript