microsoft / microsoft/TypeScript
Add a Merge utility type
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
merge types
Suggestion
It would be great if you could merge two types. The closest I can get right now is a union:
type Test1 = { id: number, code: string }
type Test2 = { id: string, code: number }
type Test3 = Test1 | Test2
// This is not allowed, because x doesn't match Test1 OR Test2
export const x: Test3 = {
id: "bob",
code: "bob"
}
Basically I want to be able to create the following type by merging Test1 and Test2:
type Test3 = { id: number | string, code: number | string }
Perhaps there could be a utility to merge the types:
type Test3 = Merge<Test1, Test2>
Use Cases
React-Native has a few style types (TextStyle, ImageStyle, ViewStyle) and they are very useful for autocomplete and type checking in my code. The TextStyle has a property fontSize: number. I want to be able to redefine the TextStyle type to allow a string value for all of the properties (so that I can use my own variable system). Something like fontSize: "$fontSizeMD"
Examples
type EImageStyle = ImageStyle | Partial<Record<keyof ImageStyle, string>>
type ETextStyle = TextStyle | Partial<Record<keyof TextStyle, string>>
type EViewStyle = ViewStyle | Partial<Record<keyof ViewStyle, string>>
type EComponentStyle = EImageStyle | ETextStyle | EViewStyle
type EComponentStyles = { [P in keyof any]: EComponentStyle }
With the merge it would look like:
type EImageStyle = Merge<ImageStyle, Partial<Record<keyof ImageStyle, string>>>
type ETextStyle = Merge<TextStyle, Partial<Record<keyof TextStyle, string>>>
type EViewStyle = Merge<ViewStyle, Partial<Record<keyof ViewStyle, string>>>
type EComponentStyle = EImageStyle | ETextStyle | EViewStyle
type EComponentStyles = { [P in keyof any]: EComponentStyle }
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
Das Issue nennt keine Dateien, Tests oder Einstiegspunkte. Beginne damit zu untersuchen, wie TypeScript Utility-Typen darstellt und auswertet, und bestimme dann das beabsichtigte Verhalten eines Merge-Typs für sich überschneidende Eigenschaften, optionale Eigenschaften und inkompatible Typen. Die Aufgabe ist erledigt, wenn das Utility spezifiziert und implementiert ist und durch Type-Checking-Tests für die Beispiele und Randfälle abgedeckt wird.
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