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

[Strict null checks] Hoisted variables should be usable without assignment #10940

Closed
JannesMeyer opened this issue Sep 15, 2016 · 1 comment
Closed
Labels
Working as Intended The behavior described is the intended behavior; this is not a bug

Comments

@JannesMeyer
Copy link

JannesMeyer commented Sep 15, 2016

TypeScript Version: 2.0.2 with strictNullChecks

Code

if (isMaximised) {
    var onClose = () => {
        /* code */ 
    };
}
console.log(onClose); // Error: Variable 'onClose' is used before being assigned

Expected behavior:
onClose should have the type () => void | undefined and be usable. In my opinion the warning about usage before assignment is only necessary when strictNullChecks is off.

Actual behavior:
onClose has the type () => void and it doesn't compile when being used.

@JannesMeyer JannesMeyer changed the title Hoisted variables should have be usable without assignment Hoisted variables should be usable without assignment Sep 15, 2016
@mhegazy mhegazy added the Bug A bug in TypeScript label Sep 15, 2016
@JannesMeyer JannesMeyer changed the title Hoisted variables should be usable without assignment [Strict null checks] Hoisted variables should be usable without assignment Sep 15, 2016
@mhegazy mhegazy added Working as Intended The behavior described is the intended behavior; this is not a bug and removed Bug A bug in TypeScript labels Sep 19, 2016
@mhegazy
Copy link
Contributor

mhegazy commented Sep 19, 2016

the compiler could not guarantee that this variable is used before it was initialized. if your intention is to treat it as () => void | undefined add a type annotation.

@mhegazy mhegazy closed this as completed Sep 19, 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
Working as Intended The behavior described is the intended behavior; this is not a bug
Projects
None yet
Development

No branches or pull requests

2 participants