I am trying to use _.find on typescript on a Object to return a value f this object. It originally looked like this:
const iconDict = {
dashboard: <DataVisualizer />,
settings: <SettingsApp />,
'company-manager': <CompanyManager />,
'tenant-administrator': <TenantAdministrator />,
glob
const icon =
_.find(iconDict, (icon, key) => {
const url = window.location.href
if (url.indexOf(key) !== -1) {
return icon
}
}) || iconDict['global']
The code above gives me the error:
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'
Probably because it is falling on this overload
I tried to add the typing of the object like this
const icon =
_.find<typeof iconDict, JSX.Element>(iconDict, (icon, key) => {
const url = window.location.href
if (url.indexOf(key) !== -1) {
return icon
}
And I then get:
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)
Because it falls on this definition
And now I am not sure how to proceed. How to make the typescript knows that I will return either a type of a value of the object or undefined?
Thanks