microsoft / microsoft/TypeScript
Use before declaration errors when using let statements within a switch case
Nobody has claimed this yet.
- Dominant language
- Go
- Stars
- 111k
- Forks
- 14.3k
- Avg merge
- 2d 4h
- Merged PRs (30d)
- 132
Description
TypeScript Version: 2.5.3
Code
function test(value) {
switch (value) {
case 1:
let foo = 10;
break;
case 2:
foo = 20; // Runtime ReferenceError: foo is not defined
break;
default:
}
}
test(2);
tsc --target es2015 (or higher)
Expected behavior:
This should probably produce a compile time error: Block-scoped variable 'pen' used before its declaration. on reference to foo in case 2.
Actual behavior:
The code type-checks and compiles fine, however, when running the es2015 transpiled output, you will receive a ReferenceError error at runtime. Ideally this type of error could be caught at compile time.
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start with the provided switch-case example using TypeScript 2.5.3 and compile it with --target es2015 or higher. Trace the type-checking path for block-scoped let declarations and references across case clauses. Done means the reference to foo in case 2 produces a compile-time block-scoped-variable-before-declaration diagnostic instead of compiling to a runtime ReferenceError.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- typescript
- Domain
- compilers
- Issue type
- Bug
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 35/100