microsoft / microsoft/TypeScript
[Suggestion] Compiler Flag to treat types as immutable
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
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');
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
Beginnen Sie mit der Durchsicht des Vorschlags, des referenzierten Issue #10725 und der verlinkten TypeScript-Anleitung für Designvorschläge. Definieren Sie das Verhalten der Compiler-Option für const enforcement und readonly inference anhand der Beispiele und anderer Datenstrukturen; das Issue enthält keine Dateien oder auszuführenden Tests, daher würde der Abschluss ein abgestimmtes Design und eine entsprechende Abdeckung erfordern.
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
- 30/100