microsoft / microsoft/TypeScript

Make `DOMStringList` generic

Offen
#30,983 0 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.3k
Ø Merge
2 T. 4 Std.
Gemergte PRs (30 T.)
132

Beschreibung

Search Terms

DOMStringList, DOMStringMap, DOMString

Suggestion

I'd be nice if DOMStringList had an optional generic so that a more restricted string type could be used for the list item type. The generic could default to string so that existing code using the type does not break.

The implementation would be along the lines of:

interface DOMStringList<T extends string = string> {
    readonly length: number;
    contains(string: T): boolean;
    item(index: number): T | null;
    [index: number]: T;
}

Use Cases

Libraries that wrap native DOM methods may return the DOMStringLists but want more strict types.

Examples

const dbOpenRequest = indexedDB.open("store", 1);

dbOpenRequest.onupgradeneeded = () => {
  const db = dbOpenRequest.result;
  db.createObjectStore('fruits');
  db.createObjectStore('meats');
}

dbOpenRequest.onsuccess = () => {
  const db = dbOpenRequest.result;
  const storeNames = db.objectStoreNames as DOMStringList<'fruits' | 'meats'>;
};

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, etc.)
  • This feature would agree with the rest of TypeScript's Design Goals.

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 damit, die DOMStringList-Deklaration zu finden und zu prüfen, wie ihre length-, contains-, item- und indexed-access-Member typisiert sind. Implementiere den optionalen, auf string beschränkten Generic mit einem string-Default und verifiziere anschließend, dass sowohl die bestehende Form als auch die Verwendung von DOMStringList<'fruits' | 'meats'> weiterhin gültig sind.

Vom Indexierungsmodell aus dem Issue-Text verfasst.

Bewertung

Tech-Stack
typescript
Bereich
compilers
Issue-Typ
Feature
Schwierigkeit
2/5
Geschätzter Aufwand
1-3 Stunden
Aktivitätsstatus
Veraltet
Klarheit
Klar beschrieben
Anfängerfreundlichkeit
45/100

Neue Issues direkt in Ihr Postfach

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