Skip to content

Object is possibly undefined inside a closure #33319

Closed
@iby

Description

@iby

TypeScript Version: 3.7.0-dev.20190907

Search Terms: possibly undefined is:open

Code

function foo(value?: string) {
    if (value === undefined) { value = "hello"; }
    console.log(value.toUpperCase()); // OK
    setTimeout(() => value.toUpperCase(), 1); // TS2532
}

Expected behavior: Successful compilation.

Actual behavior: Compilation failing with error TS2532: Object is possibly 'undefined'..

Playground Link: www.typescriptlang.org/play/#code/…

Related Issues: #31060

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions