Skip to content

instanceof Promise in ternary does not narrow Promiselike in 4.1 #41606

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
brainkim opened this issue Nov 20, 2020 · 6 comments
Closed

instanceof Promise in ternary does not narrow Promiselike in 4.1 #41606

brainkim opened this issue Nov 20, 2020 · 6 comments
Labels
Has Repro This issue has compiler-backed repros: https://aka.ms/ts-repros Needs Investigation This issue needs a team member to investigate its status.

Comments

@brainkim
Copy link

brainkim commented Nov 20, 2020

TypeScript Version: 4.1.0-beta

Search Terms:
promise promiselike instanceof
Code

declare const p: PromiseLike<number>;
const p1 = p instanceof Promise ? p : Promise.resolve(p);
const p2: Promise<number> = p1.then((v) => v + 1);

Expected behavior:

No errors.

Actual behavior:
p2 errors because the expression has been determined to be promise-like.
Also p1 is inferred as PromiseLike & Promise.

Playground Link:
https://www.typescriptlang.org/play?ts=4.1.0-beta#code/CYUwxgNghgTiAEYD2A7AzgF3gBwFzwAUYkBbASzRABkyBrEAHhQFcSAjEGAPgG4AoZOizYAjPAC8OeGSFQUYEEgBmhYuUrwA-FPxFSFEADo4aJBABuIABTYAlP0GYcAJl1qDTVh24ScIwxgAFiAoVlbmthJc8ObwANTwIvZAA

Related Issues:

@brainkim brainkim changed the title ternary with instanceof Promise is working strangely instanceof Promise in ternary does not narrow Promiselike. Nov 20, 2020
@MartinJohns
Copy link
Contributor

const p2: Promise = p.then((v) => v + 1);

Did you mean to use p1 instead of p?

@brainkim
Copy link
Author

brainkim commented Nov 20, 2020

Ah yeah. I was wondering why the bug is only on the new version and I couldn’t reproduce the old behavior. Fixed the typescript example so it only fails on 4.1.

@brainkim brainkim changed the title instanceof Promise in ternary does not narrow Promiselike. instanceof Promise in ternary does not narrow Promiselike in 4.1 Nov 20, 2020
@andrewbranch
Copy link
Member

andrewbranch commented Nov 24, 2020

I think this cooould be an instance of #41563. It’s essentially an overload selection problem—the intersected PromiseLike and Promise is a reasonably correct representation of what we know about p1, but the problem is that we now have an intersected signature for then, which equates to overloads, and no heuristics to help us pick between them.

@DanielRosenwasser DanielRosenwasser added the Needs Investigation This issue needs a team member to investigate its status. label Nov 25, 2020
@typescript-bot typescript-bot added the Has Repro This issue has compiler-backed repros: https://aka.ms/ts-repros label Nov 25, 2020
@typescript-bot
Copy link
Collaborator

typescript-bot commented Apr 13, 2022

👋 Hi, I'm the Repro bot. I can help narrow down and track compiler bugs across releases! This comment reflects the current state of the repro in the issue body running against the nightly TypeScript.


Issue body code block by @brainkim

👍 Compiled

Historical Information
Version Reproduction Outputs
4.2.2, 4.3.2, 4.4.2, 4.5.2, 4.6.2

👍 Compiled

@rubiesonthesky
Copy link

It seems that this have been already fixed in 4.2.2 and issue could be closed?

@brainkim
Copy link
Author

brainkim commented Jan 5, 2023

@rubiesonthesky This appears to have been fixed since 4.2.2. Thanks.

@brainkim brainkim closed this as completed Jan 5, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Has Repro This issue has compiler-backed repros: https://aka.ms/ts-repros Needs Investigation This issue needs a team member to investigate its status.
Projects
None yet
Development

No branches or pull requests

6 participants