Similar to Nested Typescript Map Type, this case involves nesting on the "value" side.
const mapObjectObject: Map<string, string | Map<string, string>> = new Map(Object.entries({
"a": "b",
"c": new Map(Object.entries({
"d": "e",
}))
})); // ✓
const mapObjectMap: Map<string, string | Map<string, string>> = new Map(Object.entries({
"a": "b",
"c": new Map([
["d", "e"]
])
})); // ✓
const mapMapObject: Map<string, string | Map<string, string>> = new Map([
["a", "b"],
["c", new Map(Object.entries({
"d": "e",
}))
]); // ✗
const mapMapMap: Map<string, string | Map<string, string>> = new Map([
["a", "b"],
["c", new Map([
["d", "e"]
])]
]); // ✗
The first two examples work, but the second two do not. Both give an error message:
Overload 1 of 3, '(iterable: Iterable<readonly [string, string]>): Map<string, string>', gave the following error.
Argument of type '([string, string] | [string, Map<string, string>])[]' is not assignable to parameter of type 'Iterable<readonly [string, string]>'.
The types returned by '[Symbol.iterator]().next(...)' are incompatible between these types.
Type 'IteratorResult<[string, string] | [string, Map<string, string>], any>' is not assignable to type 'IteratorResult<readonly [string, string], any>'.
Type 'IteratorYieldResult<[string, string] | [string, Map<string, string>]>' is not assignable to type 'IteratorResult<readonly [string, string], any>'.
Type 'IteratorYieldResult<[string, string] | [string, Map<string, string>]>' is not assignable to type 'IteratorYieldResult<readonly [string, string]>'.
Type '[string, string] | [string, Map<string, string>]' is not assignable to type 'readonly [string, string]'.
Type '[string, Map<string, string>]' is not assignable to type 'readonly [string, string]'.
Types of property '1' are incompatible.
Type 'Map<string, string>' is not assignable to type 'string'.
Overload 2 of 3, '(entries?: readonly (readonly [string, string])[] | null | undefined): Map<string, string>', gave the following error.
Type 'Map<string, string>' is not assignable to type 'string'.(2769)
and
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] | [string, Map<string, string>])[]' is not assignable to parameter of type 'Iterable<readonly [string, string]>'.
Overload 2 of 3, '(entries?: readonly (readonly [string, string])[] | null | undefined): Map<string, string>', gave the following error.
Type 'Map<string, string>' is not assignable to type 'string'.(2769)
Respectively. Even removing the type declaration does not resolve the issue. Inferred types also return the same error.