My data structure includes a Map
that maps one string
to another string
:
const strToStr = new Map<string, string>([
['foo', 'foo'],
['bar', 'qux'],
]);
I also have a different Map
where each key is a string
and the value is a function that takes a string as input:
const strToFnc = new Map<string, (_: string) => boolean>([
['baz', (_: string) => true],
['buz', (_: string) => false],
]);
In the third Map
, I intend to store these predicate functions according to their respective keys:
const predicates = new Map<string, (_: string) => boolean>();
To assign the functions to the keys in the first set of data (strToStr
), I use the following approach:
strToStr.forEach((k) => predicates.set(k, (_) => (k == strToStr.get(k))));
This method works correctly. However, I encounter an issue when attempting to do the same for the second set of data:
strToFnc.forEach((k) => predicates.set(k, strToFnc.get(k)));
temp.ts:11:40 - error TS2345: Argument of type '(_: string) => boolean' is not assignable to parameter of type 'string'.
11 strToFnc.forEach((k) => predicates.set(k, strToFnc.get(k)));
~
temp.ts:11:56 - error TS2345: Argument of type '(_: string) => boolean' is not assignable to parameter of type 'string'.
11 strToFnc.forEach((k) => predicates.set(k, strToFnc.get(k)));
~
Found 2 errors in the same file, starting at: temp.ts:11
I am puzzled by the fact that strToFnc.get('...')
returns a predicate function but fails to be added this way. What could be causing this discrepancy?