microsoft / microsoft/TypeScript

union of pattern template literals intersected with optional brand mutually assignable to one with incompatible brand

Ouverte
#63,420 2 commentaires 0 réactions 0 personnes assignées Voir sur GitHub

Personne n'a encore pris cette issue.

Bug Domain: Intersection
Langage dominant
Go
Étoiles
111k
Forks
14.3k
Merge moyen
2 j 4 h
PR mergées (30 j)
132

Description

🔎 Search Terms

brand, flavor, pattern template literal, placeholder, union, intersection, optional, assignable

🕗 Version & Regression Information
  • This changed between versions 4.3.0-dev.20210330 and 4.3.0-dev.20210331
  • This changed in commit or PR #43440, I think
⏯ Playground Link

Playground link

💻 Code
type TmpLitUnion = `0${string}` | `1${string}`
type BrandX = TmpLitUnion & { a?: "x" };
type BrandY = TmpLitUnion & { a?: "y" };
declare const brandY: BrandY;
const brandX: BrandX = brandY; // <-- no error
🙁 Actual behavior

BrandX and BrandY are mutually assignable, even though they have an incompatible optional a property.

🙂 Expected behavior

There should be an error when trying to assign a BrandY to a BrandX or vice versa.

Additional information about the issue

This is from a Stack Overflow question. Looks like optional brands (this is called... "flavored" I guess?) don't distinguish between unions-of-pattern-template-literals. If you get rid of the union, you get the expected error:

type TmpLit = `0${string}`;
type BrandX = TmpLit & { a?: "x" };
type BrandY = TmpLit & { a?: "y" };
declare const brandY: BrandY;
const brandX: BrandX = brandY; // <-- error

Or if you add something else to the union, you get the expected error:

type TmpLitUnionOrSomethingElse = `0${string}` | `1${string}` | "somethingElse"
type BrandX = TmpLitUnionOrSomethingElse & { a?: "x" };
type BrandY = TmpLitUnionOrSomethingElse & { a?: "y" };
declare const brandY: BrandY;
const brandX: BrandX = brandY; // <-- error

Or if the brand is not optional, etc.

Not sure if this is a bug or a design limitation, or somehow intentional.

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

Reproduisez l’exercice dans le TypeScript Playground lié, puis comparez-le aux exemples sans union et sans marque optionnelle de l’issue. Étudiez l’assignabilité des unions de littéraux de gabarit de pattern intersectées avec des propriétés optionnelles ; le travail est terminé lorsque BrandX et BrandY ne sont plus mutuellement assignables, tandis que les cas de comparaison signalés restent corrects.

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

Évaluation

Stack technique
typescript
Domaine
compilers
Type d'issue
Bug
Difficulté
4/5
Temps estimé
3-5 jours
Activité
Calme
Clarté
Plutôt claire
Accessibilité débutants
52/100

Recevez les nouvelles issues par e-mail

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