【发布时间】:2019-11-28 06:56:09
【问题描述】:
我有以下实用方法。这需要两个参数,inputValue 和 selectOptions。
export interface SelectType {
label: string;
value: string;
}
const checkCaseSensitiveSelectUtil = (inputValue: SelectType, selectOptions: SelectType[]): any => {
const exactValueExists = selectOptions.find(input => input.value === inputValue);
const valueIsNotEmpty = inputValue.trim().length;
return !exactValueExists && valueIsNotEmpty;
};
export default checkCaseSensitiveSelectUtil;
但我收到以下错误:
1. 在input.value === inputValue 我得到以下信息:
(parameter) input: SelectType
This condition will always return 'false' since the types 'string' and 'SelectType' have no overlap.ts(2367)
- 在
trim我得到了这个:
Property 'trim' does not exist on type 'SelectType'.ts(2339)
任何人都知道如何真正消除这两个错误。我的意思是,由于参数是那种确切的类型,我唯一的选择是将它们都转换为任何类型吗?
【问题讨论】:
-
你的参数 inputValue 不是字符串而是对象! inputValue: SelectType...你可能需要比较这个对象的属性... nput.value === inputValue.label (or inputValue.value)
-
对不起。我不懂你在说什么。你能提供更多关于这个问题的信息吗?也许是一个例子?抱歉给您添麻烦
标签: javascript reactjs typescript interface