microsoft / microsoft/TypeScript

Allow subclasses to declare overloads of methods

Offen
#54,747 1 Kommentar 0 Reaktionen 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

Suggestion

Allow subclasses to declare an overload of a superclass method:

This would be useful with addEventListener() in order to provide the correct event type for an event name:

class MyElement extends HTMLElement {
  declare addEventListener(type: 'my-event', listener: (this: MyElement, ev: MyEvent) => any, options?: boolean | AddEventListenerOptions): void;
}

🔍 Search Terms

Various combinations: addEventListener subclass declare overload method

A lot of issues were returned from queries, I could have missed something.

✅ Viability Checklist

My suggestion meets these guidelines:

  • This wouldn't be a breaking change in existing TypeScript/JavaScript code
  • This wouldn't change the runtime behavior of existing JavaScript code
  • This could be implemented without emitting different JS based on the types of the expressions
  • This isn't a runtime feature (e.g. library functionality, non-ECMAScript syntax with JavaScript output, new syntax sugar for JS, etc.)
  • This feature would agree with the rest of TypeScript's Design Goals.

⭐ Suggestion

See above. I'm not sure the reason for not allowing declare on methods currently, so hopefully that could be relaxed.

📃 Motivating Example

See above.

Right now you would have to augment the HTMLElementEventNameMap, but that makes events global, which they might not be if they don't bubble.

💻 Use Cases

See above.

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 der Untersuchung, wie TypeScript declare-Methoden und geerbte Methodenüberladungen behandelt. Vergleiche die vorgeschlagene addEventListener-Deklaration der Unterklasse mit dem aktuellen Verhalten und ermittle die erforderlichen Regeln für die Typprüfung. Die fertige Lösung sollte die Überladung der Unterklasse ermöglichen, ohne das erzeugte JavaScript zu ändern, und gleichzeitig die Kompatibilität mit der Methode der Oberklasse wahren.

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.