microsoft / microsoft/TypeScript

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

未关闭
#63,420 2 条评论 0 个 reaction 已指派 0 人 在 GitHub 查看

还没有人认领这个 Issue。

Bug Domain: Intersection
主要语言
Go
星标
111k
派生
14.3k
平均合并
2 天 4 小时
30 天内合并 PR
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

💻 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.

贡献指南

打开贡献指南

从这里开始

  1. 先读完整个 Issue,再读项目的贡献指南。
  2. 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
  3. Fork 仓库,在一个分支上完成修改。
  4. 提交 Pull Request,并在描述里引用这个 Issue 编号。

调研方向

在链接的 TypeScript Playground 中复现该赋值,然后将其与 issue 中不含 union 和不含可选 brand 的示例进行比较。调查与可选属性相交的模式模板字面量 union 的可赋值性;完成标准是 BrandX 和 BrandY 不再可以相互赋值,同时报告的比较用例仍然正确。

由索引模型根据 Issue 内容生成。

评估

技术栈
typescript
领域
compilers
Issue 类型
缺陷
难度
4/5
预计耗时
3-5 天
活跃度
冷清
描述清晰度
基本清楚
新手友好度
52/100

把新 issue 发到你的邮箱

精选适合新手参与的 GitHub issue 摘要。