microsoft / microsoft/TypeScript

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

Open
#63,420 2 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Bug Domain: Intersection
Dominant language
Go
Stars
111k
Forks
14.3k
Avg merge
2d 4h
Merged PRs (30d)
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](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.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Reproduce the assignment in the linked TypeScript Playground, then compare it with the non-union and non-optional-brand examples in the issue. Investigate assignability for unions of pattern template literals intersected with optional properties; done means BrandX and BrandY are no longer mutually assignable while the reported comparison cases remain correct.

Written by the indexing model from the issue text.

Assessment

Tech stack
typescript
Domain
compilers
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Quiet
Clarity
Mostly clear
Newbie friendliness
52/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.