microsoft / microsoft/TypeScript

`readonly` mapped type modifiers for `Map` / `ReadonlyMap`

Aperta
#30,633 2 commenti 3 reazioni 0 assegnatari Vedi su GitHub

Nessuno ha ancora preso questa issue.

In Discussion Suggestion
Lingua principale
Go
Stelle
111k
Fork
14.3k
Merge medio
2g 4h
PR unite (30g)
132

Descrizione

Search Terms

ReadonlyMap
ReadWrite
Writable

Suggestion

3.4.0-rc introduced the ability to use readonly mapped type modifiers with arrays.

(https://devblogs.microsoft.com/typescript/announcing-typescript-3-4-rc/)

It would be useful if the same syntax worked on

Use Cases

This is useful in any library dealing with immutable versions of types. It would allow mapped types to easily convert between Map and ReadonlyMap.

Examples

type ReadWrite<T> = { -readonly [P in keyof T]: T[P] };
const map: ReadWrite<ReadonlyMap<string, string>> = new Map();

map.put('a', 'c'); // this should work, since readonly has been removed.
type Readonly<T> = { readonly [P in keyof T]: T[P] };
const map: Readonly<Map<string, string>> = new Map();

map.put('a', 'c'); // this should not work, since readonly has been applied

Similar examples using Array instead of Map didn't work before 3.4 and did work afterwards.

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

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Direzione di ricerca

Inizia riproducendo gli esempi di tipi mappati ReadWrite e Readonly dell'issue e confronta il loro comportamento con quello di Array introdotto in TypeScript 3.4. Traccia la gestione dei tipi mappati nel compilatore per determinare il design richiesto; il lavoro è completato quando gli esempi di Map/ReadonlyMap si comportano come specificato senza modificare il JavaScript emesso.

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
25/100

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.