microsoft / microsoft/TypeScript
Nullish coalescing operator but for conditional types
Personne n'a encore pris cette issue.
- Langage dominant
- Go
- Étoiles
- 111k
- Forks
- 14.4k
- Merge moyen
- 1 j 19 h
- PR mergées (30 j)
- 117
Description
🔍 Search Terms
nullish coalescing operator
conditional types
✅ Viability Checklist
- 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, new syntax sugar for JS, etc.)
- This isn't a request to add a new utility type: https://github.com/microsoft/TypeScript/wiki/No-New-Utility-Types
- This feature would agree with the rest of our Design Goals: https://github.com/Microsoft/TypeScript/wiki/TypeScript-Design-Goals
⭐ Suggestion
Add syntactic sugar to allow this:
type T = A extends B ?? C
to be shorthand of:
type T = A extends B ? A : C
📃 Motivating Example
When working with large complicated types, it is often that you need to check that a type extends another type before passing it as a type argument to another type.
For example, ensuring it is an array before passing it to something that requires it to be an array:
type Foo<T> = Bar<
Some<Really<Deep<Complex<Type<Utility<T>>>>>> extends readonly unknown[]
? Some<Really<Deep<Complex<Type<Utility<T>>>>>>
: never
>;
type Bar<T extends readonly unknown[]> = ...;
The more complex the type you need to check, the more you need to repeat yourself to pass the same type in again.
This proposal aims to address that.
type Foo<T> = Bar<
Some<Really<Deep<Complex<Type<Utility<T>>>>>> extends readonly unknown[] ?? never
>;
💻 Use Cases
- What do you want to use this for?
Making my type code more readable and easier to maintain.
- What shortcomings exist with current approaches?
The need to repeat yourself. Updates to the type need to be make in two places.
- What workarounds are you using in the meantime?
The long-form syntax.
Guide de contribution
Ouvrir le guide de contribution
Par où commencer
- Lisez l'issue en entier, puis le guide de contribution du projet.
- Signalez en commentaire que vous la prenez — cela évite que deux personnes fassent le même travail.
- Forkez le dépôt et travaillez sur une branche.
- Ouvrez une pull request qui référence le numéro de l'issue.
Piste de recherche
L’issue ne nomme aucun fichier d’implémentation ni aucun test. Commencez par suivre la manière dont les types conditionnels sont analysés et vérifiés, puis déterminez les changements de syntaxe et de système de types nécessaires pour que A extends B ?? C se comporte comme A extends B ? A : C ; le travail est terminé lorsque la forme abrégée est acceptée et présente le comportement de type équivalent indiqué.
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é
- Clairement spécifiée
- Accessibilité débutants
- 30/100