Skip to content

"Used before assigned" error with arrow functions and strict null checks #9382

Description

TypeScript Version:

1.9.0-dev.20160627-1.0

Code

const f = () => { return f; };

Compiled with --strictNullChecks.

Expected behavior:

No errors.

Actual behavior:

foo.ts(1,26): error TS2454: Variable 'f' is used before being assigned.

It sems to be impossible to call f before f is assigned, so I don't think this should trigger an error.

Activity

  1. mhegazy commented on Jun 27, 2016

    @mhegazy
    Contributor

    The error message is a bit misleading. the message is about the type system trying to know what is the type of f and catching its own tail. so an explicit type annotation would resolve the issue. so:

    const f: any = () => { return f; };
  2. Christoph-D commented on Jun 28, 2016

    @Christoph-D
    Author

    This gives the same error, although I added an explicit type annotation.

    const f: () => void = () => { f; }

    But the following gives no error:

    let f: () => void;
    f = () => { f; }
  3. bolismauro commented on Jul 6, 2016

    @bolismauro

    Christoph (@Christoph-D) I don't receive the error if I change the definition of the function from const to let.
    So basically this gives me an error

    const t2 = (): number => 1 + t2();

    while this does not

    let t = (): number => 1 + t();
  4. mhegazy commented on Sep 29, 2016

    @mhegazy
    Contributor

    should be fixed by #10815

  5. locked and limited conversation to collaborators on Jun 19, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Labels

BugA bug in TypeScriptFixedA PR has been merged for this issue

Type

No type

Projects

No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions