microsoft / microsoft/TypeScript
union of pattern template literals intersected with optional brand mutually assignable to one with incompatible brand
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
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](https://www.typescriptlang.org/play/?ts=6.0.3#code/C4TwDgpgBAKgtmAMgS2AVQHbIPYagXigAMAGAEgG8BnYAJ2QwHMBfIqAH2IEZKb6nWAWABQoSFABCtAIYYAJgA0CsBCnRZcUAGRQKUaQH4AXFABEAD1NRmAbhFjoU2XICay+ElSYceHXsMmpiBWtiJyEADGADbStNARuDRQAEYy8i4mTul2wgkYSanOCplpisqF2VAA9FVQADwAtA1QGNhQELS02LQivcIY0nAQVGDSEdAActjqProiUFAOKp7AyqS8dAwsRDkLS1llhB5q2rr6xmaW1ruL4I6lbkeqqKf+F0EhN+HRsfGJqxVXCVnC4bnkCqVipJIeUHjZqrVGs0Ol0esIRMw+gMhiMxtAAMrYIbAAAWWwAolEqNAKPNbuJjl4NBgAPK0QnEslMSnUtbkaibARsThEHgC-jbDhmKhEiCkilUiCmOn7GFPFbeXBsjlyrmMHnQPznQJXULCPZ3aEg9zPGZa9my+XcxWvY1mYLXOnfGJxKDggEPYHZOn+lKQoOHMMg+E1epNdqdbp9ZhAA)
### 💻 Code
```ts
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](https://stackoverflow.com/q/79929227/2887218). 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:
```ts
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:
```ts
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.
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
Riproduci l’esercizio nel TypeScript Playground collegato, quindi confrontalo con gli esempi senza union e senza brand opzionale presenti nell’issue. Esamina l’assegnabilità di union di template literal di pattern intersecate con proprietà opzionali; il lavoro è completato quando BrandX e BrandY non sono più assegnabili reciprocamente, mentre i casi di confronto riportati rimangono corretti.
Scritto dal modello di indicizzazione a partire dal testo della issue.
Valutazione
- Stack tecnologico
- typescript
- Ambito
- compilers
- Tipo di issue
- Bug
- Difficoltà
- 4/5
- Tempo stimato
- 3-5 giorni
- Stato di attività
- Tranquilla
- Chiarezza
- Abbastanza chiara
- Idoneità per principianti
- 52/100