Lodash с машинописным шрифтом не работает с перегрузкой - PullRequest
0 голосов
/ 10 октября 2019

Я пытаюсь использовать _.find для машинописного текста объекта, чтобы вернуть значение для этого объекта. Первоначально это выглядело так:

  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']

Приведенный выше код дает мне ошибку:

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'

Возможно, потому что он падает на эта перегрузка

Я пытался добавить тип объекта следующим образом

  const icon =
    _.find<typeof iconDict, JSX.Element>(iconDict, (icon, key) => {
      const url = window.location.href
      if (url.indexOf(key) !== -1) {
        return icon
      }

И затем я получаю:

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)

Поскольку он падает на это определение

И теперь я не уверен, как поступить. Как сделать, чтобы машинопись знала, что я верну либо тип значения объекта, либо неопределенное?

Спасибо

...