Skip to content

Wrong type with Promise chaining #12409

Closed
@tkrotoff

Description

@tkrotoff

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))

Activity

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[/+] on Nov 22, 2016
brandedoutcast

brandedoutcast commented on Nov 26, 2016

@brandedoutcast

@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

jwbay commented on Dec 2, 2016

@jwbay
Contributor

Duplicate of #10977 I think

changed the title [-]Wrong type with Promise chaining and typescript@2.2.0-dev.20161121[/-] [+]Wrong type with Promise chaining and 2.1.4[/+] on Dec 13, 2016
added a commit that references this issue on Dec 15, 2016
tkrotoff

tkrotoff commented on Feb 24, 2017

@tkrotoff
Author

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

jwbay commented on Feb 24, 2017

@jwbay
Contributor

The fix is in version 2.3, not 2.2.

changed the title [-]Wrong type with Promise chaining and 2.1.4[/-] [+]Wrong type with Promise chaining[/+] on Feb 24, 2017
locked and limited conversation to collaborators on Jun 19, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    DuplicateAn existing issue was already created

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

      Development

      No branches or pull requests

        Participants

        @tkrotoff@jwbay@brandedoutcast@mhegazy

        Issue actions

          Wrong type with Promise chaining · Issue #12409 · microsoft/TypeScript