microsoft / microsoft/TypeScript

Missing type error for invalid use of #private field

Offen
#43,711 2 Kommentare 1 Reaktion 0 zugewiesene Personen Auf GitHub ansehen

Dieses Issue hat noch niemand übernommen.

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

Beschreibung

Bug Report

ES #private fields are not source-positional, but scoped to each instance of a class. (That is unfortunate, in my humble opinion). However TypeScript is not detecting this case.

🔎 Search Terms

typescript no type error private field mixin

🕗 Version & Regression Information

4.3-beta (I didn't try earlier)

⏯ Playground Link

Playground link with relevant code

💻 Code
type Ctor<T = {}> = new (...args: any[]) => T

function FooMixin<C extends Ctor>(Base: C) {
  return class Foo extends Base {
    #foo = 123
    test(foo: Foo) {
      console.log(foo.#foo)
    }
  }
}

class Cat extends FooMixin(Object) {}
class Dog extends FooMixin(Object) {}

const c = new Cat
c.test(new Dog) // Should there be a type error here, perhaps?
🙁 Actual behavior

No type error, but only a runtime error.

🙂 Expected behavior

There should perhaps also be a type error, if that's possible.

Mixin docs

The TypeScript Mixin documentation advertises that people can use #private fields with class-factory mixins:

// Mixins may not declare private/protected properties
// however, you can use ES2020 private fields

This may lead people to the try them and they'll have this issue.

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

Beginne mit dem verlinkten TypeScript Playground und reproduziere das Mixin-Beispiel mit #private-Feldern, FooMixin, Cat und Dog. Untersuche das Verhalten der Typprüfung im Zusammenhang mit der Identität privater Felder und bestimme, welche Diagnose, falls überhaupt, für c.test(new Dog) ausgegeben werden sollte; die Aufgabe ist erst erledigt, wenn eine abgestimmte Semantik und entsprechende Regressionstests vorhanden sind.

Vom Indexierungsmodell aus dem Issue-Text verfasst.

Bewertung

Tech-Stack
javascript, typescript
Bereich
compilers
Issue-Typ
Bug
Schwierigkeit
5/5
Geschätzter Aufwand
Über eine Woche
Aktivitätsstatus
Veraltet
Klarheit
Größtenteils klar
Anfängerfreundlichkeit
30/100

Neue Issues direkt in Ihr Postfach

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