microsoft / microsoft/TypeScript

Make `DOMStringList` generic

Aperta
#30,983 0 commenti 1 reazione 0 assegnatari Vedi su GitHub

Nessuno ha ancora preso questa issue.

Awaiting More Feedback Suggestion
Lingua principale
Go
Stelle
111k
Fork
14.3k
Merge medio
1g 19h
PR unite (30g)
117

Descrizione

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.

Guida per i contributori

Apri la guida per i contributori

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Direzione di ricerca

Inizia individuando la dichiarazione di DOMStringList e verifica come sono tipizzati i suoi membri length, contains, item e di accesso indicizzato. Implementa il generico opzionale vincolato a string con un valore predefinito string, quindi verifica che sia la forma esistente sia l’uso vincolato di DOMStringList<'fruits' | 'meats'> rimangano validi.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Valutazione

Stack tecnologico
typescript
Ambito
compilers
Tipo di issue
Funzionalità
Difficoltà
2/5
Tempo stimato
1-3 ore
Stato di attività
Ferma
Chiarezza
Specificata chiaramente
Idoneità per principianti
45/100

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.