microsoft / microsoft/TypeScript
Add a Merge utility type
Nessuno ha ancora preso questa issue.
- Lingua principale
- Go
- Stelle
- 111k
- Fork
- 14.3k
- Merge medio
- 2g 4h
- PR unite (30g)
- 132
Descrizione
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.
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
L’issue non indica file, test o punti di ingresso. Inizia esaminando come TypeScript rappresenta e valuta i tipi utility, quindi determina il comportamento previsto di un tipo Merge per proprietà sovrapposte, proprietà opzionali e tipi incompatibili. Il lavoro è completato quando l’utility è specificata e implementata e risulta coperta da test di type-checking per gli esempi e i casi limite.
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
- 25/100