microsoft / microsoft/TypeScript

const/readonly assertion support for `Map` and `Set`

Offen
#62,312 3 Kommentare 1 Reaktion 0 zugewiesene Personen Auf GitHub ansehen

Dieses Issue hat noch niemand übernommen.

Awaiting More Feedback Suggestion
Vorherrschende Sprache
Go
Sterne
111k
Forks
14.3k
Ø Merge
2 T. 4 Std.
Gemergte PRs (30 T.)
132

Beschreibung

### 🔍 Search Terms

"as const", "as readonly", "const assertion", "keyed collections"

### ✅ Viability Checklist

- [x] This wouldn't be a breaking change in existing TypeScript/JavaScript code
- [x] This wouldn't change the runtime behavior of existing JavaScript code
- [x] This could be implemented without emitting different JS based on the types of the expressions
- [x] This isn't a runtime feature (e.g. library functionality, non-ECMAScript syntax with JavaScript output, new syntax sugar for JS, etc.)
- [x] This isn't a request to add a new utility type: https://github.com/microsoft/TypeScript/wiki/No-New-Utility-Types
- [x] This feature would agree with the rest of our Design Goals: https://github.com/Microsoft/TypeScript/wiki/TypeScript-Design-Goals

### ⭐ Suggestion

Have a way to take an existing `Map` or `Set` and assert it to be its readonly variant without needing to repeat the generic types.

### 📃 Motivating Example

Today we can create maps (and sets) with explicit and inferred generic types but in order to make them readonly the generic types have to be explicitly written, sometimes repeated:

```ts
const exampleMap1: ReadonlyMap = new Map([["a", 1], ["b", 2]]);
const exampleMap2: ReadonlyMap = new Map().set('a', 1).set('b', 2);
```

With something like const assertion support for `Map` (and `Set`) we'd be able to get readonly maps and sets without repeating the generic types:

```ts
const exampleMap1 = new Map([["a", 1], ["b", 2]]) as const;
const exampleMap2 = new Map().set("a", 1).set("b", 2) as const;
```

### 💻 Use Cases

1. What do you want to use this for? I want to use this for creating keyed collections that will be used elsewhere and I want to avoid modifications. (Note: this won't prevent runtime modifications but the same is true for const assertions on arrays and objects today.)
2. What shortcomings exist with current approaches? Repetative generic types and verbose code to make a keyed collection readonly.
3. What workarounds are you using in the meantime? Repeating the generic types and explicitly defining things as readonly keyed collections.

Beitragsleitfaden

Beitragsleitfaden öffnen

Erste Schritte

  1. Lies das ganze Issue und danach den Beitragsleitfaden des Projekts.
  2. Schreib ins Issue, dass du es übernimmst — das erspart doppelte Arbeit.
  3. Forke das Repository und arbeite in einem Branch.
  4. Öffne einen Pull Request, der die Issue-Nummer nennt.

Rechercherichtung

Beginne damit, das Verhalten von TypeScript-const-Assertions für Arrays und Objekte sowie die bestehenden Regeln zur Typprüfung von Map und Set zu überprüfen. Ermittle, wie readonly-Schlüsselsammlungen dargestellt werden sollten, ohne generische Typen zu wiederholen, und füge anschließend Abdeckung hinzu, die zeigt, dass die vorgeschlagenen Assertions readonly-Map- und -Set-Typen ableiten und gleichzeitig die bisherige JavaScript-Ausgabe beibehalten.

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

Neue Issues direkt in Ihr Postfach

Eine kurze Übersicht über anfängerfreundliche GitHub-Issues.