microsoft / microsoft/TypeScript

`tsc` with `--target ESNext` fails but the same configuration succeeds on the playground.

Open
#45,297 3 comments 1 reaction 1 assignee View on GitHub

@armanio123 is already working on this.

Since Aug 2, 2021.

Needs Investigation Rescheduled
Dominant language
Go
Stars
111k
Forks
14.4k
Avg merge
1d 19h
Merged PRs (30d)
117

Description

Bug Report

tsc causes errors for --target ESNext for some order of initialized variables and variables initialized in the constructor.

Why does the code produce errors for ESNext and no error for any other target?

🔎 Search Terms
  • ESNext
🕗 Version & Regression Information

The command line fails on the code where the playground succeeds.

⏯ Playground Link

You have to set the --target to ESNext for this script Playground link with relevant code

💻 Code

Compiling this code with

./node_modules/.bin/tsc --target ESNext x.ts

function writeVal(a: number, b: any): number {
    console.log(‘writeVal’, a, b);
    return a;
}
class TestClass {
    a: number;
    public b = writeVal(2, this.z);    
    constructor(public z: number) {
        this.a = writeVal(1, this.z);
    }
    public c = writeVal(3, this.z);
}

I get the following error:

x.ts:7:33 - error TS2729: Property ‘z’ is used before its initialization.

7     public b = writeVal(2, this.z);
                                  ~

  x.ts:8:17
    8     constructor(public z: number) {
                      ~~~~~~~~~~~~~~~~
    ‘z’ is declared here.

x.ts:11:33 - error TS2729: Property ‘z’ is used before its initialization.

11     public c = writeVal(3, this.z);
                                   ~

  x.ts:8:17
    8     constructor(public z: number) {
                      ~~~~~~~~~~~~~~~~
    ‘z’ is declared here.


Found 2 errors.
🙁 Actual behavior

Running the code above in the playground there is no error, but tsc throws errors and fails to compile.

🙂 Expected behavior

Playground and the compiler should produce the same errors.

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.

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.