microsoft / microsoft/TypeScript

Add a Merge utility type

Ouverte
#35,627 8 commentaires 14 réactions 0 personnes assignées Voir sur GitHub

Personne n'a encore pris cette issue.

Awaiting More Feedback Suggestion
Langage dominant
Go
Étoiles
111k
Forks
14.3k
Merge moyen
2 j 4 h
PR mergées (30 j)
132

Description

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.

Guide de contribution

Ouvrir le guide de contribution

Par où commencer

  1. Lisez l'issue en entier, puis le guide de contribution du projet.
  2. Signalez en commentaire que vous la prenez — cela évite que deux personnes fassent le même travail.
  3. Forkez le dépôt et travaillez sur une branche.
  4. Ouvrez une pull request qui référence le numéro de l'issue.

Piste de recherche

L’issue ne nomme aucun fichier, test ou point d’entrée. Commencez par examiner comment TypeScript représente et évalue les types utilitaires, puis déterminez le comportement attendu d’un type Merge pour les propriétés qui se chevauchent, les propriétés optionnelles et les types incompatibles. Le travail est terminé lorsque l’utilitaire est spécifié et implémenté, et couvert par des tests de vérification de types pour les exemples et les cas limites.

Rédigé par le modèle d'indexation à partir du texte de l'issue.

Évaluation

Stack technique
typescript
Domaine
compilers
Type d'issue
Fonctionnalité
Difficulté
5/5
Temps estimé
Plus d'une semaine
Activité
À l'abandon
Clarté
Plutôt claire
Accessibilité débutants
25/100

Recevez les nouvelles issues par e-mail

Un résumé court des issues GitHub adaptées aux débutants.