microsoft / microsoft/TypeScript

Make `DOMStringList` generic

Abierto
#30,983 0 comentarios 1 reacción 0 asignados Ver en GitHub

Nadie ha tomado este issue todavía.

Awaiting More Feedback Suggestion
Lenguaje dominante
Go
Estrellas
111k
Forks
14.4k
Merge medio
1 d 19 h
PR fusionados (30 d)
117

Descripción

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.

Guía de contribución

Abrir la guía de contribución

Primeros pasos

  1. Lee el issue completo y luego la guía de contribución del proyecto.
  2. Comenta en el issue que vas a ocuparte — evita que dos personas hagan lo mismo.
  3. Haz un fork del repositorio y trabaja en una rama.
  4. Abre un pull request que haga referencia al número del issue.

Línea de trabajo

Empieza localizando la declaración de DOMStringList y revisa cómo están tipados sus miembros length, contains, item y de acceso indexado. Implementa el genérico opcional restringido a string con un valor predeterminado string y, después, verifica que tanto la forma existente como el uso restringido de DOMStringList<'fruits' | 'meats'> sigan siendo válidos.

Escrito por el modelo de indexación a partir del texto del issue.

Evaluación

Stack tecnológico
typescript
Área
compilers
Tipo de issue
Nueva funcionalidad
Dificultad
2/5
Tiempo estimado
1-3 horas
Estado de actividad
Estancado
Claridad
Bien especificado
Aptitud para principiantes
45/100

Recibe los nuevos issues en tu correo

Un resumen breve de issues de GitHub para principiantes.