Javascript 使用typescript查找的Lodash无法使用重载
我试图在对象上使用typescript上的u.find来返回此对象的值。 原来是这样的:Javascript 使用typescript查找的Lodash无法使用重载,javascript,typescript,lodash,Javascript,Typescript,Lodash,我试图在对象上使用typescript上的u.find来返回此对象的值。 原来是这样的: const icondit={ 仪表板:, 设置:, “公司经理”:, “租户管理员”:, 地球仪 常量图标= _.find(icondit,(icon,key)=>{ const url=window.location.href if(url.indexOf(key)!=-1){ 返回图标 } })| | ICondit['global'] 上面的代码给出了错误: No overload matches
const icondit={
仪表板:,
设置:,
“公司经理”:,
“租户管理员”:,
地球仪
常量图标=
_.find(icondit,(icon,key)=>{
const url=window.location.href
if(url.indexOf(key)!=-1){
返回图标
}
})| | ICondit['global']
上面的代码给出了错误:
No overload matches this call.
The last overload gave the following error.
Argument of type '(icon: Element, key: string) => Element | undefined' is not assignable to parameter of type 'string | number | symbol | [string | number | symbol, any] | ObjectIterator | PartialShallow | undefined'.
Type '(icon: Element, key: string) => Element | undefined' is not assignable to type 'ObjectIterator'.
Type 'Element | undefined' is not assignable to type 'boolean'.
Type 'undefined' is not assignable to type 'boolean'
没有与此调用匹配的重载。
上一次重载导致以下错误。
类型为“(icon:Element,key:string)=>Element | undefined”的参数不能分配给类型为“string | number | symbol |[string | number | symbol,any]| ObjectIterator | PartialShallow | undefined”的参数。
Type'(icon:Element,key:string)=>Element | undefined'不可分配给类型'ObjectIterator'。
类型“Element | undefined”不可分配给类型“boolean”。
类型“undefined”不可分配给类型“boolean”
可能是因为它正在坠落
我试图像这样添加对象的类型
const图标=
_.find(icondit,(icon,key)=>{
const url=window.location.href
if(url.indexOf(key)!=-1){
返回图标
}
然后我得到:
Argument of type '(icon: Element, key: string) => Element | undefined' is not assignable to parameter of type 'ObjectIteratorTypeGuard'.
Signature '(icon: Element, key: string): Element | undefined' must be a type predicate.ts(2345)
类型为“(icon:Element,key:string)=>Element | undefined”的参数不能分配给类型为“ObjectIteratorTypeGuard”的参数。
签名“(图标:元素,键:字符串):元素|未定义”必须是类型谓词。ts(2345)
因为它落在
现在我不知道该怎么做。
如何使typescript知道我将返回对象值的类型或未定义的类型
谢谢我不确定您是否正在使用
@types/lodash
,但这是第一步,给TS一些查找
的签名
存在对“查找”的覆盖:
查找(
集合:列表|空|未定义,
谓词?:ListIterateeCustom,
fromIndex?:数字
):T |未定义;
如果将谓词更改为
(图标、键)=>{
const url=window.location.href
返回url.indexOf(键)!=-1?图标:false
}
因此,在未找到的情况下,它将返回boolean