Skip to content
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

Wrong type with Promise chaining #12409

Closed
tkrotoff opened this issue Nov 21, 2016 · 4 comments
Closed

Wrong type with Promise chaining #12409

tkrotoff opened this issue Nov 21, 2016 · 4 comments
Labels
Duplicate An existing issue was already created

Comments

@tkrotoff
Copy link

tkrotoff commented Nov 21, 2016

With 2.1.4 (edit: first reported with typescript@2.2.0-dev.20161121):

declare function createHello(): Promise<Hello>;

interface Hello {
  world(): Promise<any>;
}

createHello()
  .then(hello => hello.world())
  .then(message => {
    console.log(message); // BUG: message is of type Hello instead of any
  });

On TypeScript playground, no problem (edit: this is not true anymore, was working with 2.0.10, not with 2.1.4): https://www.typescriptlang.org/play/index.html#src=declare%20function%20createHello()%3A%20Promise%3CHello%3E%3B%0D%0A%0D%0Ainterface%20Hello%20%7B%0D%0A%20%20world()%3A%20Promise%3Cany%3E%3B%0D%0A%7D%0D%0A%0D%0AcreateHello()%0D%0A%20%20.then(hello%20%3D%3E%20hello.world())%0D%0A%20%20.then(message%20%3D%3E%20%7B%0D%0A%20%20%20%20console.log(message)%3B%20%2F%2F%20message%20is%20of%20type%20any%20%3D%3E%20works%0D%0A%20%20%7D)%3B%0D%0A

(for the little story, the bug was found here: DefinitelyTyped/DefinitelyTyped#12808 (comment))

@tkrotoff tkrotoff changed the title Wrong type detection with Promise chaining and typescript@2.2.0-dev.20161121 Wrong type with Promise chaining and typescript@2.2.0-dev.20161121 Nov 22, 2016
@brandedoutcast
Copy link

@tkrotoff I'm not sure if it's an issue or a feature but the recent changes to then callback might be breaking code & the alternative I see is to use the generic version of then<TResult>.

createHello()
  .then<any>(hello => hello.world())
  .then(message => {
    console.log(message); // Now message is of type any
});

Breaking changes

@jwbay
Copy link
Contributor

jwbay commented Dec 2, 2016

Duplicate of #10977 I think

@tkrotoff tkrotoff changed the title Wrong type with Promise chaining and typescript@2.2.0-dev.20161121 Wrong type with Promise chaining and 2.1.4 Dec 13, 2016
tkrotoff added a commit to tkrotoff/DefinitelyTyped that referenced this issue Dec 14, 2016
See Wrong type with Promise chaining and 2.1.4 microsoft/TypeScript#12409
greglo pushed a commit to greglo/DefinitelyTyped that referenced this issue Dec 15, 2016
See Wrong type with Promise chaining and 2.1.4 microsoft/TypeScript#12409
@mhegazy mhegazy added the Duplicate An existing issue was already created label Dec 16, 2016
@tkrotoff
Copy link
Author

tkrotoff commented Feb 24, 2017

TypeScript 2.2 Promise implementation is still broken, example:

interface Toto {}

const promise = new Promise<Toto>((resolve, reject) => {
  resolve('toto');
});

promise
  .then(value => { // value is of type Toto => OK
    console.log('then1 value=', value);
    return 'titi';
  })
  .then(value => { // value is of type Toto => KO, should be 'titi' or string
    console.log('then2 value=', value);
    return 2;
  })
  .then(value => { // value is of type Toto => KO, should be 2 or number
    console.log('then3 value=', value);
  });

Verification by executing the code inside Chrome DevTools:

then1 value= toto
then2 value= titi
then3 value= 2

Solution for now:

promise
  .then<string>(value => {
    console.log('then1 value=', value);
    return 'titi';
  })
  .then<number>(value => {
    console.log('then2 value=', value);
    return 2;
  })
  .then(value => {
    console.log('then3 value=', value);
  });

@jwbay
Copy link
Contributor

jwbay commented Feb 24, 2017

The fix is in version 2.3, not 2.2.

@tkrotoff tkrotoff changed the title Wrong type with Promise chaining and 2.1.4 Wrong type with Promise chaining Feb 24, 2017
@mhegazy mhegazy closed this as completed Apr 21, 2017
@microsoft microsoft locked and limited conversation to collaborators Jun 19, 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

4 participants