Skip to content

Type union narrowing in callbacks #16917

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
sccolbert opened this issue Jul 3, 2017 · 3 comments
Closed

Type union narrowing in callbacks #16917

sccolbert opened this issue Jul 3, 2017 · 3 comments
Labels
Duplicate An existing issue was already created

Comments

@sccolbert
Copy link

sccolbert commented Jul 3, 2017

TypeScript Version: 2.3.1

Code

type Thing = {
  type: 'thing';
  thing: number;
  doStuff(cb: (value: Thing) => void): Thing;
}


type Foo = {
  type: 'foo';
  foo: number;
  doStuff(cb: (value: Foo) => void): Foo;
}


type Bar = {
  type: 'bar';
  bar: number;
  doStuff(cb: (value: Bar) => void): Bar;
}


type Baz = {
  type: 'baz';
  baz: number;
  doStuff(cb: (value: Baz) => void): Baz;
}


type Stuff = Thing | Foo | Bar | Baz;


function main() {
  let stuff: Stuff;
  if (stuff.type !== 'thing' && stuff.type !== 'foo') {
    return;
  }
  stuff = stuff.doStuff((value: Foo | Thing) => {
    value; // value should be Foo | Thing here, but fails with error TS2349: Cannot invoke an expression whose type lacks a call signature....
  });
}

Expected behavior:

It seems like the above snippet should compile.

Actual behavior:

The compiler doesn't see the callback as compatible with the callback required by the remaining types in the union.

@yortus
Copy link
Contributor

yortus commented Jul 4, 2017

Same cause as #1805?

@mhegazy mhegazy added the Needs Investigation This issue needs a team member to investigate its status. label Aug 29, 2017
@mhegazy
Copy link
Contributor

mhegazy commented Oct 25, 2017

Duplicate of #7294

@mhegazy mhegazy marked this as a duplicate of #7294 Oct 25, 2017
@mhegazy mhegazy added Duplicate An existing issue was already created and removed Needs Investigation This issue needs a team member to investigate its status. labels Oct 25, 2017
@mhegazy
Copy link
Contributor

mhegazy commented Nov 9, 2017

Automatically closing this issue for housekeeping purposes. The issue labels indicate that it is unactionable at the moment or has already been addressed.

@mhegazy mhegazy closed this as completed Nov 9, 2017
@microsoft microsoft locked and limited conversation to collaborators Jun 14, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Duplicate An existing issue was already created
Projects
None yet
Development

No branches or pull requests

3 participants