Skip to content

Missing reduction step in a union of intersections with bounded type parameterΒ #48718

Closed
@jfet97

Description

@jfet97

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Design LimitationConstraints of the existing architecture prevent this from being fixed

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions