microsoft / microsoft/TypeScript

should error on assignment that shadows a prototype method

Offen
#60,649 2 Kommentare 3 Reaktionen 0 zugewiesene Personen Auf GitHub ansehen

Dieses Issue hat noch niemand übernommen.

Awaiting More Feedback Suggestion
Vorherrschende Sprache
Go
Sterne
111k
Forks
14.3k
Ø Merge
1 T. 19 Std.
Gemergte PRs (30 T.)
117

Beschreibung

🔎 Search Terms

own property, shadow, prototype, method, field, inheritance

🕗 Version & Regression Information
⏯ Playground Link

https://www.typescriptlang.org/play/?ts=5.8.0-dev.20241201#code/MYGwhgzhAEBiD29oG8BQ0PWPAdhALgE4Cuw+8hAFAJQrqYP4AWAlhAHQAOh85+AnpwCmAWSHN4AE2gBeaDVkA+Og1WZseeCCHsQ8AOaUA5BCZhJ8AO4xuvXoKHQAtuKZSj1ANz01AXx-Q-gy2fA5iEpIKaGoYGhBaOnqGRi4R0LjQIfbCHt4M-r5AA

💻 Code
class Foo {
    constructor() {
        this.prototypeMethod = () => {
            console.log('shadows prototype method');
        }
    }
    prototypeMethod() {
        console.log('method on prototype');
    }
}
🙁 Actual behavior

no error

🙂 Expected behavior

error - something like "Type 'Foo' has no instance property named 'prototypeMethod'. If you meant to reassign the method 'prototypeMethod', it must be a function-valued instance property instead of a prototype method.".

I expect an error here because the code is effectively equivalent to

class Foo {
    prototypeMethod = () => {
        console.log('shadows prototype method');
    };
    prototypeMethod() {
        console.log('method on prototype');
    }
}

which is a TS error (see https://github.com/microsoft/TypeScript/issues/13141).

Furthermore, it breaks error reporting in subclasses. The following code correctly reports a TS error due to the extended class not being able to override an instance property with a method:
(playground)

class Foo {
    instanceProperty = () => {
        console.log('function-valued instance property');
    }
}

class Bar extends Foo {
    // TS ERROR: Class 'Foo' defines instance member property 'instanceProperty', but extended class 'Bar' defines it as instance member function.(2425)
    instanceProperty() {
        console.log('overridden with method')
    }
}

const b = new Bar();
b.instanceProperty(); // prints 'function-valued instance property'

However, this does not (playground)

class Foo {
    constructor() {
        this.method = () => {
            console.log('function-valued instance property')
        }
    }
    method() {
        console.log('method');
    }

}

class Bar extends Foo {
    // no error here  :(
    method() {
        console.log('overridden with method')
    }
}

const b = new Bar();
b.method(); // prints 'function-valued instance property'
Additional information about the issue

inspired by https://github.com/typescript-eslint/typescript-eslint/issues/10427

cc @miguel-leon

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.

Rechercherichtung

Reproduziere die Konstruktorzuweisungs- und Unterklassenbeispiele im verlinkten TypeScript Playground und vergleiche sie anschließend mit dem entsprechenden Fall einer instanzbezogenen Eigenschaft mit Funktionswert. Verfolge die Prüfungen des Compilers für Klassenmitglieder und Vererbung; abgeschlossen ist die Aufgabe, wenn Zuweisungen, die Prototypmethoden verdecken, die erwartete Diagnose erzeugen und Überschreibungen in Unterklassen konsistent geprüft werden.

Vom Indexierungsmodell aus dem Issue-Text verfasst.

Bewertung

Tech-Stack
javascript, typescript
Bereich
compilers
Issue-Typ
Bug
Schwierigkeit
4/5
Geschätzter Aufwand
3-5 Tage
Aktivitätsstatus
Ruhig
Klarheit
Größtenteils klar
Anfängerfreundlichkeit
45/100

Neue Issues direkt in Ihr Postfach

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