reactjs - React、Typescipt、条件 rops
问题描述
我有这种类型:
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
}
推荐阅读
- php - mysqli多查询分离
- react-native - Firebase 更新错误:存储在 Firestore 中的日期对象的行为将发生变化,您的应用可能会中断
- sql - sql - 检查列是否以特定的正则表达式开头
- android - 从 onPostExecute 获取 JSON 字符串到 OnCreateView
- php - Uncaught ReferenceError: $ is not defined - Laravel 5.5 + Laravel Mix
- react-native - 如何升级 react-native gradle 版本
- javascript - 无法动态创建 div 内容
- proxy - Node.js 中的 Google GAX / gRPC 不适用于 HTTP/S 代理
- javascript - 如何将点分隔字符串中的每个第一个单词大写?
- c - C:如何获取操作系统名称(Ubuntu、Debian、Centos、..)以及如果定义操作系统名称 = ubuntu