Open
Description
Bug Report
🔎 Search Terms
- narrowing of generic types
- exhaustive switch case
🕗 Version & Regression Information
Theoretically this should be fixed by:
- Generics extending unions cannot be narrowed #13995
- Improve narrowing of generic types in control flow analysis #43183
Similar issue:
I'm trying against the latest beta: 4.3.0-pr-43183-11 / 15fae38.
⏯ Playground Link
Playground link with relevant code
💻 Code
interface TopLevelElementMap {
"a": HTMLElement;
"b": HTMLElement;
}
interface ElementAttributes {}
interface ElementAttributesMap {
"a": ElementAttributes;
"b": ElementAttributes;
}
class Element<TagName extends keyof TopLevelElementMap> {
protected attributes: ElementAttributesMap[TagName] = {};
public constructor(type: TagName) {}
}
const ElementTagNameMap = {
"a": function(): Element<"a"> {
return new Element("a");
},
"b": function(): Element<"b"> {
return new Element("b");
}
};
function createPrimitive<TagName extends keyof typeof ElementTagNameMap>(tagName: TagName) {
switch (tagName) {
case "a":
return function(): Element<TagName> {
//
// [?] Shouldn't `tagName` get narrowed to just "a"?
//
return ElementTagNameMap[tagName]();
};
case "b":
default:
throw new Error("Unrecognized element `" + tagName + "`.");
}
}
const a = createPrimitive("a");
const b = createPrimitive("b");
As a workaround I can assert as Element<TagName>
on the inner-most return statement to silence the error.
🙁 Actual behavior
Type 'Element<"a"> | Element<"b">' is not assignable to type 'Element<TagName>'.
Type 'Element<"a">' is not assignable to type 'Element<TagName>'.
Type '"a"' is not assignable to type 'TagName'.
'"a"' is assignable to the constraint of type 'TagName', but 'TagName' could be instantiated with a different subtype of constraint '"a" | "b"'. (2322)
🙂 Expected behavior
TagName
should be narrowed to one of the possible values dictated by the case clause.