microsoft / microsoft/TypeScript

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

Open
#53,626 3 comments 1 reaction 1 assignee View on GitHub

@rbuckton is already working on this.

Since Apr 10, 2023.

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

Description

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

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.