Bug Report
🔎 Search Terms
reduction, generic, union, intersection
🕗 Version & Regression Information
This bug is also the 'Nightly' version. Versions prior to 4.6.2 give even more errors.
⏯ Playground Link
Playground link with relevant code
💻 Code
type NumericLiteral = {
value: number;
type: "NumericLiteral";
};
type StringLiteral = {
value: string;
type: "StringLiteral";
};
type Identifier = {
name: string;
type: "Identifier";
};
type CallExpression = {
name: string;
arguments: DropbearNode[];
type: "CallExpression";
};
type DropbearNode =
| NumericLiteral
| StringLiteral
| Identifier
| CallExpression;
type Visitor = {
[K in DropbearNode["type"]]: (node: Extract<DropbearNode, { type: K }>) => void
};
function visitNode<K extends DropbearNode['type']>(node: Extract<DropbearNode, { type: K }>, v: Visitor) {
v[node.type](node) // error
}
🙁 Actual behavior
typeof node.type is inferred as (K & "NumericLiteral") | (K & "StringLiteral") | (K & "Identifier") | (K & "CallExpression") that it is equal to K itself considering that K extends "NumericLiteral" | "StringLiteral" | "Identifier" | "CallExpression".
Something goes really wrong when I try to use node.type as an index, maybe because its type doesn't perform well if used as an index to access the Visitor type.
🙂 Expected behavior
typeof node.type inferred as K because it is simpler to interact with and doesn't give problems if used as an index.
Bug Report
🔎 Search Terms
reduction, generic, union, intersection
🕗 Version & Regression Information
This bug is also the 'Nightly' version. Versions prior to
4.6.2give even more errors.⏯ Playground Link
Playground link with relevant code
💻 Code
🙁 Actual behavior
typeof node.typeis inferred as(K & "NumericLiteral") | (K & "StringLiteral") | (K & "Identifier") | (K & "CallExpression")that it is equal toKitself considering thatK extends "NumericLiteral" | "StringLiteral" | "Identifier" | "CallExpression".Something goes really wrong when I try to use
node.typeas an index, maybe because its type doesn't perform well if used as an index to access theVisitortype.🙂 Expected behavior
typeof node.typeinferred asKbecause it is simpler to interact with and doesn't give problems if used as an index.