Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
Christoph-D opened this issue Jun 27, 2016 · 4 comments
Closed
Assignees
Labels
Bug A bug in TypeScript Fixed A PR has been merged for this issue

Comments

@Christoph-D
Copy link

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.

@mhegazy
Copy link
Contributor

mhegazy commented Jun 27, 2016

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; };

@mhegazy mhegazy added the Bug A bug in TypeScript label Jun 27, 2016
@mhegazy mhegazy added this to the TypeScript 2.1 milestone Jun 27, 2016
@Christoph-D
Copy link
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; }

@bolismauro
Copy link

@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();

@mhegazy
Copy link
Contributor

mhegazy commented Sep 29, 2016

should be fixed by #10815

@mhegazy mhegazy closed this as completed Sep 29, 2016
@microsoft microsoft locked and limited conversation to collaborators Jun 19, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Bug A bug in TypeScript Fixed A PR has been merged for this issue
Projects
None yet
Development

No branches or pull requests

4 participants