microsoft / microsoft/TypeScript

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

Offen
#45,297 3 Kommentare 1 Reaktion 1 zugewiesene Person Auf GitHub ansehen

@armanio123 arbeitet bereits daran.

Seit 02.8.2021.

Needs Investigation Rescheduled
Vorherrschende Sprache
Go
Sterne
111k
Forks
14.4k
Ø Merge
1 T. 19 Std.
Gemergte PRs (30 T.)
117

Beschreibung

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.

Beitragsleitfaden

Beitragsleitfaden öffnen

Erste Schritte

  1. Lies das ganze Issue und danach den Beitragsleitfaden des Projekts.
  2. Schreib ins Issue, dass du es übernimmst — das erspart doppelte Arbeit.
  3. Forke das Repository und arbeite in einem Branch.
  4. Öffne einen Pull Request, der die Issue-Nummer nennt.

Bewertung

Dieses Issue wurde noch nicht bewertet.

Neue Issues direkt in Ihr Postfach

Eine kurze Übersicht über anfängerfreundliche GitHub-Issues.