镜像站点 · 本页由第三方 GitHub 只读镜像提供,非 GitHub 官方站点,不接受任何登录或凭据输入。前往 github.com
Skip to content

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

Description

@quassnoi

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

Activity

  1. changed the title [-]Symbol-keyed properties cannot be initialized within constructor if the symbol name comes from a static property (e.g. [Symbol.iterator])[/-] [+]Symbol-keyed properties cannot be initialized within constructor if the symbol key value comes from a static property (e.g. [Symbol.iterator])[/+] on Apr 2, 2023
  2. whzx5byb commented on Apr 2, 2023

    @whzx5byb

    Similar to #49594.

  3. changed the title [-]Symbol-keyed properties cannot be initialized within constructor if the symbol key value comes from a static property (e.g. [Symbol.iterator])[/-] [+]Class properties cannot be initialized within constructor if the property key comes from a static class property (e.g. [Symbol.iterator])[/+] on Apr 3, 2023
  4. LoganDark commented on May 30, 2024

    @LoganDark

    Just ran into this issue, super annoying. Can't assign Symbol.iterator in the constructor.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Labels

Needs InvestigationThis issue needs a team member to investigate its status.RescheduledThis issue was previously scheduled to an earlier milestone

Type

No type

Projects

No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions