microsoft / microsoft/TypeScript
Type named capture groups better
Nadie ha tomado este issue todavía.
- Lenguaje dominante
- Go
- Estrellas
- 111k
- Forks
- 14.3k
- Merge medio
- 2 d 4 h
- PR fusionados (30 d)
- 132
Descripción
Search Terms
named regexp, named capture groups
Motivation
Currently named capture groups are a bit of a pain in TypeScript:
- All names are of type string even if the regexp doesn't have that named group.
- You need to use non-null assertion for
.groupseven when that is the only possibility.
Suggestion
I propose making RegExp higher order on its named capture groups so that .groups is well typed.
// Would have type: RegExp<{ year: string, month: string }>
const date = /(?<year>[0-9]{4})-(?<month>[0-9]{2})/
const match = someString.match(date)
if (match) {
// match.groups type would be { year: string, month: string }
// currently is undefined | { [key: string]: string }
}
// Would have type RegExp<{ year: string, month?: string }>
const optionalMonth = /(?<year>[0-9]{4})(-(?<month>[0-9]{2}))?/
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
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
El issue no nombra archivos de implementación ni pruebas. Empieza revisando cómo se tipan actualmente RegExp y los grupos de coincidencia, y determina después si los grupos con nombre pueden inferirse como se propone; se considera terminado cuando los grupos de captura con nombre y opcionales se representan con precisión sin cambiar el JavaScript emitido.
Escrito por el modelo de indexación a partir del texto del issue.
Evaluación
- Stack tecnológico
- javascript, 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
- 25/100