Skip to content

Variable still reported as possibly null after nullish coalescing operator #35193

@marnusw

Description

@marnusw

TypeScript Version: 3.8.0-dev.20191119

Search Terms:

nullish

Code

const takesAString = (s: string) => s
let maybeString: string | null
const result = maybeString ?? takesAString(maybeString)

Expected behavior:

Since takesAString will never be executed when maybeString === null due to the nullish coalescing operator this code should be valid.

Actual behavior:

Passing maybeString to the function results in the error:

error TS2345: Argument of type 'string | null' is not assignable to parameter of type 'string'.

This only happens when strictNullChecks: true, but it should be fine either way.

Playground Link:

Playground link

Related Issues:

None

Metadata

Metadata

Assignees

No one assigned

    Labels

    Working as IntendedThe behavior described is the intended behavior; this is not a bug

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions