microsoft / microsoft/TypeScript

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

オープン
#63,420 コメント 2 件 リアクション 0 件 担当者 0 名 GitHub で見る

まだ誰も着手していません。

Bug Domain: Intersection
主要言語
Go
スター
111k
フォーク
14.3k
平均マージ
2日 4時間
マージ済み PR(30日)
132

説明

### 🔎 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.

コントリビューションガイド

コントリビューションガイドを開く

はじめの一歩

  1. issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
  2. 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
  3. リポジトリをフォークし、ブランチを切って変更します。
  4. issue 番号を参照したプルリクエストを送ります。

調査の方向性

リンクされた TypeScript Playground で課題を再現し、その後、issue にある union なしおよび optional brand なしの例と比較してください。optional properties と交差する pattern template literals の union に対する代入可能性を調査してください。完了条件は、BrandX と BrandY が相互に代入可能でなくなり、報告されている比較ケースが引き続き正しいことです。

索引モデルが issue の本文から書いたものです。

評価

技術スタック
typescript
領域
compilers
issue の種類
バグ
難易度
4/5
見積もり時間
3〜5日
活発さ
静か
明瞭さ
おおむね明確
初心者へのやさしさ
52/100

新しい issue をメールで受け取る

初心者向けの GitHub issue を短くまとめたダイジェスト。