Skip to content

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

Closed
@brainkim

Description

@brainkim

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:

Metadata

Metadata

Assignees

No one assigned

    Labels

    Has ReproThis issue has compiler-backed repros: https://aka.ms/ts-reprosNeeds InvestigationThis issue needs a team member to investigate its status.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions