microsoft / microsoft/TypeScript

Support either type asserting `self` in classes, or properly ignoring workaround

Offen
#39,864 3 Kommentare 0 Reaktionen 0 zugewiesene Personen Auf GitHub ansehen

Dieses Issue hat noch niemand übernommen.

Bug Domain: classes Help Wanted
Vorherrschende Sprache
Go
Sterne
111k
Forks
14.3k
Ø Merge
2 T. 4 Std.
Gemergte PRs (30 T.)
132

Beschreibung

I currently have a scenario very similar to https://github.com/microsoft/TypeScript/issues/2000#issuecomment-546929745. While it would be fixed it TS natively supported overriding class methods (see that issue), I followed the outline of the linked comment, but am running into a small issue. This is not a huge blocker but rather a nice-to have to get proper type completion for my scenario.


export class WritableObjStream<T> extends (stream.Writable as any) {
    writableObjectMode: true

    write(object: T, cb?: (error: Error | null | undefined) => void): boolean {
        return super.write(...more details)
    }
}

Because we type assert the base class to any, TS has no knowledge of the superclass. I tried a few solutions, with errors included below:

(super as any).write() // 'super' must be followed by an argument list or member access.
let s: stream.Writable = undefined;
// @ts-ignore
s = super // Identifier expected.

return s.write() // 'super' must be followed by an argument list or member access, Expected 1-3 arguments, but got 0.

Luckily, it autocomplete is picking up on the proper type of s.write() now and giving me an argument error.
Also, from what I can gather super is treated as a keyword not an identifier, which results in the error above.

This may not be the most common of errors, but there may be a few scenarios/other use cases. I tried writing a few examples up but none made real-world sense besides this use-case of a workaround for overriding methods.

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 Klassenbeispiel im Issue und dem verlinkten TypeScript-Issue/-Kommentar, der den Workaround beschreibt. Untersuche, wie TypeScript in diesem Szenario super parst und typprüft, und stelle anschließend fest, ob Typassertionen für self unterstützt werden sollten oder ob der Workaround ausdrücklich abgelehnt werden sollte. Als erledigt gilt die Aufgabe, wenn das gewählte Verhalten durch Tests dokumentiert ist, die die gezeigten Beispiele abdecken.

Vom Indexierungsmodell aus dem Issue-Text verfasst.

Bewertung

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

Neue Issues direkt in Ihr Postfach

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