microsoft / microsoft/TypeScript
Typeguards with destructuring parameters
Nadie ha tomado este issue todavía.
- Lenguaje dominante
- Go
- Estrellas
- 111k
- Forks
- 14.3k
- Merge medio
- 1 d 19 h
- PR fusionados (30 d)
- 117
Descripción
Search Terms
Typeguard
type narrowing
destructuring
Suggestion
It'd be nice to be able to have typeguard functions with destructuring arguments.
Use Cases
When manipulating tuples or other anonymous structured types, whose typeguard relies only on a small part of the object, it would make the code much easier to read.
Currently, typeguards cannot be used with destructuring arguments, which makes it a weird special case.
Examples
const val: Array<[string, boolean]> = ([] as Array<[string | undefined, boolean]>)
.filter(([foo, bar]: [string | undefined, boolean]): [foo, bar] is [string, boolean] => foo !== undefined);
const val2: Array<[string, boolean]> = ([] as Array<[string | undefined, boolean]>)
.filter((val: [string | undefined, boolean]): val is [string, boolean] => val[0] !== undefined);
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. new expression-level syntax)
Guía de contribución
Primeros pasos
- Lee el issue completo y luego la guía de contribución del proyecto.
- Comenta en el issue que vas a ocuparte — evita que dos personas hagan lo mismo.
- Haz un fork del repositorio y trabaja en una rama.
- Abre un pull request que haga referencia al número del issue.
Línea de trabajo
Empieza reproduciendo los ejemplos desestructurados y no desestructurados en el TypeScript Playground enlazado. El issue no menciona ningún archivo fuente ni ninguna prueba; rastrea cómo se manejan los predicados de tipo y los parámetros desestructurados. Se considera completado cuando el predicado desestructurado estrecha el tipo de tupla filtrado sin cambiar el JavaScript emitido, con cobertura de regresión.
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
- 5/5
- Tiempo estimado
- Más de una semana
- Estado de actividad
- Estancado
- Claridad
- Bastante claro
- Aptitud para principiantes
- 30/100