microsoft / microsoft/TypeScript
Allow identifying readonly properties in mapped types
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
Search Terms
Pretty much the ones in the title...
Suggestion
Any TypeScript modifier should be allowed to be easily identified in mapped types. In particular, readonly should, but possibly others too, such as ?.
Use Cases
Define mapped types where the values depend on whether the property is readonly or not. At the moment, the following madness is necessary for this:
type Or<A extends boolean, B extends boolean> = A extends true
? true
: B extends true
? true
: false
type Equals<X, Y> =
(<T> () => T extends X ? 1 : 2) extends
(<T> () => T extends Y ? 1 : 2)
? true
: false
type IsReadonly<O extends Record<any, any>, P extends keyof O> =
Not<Equals<{[_ in P]: O[P]}, {-readonly [_ in P]: O[P]}>>
I would like to see some syntactic sugar similar to (not intended to be a full implementation):
type Writable<T> = {
[P in keyof T]: isreadonly T[P] ? never : T[P]
}
In my case, I would like to define a hyperscript-style function which accepts a series of attributes for the creation of HTML elements. For this, I reuse the interfaces that TypeScript defines, such as HTMLAnchorElement, but obviously readonly properties (among others) need to be excluded.
Other people seem to have similar use cases, e.g. https://stackoverflow.com/questions/52443276/how-to-exclude-getter-only-properties-from-type-in-typescript
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.
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
Beginne damit, zu prüfen, wie TypeScript gemappte Typen und Eigenschaftsmodifikatoren in seinem Compiler darstellt. Vergleiche die vorgeschlagene readonly-Erkennungssyntax mit dem bestehenden Verhalten gemappter Typen und verwandten Anwendungsfällen für Modifikatoren. Als abgeschlossen gilt die Arbeit, wenn ein abgestimmtes Design und ein Verhalten des Typsystems zur Identifizierung von readonly-Eigenschaften vorliegt, mit Abdeckung der beschriebenen Szenarien für gemappte Typen.
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
- 35/100