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

tsc doesn't catch use of unassigned variable inside closure #32025

Closed
steabert opened this issue Jun 21, 2019 · 6 comments
Closed

tsc doesn't catch use of unassigned variable inside closure #32025

steabert opened this issue Jun 21, 2019 · 6 comments
Labels
Duplicate An existing issue was already created

Comments

@steabert
Copy link

steabert commented Jun 21, 2019

TypeScript Version: 3.6.0-dev.20190621

Search Terms:

Code

// A *self-contained* demonstration of the problem follows...
// Test this by running `tsc` on the command-line, rather than through another build tool such as Gulp, Webpack, etc.
let a: number[]

const b = () => {
  a.push(2) // no 2454 error
}

b() // runtime error

Expected behavior:

Compile time error 2454.

Actual behavior:

No error during compiling with tsc, runtime error instead.

Playground Link:

Related Issues:

Not really, i found a bunch of bugs related to use before assign, but not this. It seems this should be a fairly obvious bug, or I'm doing something wrong.

@steabert steabert changed the title doesn't catch using unassigned variable inside closure tsc doesn't catch use of unassigned variable inside closure Jun 21, 2019
@nmain
Copy link

nmain commented Jun 21, 2019

Since the error is caught when b is replaced by an IIFE, isn't this just #9998?

@steabert
Copy link
Author

steabert commented Jun 21, 2019

I'm not sure about #9998, I certainly didn't immediately make the connection. However, trying to work around it by allowing a to be undefined:

let a: number[] | undefined = undefined

a.push(2)

made me even more confused as this doesn't get caught even when not in a closure. It seems I'm doing something wrong here.

@nmain
Copy link

nmain commented Jun 21, 2019

For that one, you need to turn on strictNullChecks.

@steabert
Copy link
Author

steabert commented Jun 21, 2019

Thanks! I had that in my tsconfig.json but missed that my test file wasn't included there. I'll use it as a work-around, then I guess this can be closed as duplicate of / related to #9998?

@ahejlsberg ahejlsberg added the Duplicate An existing issue was already created label Jun 21, 2019
@ajafff
Copy link
Contributor

ajafff commented Jun 21, 2019

Variables that are never assigned are likely a bug. I can offer a lint rule to detect such variables: https://github.com/fimbullinter/wotan/blob/master/packages/mimir/docs/no-unassigned-variable.md

@typescript-bot
Copy link
Collaborator

This issue has been marked as a 'Duplicate' and has seen no recent activity. It has been automatically closed for house-keeping purposes.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Duplicate An existing issue was already created
Projects
None yet
Development

No branches or pull requests

5 participants