microsoft / microsoft/TypeScript
Add `trim` as new intrinsic for use with template literal types
Dieses Issue hat noch niemand übernommen.
- Vorherrschende Sprache
- Go
- Sterne
- 111k
- Forks
- 14.3k
- Ø Merge
- 2 T. 4 Std.
- Gemergte PRs (30 T.)
- 132
Beschreibung
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.
Beitragsleitfaden
Erste Schritte
- Lies das ganze Issue und danach den Beitragsleitfaden des Projekts.
- Schreib ins Issue, dass du es übernimmst — das erspart doppelte Arbeit.
- Forke das Repository und arbeite in einem Branch.
- Öffne einen Pull Request, der die Issue-Nummer nennt.
Rechercherichtung
Das Issue nennt die vorhandenen intrinsischen Typen Uppercase, Lowercase, Capitalize und Uncapitalize sowie das zugehörige Issue #41210, enthält jedoch keine Implementierungsdateien oder Tests. Beginne mit der Überprüfung dieser intrinsischen Definitionen und der zugehörigen Diskussion; abgeschlossen wäre die Aufgabe, wenn ein abgestimmtes intrinsisches Trim-Verhalten festgelegt wäre, das die genannten Whitespace-Fälle in Template-Literals behandelt.
Vom Indexierungsmodell aus dem Issue-Text verfasst.
Bewertung
- Tech-Stack
- typescript
- Bereich
- compilers
- Issue-Typ
- Feature
- Schwierigkeit
- 5/5
- Geschätzter Aufwand
- Über eine Woche
- Aktivitätsstatus
- Veraltet
- Klarheit
- Größtenteils klar
- Anfängerfreundlichkeit
- 35/100