Skip to content

[TypeScript] defaultMemoize errors #384

@arthwood

Description

@arthwood

Current TS definitions show error for the following usages:

import { isEqual } from 'lodash'

interface Transaction {
  transactionId: string
}

const toId = (transaction: Transaction) => transaction.transactionId
const transactionsIds = (transactions: Transaction[]) => transactions.map(toId)
const collectionsEqual = (ts1: Transaction[], ts2: Transaction[]) => isEqual(transactionsIds(ts1), transactionsIds(ts2))

const createTransactionsSelector = createSelectorCreator(defaultMemoize, collectionsEqual)

Results in:

Error:(148, 58) TS2345: Argument of type '<F extends Function>(func: F, equalityCheck?: (<T>(a: T, b: T, index: number) => boolean) | undefined) => F' is not assignable to parameter of type '<F extends Function>(func: F, option1: ((ts1: Transaction[], ts2: Transaction[]) => boolean) | undefined) => F'.
  Types of parameters 'equalityCheck' and 'option1' are incompatible.
    Types of parameters 'ts1' and 'a' are incompatible.
      Type 'T' is not assignable to type 'Transaction[]'.

This also happens when creating memoized function:

const groupTransactionsByLabel = defaultMemoize(
  (transactions: Transaction[]) => groupBy(transactions, headerForTransaction),
  collectionsEqual,
)

Possible solution:

When I change:

export function defaultMemoize<F extends Function>(
  func: F, equalityCheck?: <T>(a: T, b: T, index: number) => boolean,
): F;

to:

export function defaultMemoize<F extends Function, T>(
  func: F, equalityCheck?: (a: T, b: T, index: number) => boolean,
): F;

Everything works as expected but I'm not sure if that's correct approach.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions