microsoft / microsoft/TypeScript
Add `trim` as new intrinsic for use with template literal types
Nessuno ha ancora preso questa issue.
- Lingua principale
- Go
- Stelle
- 111k
- Fork
- 14.3k
- Merge medio
- 2g 4h
- PR unite (30g)
- 132
Descrizione
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.
Guida per i contributori
Apri la guida per i contributori
Come iniziare
- Leggi tutta la issue e poi la guida ai contributi del progetto.
- Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
- Fai un fork del repository e lavora su un branch.
- Apri una pull request che faccia riferimento al numero della issue.
Direzione di ricerca
La issue cita i tipi intrinseci esistenti Uppercase, Lowercase, Capitalize e Uncapitalize, oltre alla issue correlata #41210, ma non include file di implementazione né test. Inizia esaminando queste definizioni intrinseche e la discussione correlata; il lavoro sarebbe completo quando fosse concordato un comportamento di trim intrinseco che gestisca i casi di spazi bianchi nei template literal indicati.
Scritto dal modello di indicizzazione a partire dal testo della issue.
Valutazione
- Stack tecnologico
- typescript
- Ambito
- compilers
- Tipo di issue
- Funzionalità
- Difficoltà
- 5/5
- Tempo stimato
- Più di una settimana
- Stato di attività
- Ferma
- Chiarezza
- Abbastanza chiara
- Idoneità per principianti
- 35/100