Skip to content

Narrowing doesn't recognize string constant as truthy #33878

Open
@nmain

Description

@nmain

TypeScript Version: 3.7-beta

Search Terms: cfa narrowing string truthy

Code

declare var a: { b: number } | null

!a || a.b > 3; // works
!a && true || a.b > 3 && true; // works
!a && "a was not defined" || a.b > 3 && "a.b was too big"; // error "Object is possibly null"

Expected behavior:

The narrowings all work

Actual behavior:

The third narrowing does not work and produces an error

Playground Link:
Playground

Related Issues:
I suspect this is a duplicate, as most CFA narrowing bugs are. But I searched as best I could and didn't find any other bugs that matched what was going on here. It seems like typescript doesn't understand that a non-empty string-literal is true in this situation?

Metadata

Metadata

Assignees

No one assigned

    Labels

    BugA bug in TypeScript

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions