Skip to content

Promise.then variance issue #18414

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
chancancode opened this issue Sep 12, 2017 · 3 comments
Closed

Promise.then variance issue #18414

chancancode opened this issue Sep 12, 2017 · 3 comments
Labels
Working as Intended The behavior described is the intended behavior; this is not a bug

Comments

@chancancode
Copy link
Contributor

Hello!

I understand that function parameter are unsound/bi-variant in TypeScript in general, however, I thought there was a "targeted fix" for promises at some point, so I am wondering if this is a considered a bug:

function stringOrNumber(): string | number {
  return Math.random() > 0.5 ? 'hello' : 3.14;
} 

function stringToString(s: string): string {
  return s.toUpperCase();
}

Promise.resolve().then(stringOrNumber).then(stringToString);

Playground link

@DanielRosenwasser
Copy link
Member

Unfortunately this is expected from parameter bivariance (but certainly not desired). The fix you're referencing from 2.4 prevents the following bug:

    function toStringOrNumber(): string | number {
      return Math.random() > 0.5 ? 'hello' : 3.14;
    } 

    function stringToString(s: string): string {
      return s.toUpperCase();
    }


    let stringOrNumber = Promise.resolve().then(toStringOrNumber);
    let str = Promise.resolve("").then(stringToString);

    str = stringOrNumber;
//  ~~~ bad

@DanielRosenwasser
Copy link
Member

DanielRosenwasser commented Sep 16, 2017

Check out discussion at #18513 in the meantime.

@DanielRosenwasser DanielRosenwasser added the Working as Intended The behavior described is the intended behavior; this is not a bug label Sep 16, 2017
@mhegazy
Copy link
Contributor

mhegazy commented Oct 3, 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 Oct 3, 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
Working as Intended The behavior described is the intended behavior; this is not a bug
Projects
None yet
Development

No branches or pull requests

3 participants