microsoft / microsoft/TypeScript
[Suggestion] Compiler Flag to treat types as immutable
Nessuno ha ancora preso questa issue.
- Lingua principale
- Go
- Stelle
- 111k
- Fork
- 14.4k
- Merge medio
- 1g 19h
- PR unite (30g)
- 117
Descrizione
TypeScript Version: 2.3.4
Code
I'm aware of #10725, but I want to take it one step further and add a compiler flag (e.g. --immutable) that will cause the compiler to infer Readonly, ReadonlyArray, ReadonlyMap, and ReadonlySet (and any other data structures I've missed) everywhere. The flag will also force you to use const. Thus eliminating mutability from the language.
// tsconfig.json
{
"compilerOptions": {
"immutable": true
}
}
// demo.ts
// band is automatically inferred as:
// Readonly<{
// name: string;
// members: ReadonlyArray<{
// name: string;
// instruments: ReadonlyArray<string>;
// }>;
// }>
const band = {
name: 'U2',
members: [
{
name: 'Bono',
instruments: ['vocals', 'rhythm guitar', 'harmonic']
},
{
name: 'The Edge',
instruments: ['lead guitar', 'keyboards', 'backing vocals']
},
// ...
]
};
// The following will cause compiler errors:
// band.name = 'Green Day';
// band.members.push({ name: 'Billie Joe Armstrong', instruments: ['lead vocals', 'guitar'] });
// ----------------------------------------------------------------------
// numberMap is automatically inferred to be ReadonlyMap<number, string>
const numberMap = new Map<number, string>([
[1, 'one'],
[2, 'two']
]);
// Compiler error:
// numberMap.set(3, 'three');
// letterSet is is automatically inferred to be ReadonlySet<string>
const letterSet = new Set<string>(['A', 'B', 'C', 'D']);
// Compiler error:
// letterSet.add('E');
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
Inizia esaminando la proposta, l’issue referenziata #10725 e la guida collegata di TypeScript sulle design proposals. Definisci il comportamento dell’opzione del compilatore per const enforcement e readonly inference negli esempi e nelle altre strutture dati; l’issue non fornisce file o test da eseguire, quindi il completamento richiederebbe un design concordato e la copertura corrispondente.
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
- 30/100