microsoft / microsoft/TypeScript
Make `DOMStringList` generic
Dieses Issue hat noch niemand übernommen.
- 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
Erste Schritte
- Lies das ganze Issue und danach den Beitragsleitfaden des Projekts.
- Schreib ins Issue, dass du es übernimmst — das erspart doppelte Arbeit.
- Forke das Repository und arbeite in einem Branch.
- Ö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