Skip to content

Missing type narrowing based on control flow analysis of logical expressionsΒ #50739

Closed
@mhofman

Description

@mhofman

Bug Report

πŸ”Ž Search Terms

Control-flow analysis logical

πŸ•— Version & Regression Information

  • This is the behavior in every version I tried, and I reviewed the FAQ

⏯ Playground Link

Playground link with relevant code

πŸ’» Code

declare function fail(): never;

declare const a: string | null;
declare const b: string | null;

if (!a) fail();
a.charAt(0); // a is correctly narrowed to `string`

b || fail();
b.charAt(0); // Object is possibly 'null'. ts(2531)

πŸ™ Actual behavior

It seems that the branches of logical expressions do not impact control flow analysis, and thus the logical expression form is not equivalent to the conditional form, resulting in the type of b not being narrowed.

πŸ™‚ Expected behavior

b narrowed to the string type.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Design LimitationConstraints of the existing architecture prevent this from being fixed

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions