microsoft / microsoft/TypeScript
Add `trim` as new intrinsic for use with template literal types
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
intrinsic
trim
whitespace
possible related: https://github.com/microsoft/TypeScript/issues/41210
Suggestion
Add trim as a new intrinsic alongside the existing intrinsic types:
Uppercase
Lowercase
Capitalize
Uncapitalize
Use Cases
Trim can be implemented with the current types pretty easily by doing something like:
type Whitespace = '\n' | ' ';
type Trim<T> = T extends `${Whitespace}${infer U}` ? Trim<U> : T extends `${infer U}${Whitespace}` ? Trim<U> : T;
But this can quickly run into depth limits if there is a lot of repeated whitespace.
type Whitespace = | '\n'| ' ' | 'x' // Add x as whitespace to make example easier to read
type Trim<T> = T extends `${Whitespace}${infer U}` ? Trim<U> : T extends `${infer U}${Whitespace}` ? Trim<U> : T;
type Test = Trim<`abc
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
`>
This can be improved by adding strings of repeated whitespace to the Whitespace type, but will still run into limitations:
type Whitespace = | '\n' | ' ' | ' ' | ' ' | ' ' | ' ' | ' '
type Trim<T> = T extends `${Whitespace}${infer U}` ? Trim<U> : T extends `${infer U}${Whitespace}` ? Trim<U> : T;
This seems like a good case for an intrinsic implementation since it is a fairly simple and common operation on strings.
One use case would be to correctly type template literal helpers that trim leading indent whitespace, but there are probably many cases where trimming whitespace from a string would be useful.
Examples
Trim<`
abc
`> // 'abc'
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 menciona los tipos intrínsecos existentes Uppercase, Lowercase, Capitalize y Uncapitalize, además del issue relacionado #41210, pero no incluye archivos de implementación ni tests. Empieza revisando esas definiciones de tipos intrínsecos y la discusión relacionada; se consideraría terminado cuando exista un comportamiento de trim intrínseco acordado que gestione los casos de espacios en blanco de template literals indicados.
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
- 35/100