I want to create a Map where the key can be a number or a string. I want to initialise it with values in the constructor. Some entries have a number key and some have a string key.
But I get a typescript error. Is this a TypeScript bug, or am I doing something wrong?
type MyMap = Map<string | number, string>;
// Errors, incorrectly?
const myMapWithMixedKeys: MyMap = new Map([
['abc', 'String key'],
[123, 'Number key']
]);
// All fine
myMapWithMixedKeys.set('abcd', 'String key');
myMapWithMixedKeys.set(1234, 'Number key');
const myMapStrings: MyMap = new Map([
['abc', 'String key'],
['abcd', 'String key']
]);
const myMapNumbers: MyMap = new Map([
[123, 'Number key'],
[1234, 'Number key']
]);
The error:
No overload matches this call.
Overload 1 of 3, '(iterable: Iterable<readonly [string, string]>): Map<string, string>', gave the following error.
Argument of type '([string, string] | [number, string])[]' is not assignable to parameter of type 'Iterable<readonly [string, string]>'.
Types of property '[Symbol.iterator]' are incompatible.
Type '() => IterableIterator<[string, string] | [number, string]>' is not assignable to type '() => Iterator<readonly [string, string], any, undefined>'.
Type 'IterableIterator<[string, string] | [number, string]>' is not assignable to type 'Iterator<readonly [string, string], any, undefined>'.
Types of property 'next' are incompatible.
Type '(...args: [] | [undefined]) => IteratorResult<[string, string] | [number, string], any>' is not assignable to type '(...args: [] | [undefined]) => IteratorResult<readonly [string, string], any>'.
Type 'IteratorResult<[string, string] | [number, string], any>' is not assignable to type 'IteratorResult<readonly [string, string], any>'.
Type 'IteratorYieldResult<[string, string] | [number, string]>' is not assignable to type 'IteratorResult<readonly [string, string], any>'.
Type 'IteratorYieldResult<[string, string] | [number, string]>' is not assignable to type 'IteratorYieldResult<readonly [string, string]>'.
Type '[string, string] | [number, string]' is not assignable to type 'readonly [string, string]'.
Type '[number, string]' is not assignable to type 'readonly [string, string]'.
Types of property '0' are incompatible.
Type 'number' is not assignable to type 'string'.
Overload 2 of 3, '(entries?: readonly (readonly [string, string])[] | null | undefined): Map<string, string>', gave the following error.
Argument of type '([string, string] | [number, string])[]' is not assignable to parameter of type 'readonly (readonly [string, string])[]'.
Type '[string, string] | [number, string]' is not assignable to type 'readonly [string, string]'.
Type '[number, string]' is not assignable to type 'readonly [string, string]'.ts(2769)