我有这种类型:
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; }'.
这是怎么回事?如何解决?
最佳答案
您应该检查 inputType === 'dropdown-menu' 是否:
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;
};
declare var obj: InfoItemProps;
if (obj.inputType === 'dropdown-menu') {
obj.options // ok
}
关于reactjs - React、Typescript、条件属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/68635582/