javascript - TypeScript - 实用方法中的错误
问题描述
我有以下实用方法。它有两个参数,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)
任何人都知道如何真正消除这两个错误。我的意思是,由于参数是那种确切的类型,我唯一的选择是将它们都转换为任何参数吗?
解决方案
1.
您正在比较input.value
(字符串类型)和inputValue
(类型SelectType
)。一种可能的解决方案是将 的值input
与 的值进行比较inputValue
。
selectOptions.find(input => input.value === inputValue.value);
2.
const valueIsNotEmpty = inputValue.trim().length;
因为inputValue
不是价值,而是SelectType
你的一个实例,不能只是修剪它。
尝试获取值(字符串)
const valueIsNotEmpty = inputValue.value.trim().length;
您应该考虑重命名变量。命名有点令人困惑;)
推荐阅读
- jenkins - 如何调试 Jenkins 的 `emailext` 插件?
- javascript - 如何通过 mysql datetime 输入 datetime-local
- python-3.x - 当有同名的包时,如何在 Python 3 中加载模块?
- python - 使用 Python 从 Blender 2.8 导出每个面部纹理索引
- string - xQuery:检查元素中的字符串,但忽略同一元素中但具有不同值的多个元素
- python - 如何增加 PySpark 数据帧的分区?
- javascript - 使用 withRouter 封装的组件测试按钮的状态
- scala - Spark Scala - 合并的滚动窗口
- audio - amix 过滤器导致音频消失
- express - post 适用于 request 模块,但不适用于 axios