I am trying to create a Map in typescript and getting the no overload matches this call error whenever I use a union type for the functions declaration type attached to the new Map I am using a union type for the functions because the function number of argument changes. The code is written below:
type FirstFunction = (a: string, b: string, c?:string) => boolean;
type SecondFunction = (a: string[], b: string, c: string) => boolean;
type BothFunctions = FirstFunction | SecondFunction;
const filterHandlers: Map<string, BothFunctions=> = new Map([
[
"orgName",
(a: string, b: string): boolean =>
a.toLowerCase().includes(b.toLowerCase()),
],
[
"email",
(a: string, b: string): boolean =>
a.toLowerCase().includes(b.toLowerCase()),
],
[
"userName",
(a: string, b: string): boolean =>
a.toLowerCase().includes(b.toLowerCase()),
],
[
"date",
(a: string, b: string): boolean =>
a.toLowerCase().includes(b.toLowerCase()),
],
[
"phoneNumber",
(a: string, b: string): boolean =>
a.toLowerCase().includes(b.toLowerCase()),
],
[
"status",
(a: string[], b: string, c: string): boolean =>
getUserStatus(a,b) === c,
],
]);
The error that I am getting is this:
(method) String.toLowerCase(): string
Converts all the alphabetic characters in a string to lowercase.
No overload matches this call.
Overload 1 of 4, '(iterable?: Iterable<readonly [unknown, unknown]> | null | undefined): Map<unknown, unknown>', gave the following error.
Argument of type '((string | ((a: string, b: string) => boolean))[] | (string | ((a: string[], b: string, c: string) => boolean))[])[]' is not assignable to parameter of type 'Iterable<readonly [unknown, unknown]>'.
The types returned by '[Symbol.iterator]().next(...)' are incompatible between these types.
Type 'IteratorResult<(string | ((a: string, b: string) => boolean))[] | (string | ((a: string[], b: string, c: string) => boolean))[], any>' is not assignable to type 'IteratorResult<readonly [unknown, unknown], any>'.
Type 'IteratorYieldResult<(string | ((a: string, b: string) => boolean))[] | (string | ((a: string[], b: string, c: string) => boolean))[]>' is not assignable to type 'IteratorResult<readonly [unknown, unknown], any>'.
Type 'IteratorYieldResult<(string | ((a: string, b: string) => boolean))[] | (string | ((a: string[], b: string, c: string) => boolean))[]>' is not assignable to type 'IteratorYieldResult<readonly [unknown, unknown]>'.
Type '(string | ((a: string, b: string) => boolean))[] | (string | ((a: string[], b: string, c: string) => boolean))[]' is not assignable to type 'readonly [unknown, unknown]'.
Type '(string | ((a: string, b: string) => boolean))[]' is not assignable to type 'readonly [unknown, unknown]'.
Target requires 2 element(s) but source may have fewer.
Overload 2 of 4, '(entries?: readonly (readonly [string, (a: string, b: string) => boolean])[] | null | undefined): Map<string, (a: string, b: string) => boolean>', gave the following error.
Type '(a: string[], b: string, c: string) => boolean' is not assignable to type '(a: string, b: string) => boolean'.ts(2769)
No quick fixes available
The reason I used the union types was to make the two functions optional and not mandatory.
How do I fix it?
Thank you in advance.