Skip to content

someBool || true gets the literal type true when assigned to a let variable at initialization (unlike true getting the type boolean)Β #61799

Open
@aweebit

Description

@aweebit

πŸ”Ž Search Terms

boolean literal type true false assign expression

πŸ•— Version & Regression Information

⏯ Playground Link

https://www.typescriptlang.org/play/?#code/CYUwxgNghgTiAEEQBd4GcD2BbEAhDGEAXPAEYFJQB2A3AFB1KogAeUWADkgIzwC88ZDACuIek3it2XEACZ+6bHgrwAPqsEixQA

πŸ’» Code

declare let someBool: boolean;

let okay = true;
let weird = someBool || true;

πŸ™ Actual behavior

weird is assigned the type true, whereas okay is of type boolean.

πŸ™‚ Expected behavior

Both variables have the type boolean.

Additional information about the issue

Stumbled upon this while writing facebook/react#33399.

Very unexpected and confusing behavior, I highly doubt it is intended. Couldn't find anything explaining the change in the release notes for TypeScript 4.2, so pretty sure it's an unwanted regression. Please correct me if I'm wrong.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Not a DefectThis behavior is one of several equally-correct options

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions