gpt4 book ai didi

reactjs - react , typescript ,条件 Prop

转载 作者:行者123 更新时间:2023-12-04 07:18:41 25 4
gpt4 key购买 nike

我有这种类型:

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 ,条件 Prop ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/68635582/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com