Skip to content

Uninitialized local check still permits provably erroneous use-before-assign in closureΒ #60181

@kirkwaiblinger

Description

@kirkwaiblinger

πŸ”Ž Search Terms

uninitialized, local, use before assign, control flow

πŸ•— Version & Regression Information

⏯ Playground Link

https://www.typescriptlang.org/play/?ts=5.7.0-dev.20240925#code/DYUwLgBAZg9jBcEDOYBOBLAdgcwNwCh8BjGTFZcAMTggF4IAKASkQDcZ0ATOgPggG98ECOiiNYMAHSgcYABYQAPBACMTAUOHQa9AOQTdBYQF98xgvhAAPAA4xUkfqaA

πŸ’» Code

let foo: string;

const setFoo = (): void => {
  if (foo.length < 1) {
    foo = 'foo';
  }
};

export {}

πŸ™ Actual behavior

No TS Error

πŸ™‚ Expected behavior

Variable 'foo' is used before being assigned.(2454)

Additional information about the issue

(this error is correctly reported if we never assign at all, as below)

let foo: string;

const setFoo = (): void => {
  if (foo.length < 1) {
  }
};

export {}

Related, #60064.

Otherwise this can be solved by not considering assignments which occur unconditionally after a read.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Awaiting More FeedbackThis means we'd like to hear from more people who would be helped by this featureSuggestionAn idea for TypeScript

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions