microsoft / microsoft/TypeScript

Missing error if ClassDeclaration is used in Statement position

Open
#22,682 4 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Bug Domain: Parser Effort: Casual Help Wanted
Dominant language
Go
Stars
111k
Forks
14.3k
Avg merge
2d 4h
Merged PRs (30d)
132

Description

TypeScript Version: 2.8.0-dev.20180318

Search Terms: class statement

Code

let codition = false as boolean;

if (condition)
    class C {} // runtime error if transpiled to ES2015 or above: Unexpected token class
if (condition)
    enum E { Foo } // should probably be an error?

new C(); // runtime error if transpiled to ES5, because 'C' is undefined
E.Foo; // runtime error: cannot read property 'Foo' of 'E'

Expected behavior:

Expected a compile error on class C and maybe on enum E

Actual behavior:
no error for these declarations, instead I get a runtime error when targeting ES2015 or above.

Playground Link:

Related Issues:

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by reproducing the provided conditional class and enum examples with the specified TypeScript version and compiler targets. Trace how declarations in statement position are parsed and checked, then verify that invalid cases produce compile errors while valid declaration contexts remain unaffected.

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
38/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.