Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Method returning index type breaks type interference of union types #30717

Copy link
Copy link
@thammi

Description

@thammi
Issue body actions

TypeScript Version: 3.4.0-dev.201xxxxx

Search Terms: union, index type

Code

class Test<Props extends Object> {
  constructor(public o: Props) {}
  get<K extends keyof Props>(k: K) : Props[K] { return this.o[k]; }
}

interface A {t: "A";};
interface B {t: "B";};
type u = Test<A> | Test<B>;

const tmp = new Test<B>({t: 'B'}) as u;

switch(tmp.get('t')) {
  case 'A': break;
  case 'B': break;
}

Expected behavior:

Code compiles with tmp.get('t') having type "A" | "B".

This works for:

Actual behavior:

Compilation fails with error:

Type '"B"' is not comparable to type '"A"'.

Type of tmp.get('t') is only "A".

Problem is not contained to string literals, but also other types. Enountered this bug using immutable-js Record.get().

Playground Link: | Test; const tmp = new Test({t: 'B'}) as u; switch(tmp.get('t')) { case 'A': break; case 'B': break; } " rel="nofollow">playground

Related Issues: None found

Reactions are currently unavailable

Metadata

Metadata

Assignees

Labels

BugA bug in TypeScriptA bug in TypeScriptFix AvailableA PR has been opened for this issueA PR has been opened for this issue

Type

No type
No fields configured for issues without a type.

Projects

No projects

Relationships

None yet

Development

No branches or pull requests

Issue actions

    Morty Proxy This is a proxified and sanitized view of the page, visit original site.