microsoft / microsoft/TypeScript

Class properties cannot be initialized within constructor if the property key comes from a static class property (e.g. [Symbol.iterator])

Offen
#53,626 3 Kommentare 1 Reaktion 1 zugewiesene Person Auf GitHub ansehen

@rbuckton arbeitet bereits daran.

Seit 10.4.2023.

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

Beschreibung

Bug Report

🔎 Search Terms

symbol property constructor 2564

🕗 Version & Regression Information
  • This changed between versions 4.6.4 and 4.7.4
⏯ Playground Link

Playground link with relevant code

💻 Code
const standaloneSymbol = Symbol("standalone");

class SymbolClass {
    static readonly staticSymbol = Symbol("static");
}

interface IHaveSymbols {
    [standaloneSymbol]: number;
    [SymbolClass.staticSymbol]: number;
}

class HavePropertyInitializedSymbols implements IHaveSymbols {
    [standaloneSymbol] = 1;
    [SymbolClass.staticSymbol] = 2;
}

class HaveOverriddenSymbols extends HavePropertyInitializedSymbols {
    constructor() {
        super();
        this[standaloneSymbol] = 3;
        this[SymbolClass.staticSymbol] = 4;
    }
}

class HaveConstructorInitializedSymbols implements IHaveSymbols {
    [standaloneSymbol]: number;
    [SymbolClass.staticSymbol]: number;
    constructor() {
        this[standaloneSymbol] = 3;
        this[SymbolClass.staticSymbol] = 4;
    }
}
🙁 Actual behavior
Property '[SymbolClass.staticSymbol]' has no initializer and is not definitely assigned in the constructor.(2564)
(property) SymbolClass.staticSymbol: typeof SymbolClass.staticSymbol

It's wrong because the property is definitely assigned in the constructor

🙂 Expected behavior

No error, because the property is definitely assigned in the constructor

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.