microsoft / microsoft/TypeScript

Generic types should be compatible

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

还没有人认领这个 Issue。

Bug Domain: check: Variance Relationships Help Wanted
主要语言
Go
星标
111k
派生
14.4k
平均合并
1 天 19 小时
30 天内合并 PR
117

描述

Bug Report

🔎 Search Terms
  • generic types not equal
🕗 Version & Regression Information
  • This is the behavior in every version I tried, and I reviewed the FAQ for entries about generics.
⏯ Playground Link

Playground link with relevant code

💻 Code
// These are the types of records in the database.
type TableToRecord = {
	a: { a: number }
	b: { b: string }
	c: { c: string[] }
}

type Table = keyof TableToRecord

// Create a union of {table, id} objects.
type Pointer<T extends Table = Table> = {
    [K in T]: {table: K, id: string}
}[T]

// Hover X to see this is a proper union type
type X = Pointer

// ✅ this works as expected.
declare function getRecord<T extends Table>(pointer: Pointer<T>): TableToRecord[T] 
const x = getRecord({table: "a", id: ""})

// ❌ this surprisingly doesn't work
declare function something(pointer: Pointer): void
const p: Pointer<"a"> = {table: "a", id: ""}
something(p)
function run<T extends Table>(pointer: Pointer<T>) {
    const x = something(pointer)
}

// ✅ However, this does work if run is generic on Pointer instead of Table.
declare function something2(pointer: Pointer): void
function run2<P extends Pointer>(pointer: P) {
    const x = something(pointer)
}

// ✅ Which then makes me wonder if its better to write getRecord this way
declare function getRecord2<P extends Pointer>(pointer: P): TableToRecord[P["table"]] 
const x2 = getRecord2({table: "a", id: ""})
🙁 Actual behavior

I'm surprised by this error where Pointer<T> where T extends Table doesn't satisfy the argument Pointer<Table>.

If we were talking arrays, Array<T> where T extends string | number, I'd imagine you should be able to pass that to a function that accepts Array<string | number> as an argument. But it is a bit tricky if that argument gets mutated by the function, e.g. pushing a number onto a string array. I think there's a fancy type-system word for this behavior?

But as I understand it, TypeScript is all structural comparison and since Pointer<T> unfurls into the union type, I'm curious where the problem lies and it seems to me like the type system should let this work...

I noticed when the generic param is P extends Pointer instead of T extends Table and then using Pointer<T>, then the code does work. But then that leads me to wonder if there's any difference between function getRecord<T extends Table>(pointer: Pointer<T>): TableToRecord[T] and function getRecord2<P extends Pointer>(pointer: P): TableToRecord[P["table"]] ...

🙂 Expected behavior

贡献指南

打开贡献指南

从这里开始

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

调研方向

从链接的 TypeScript Playground 开始,重现 Pointer 与 Pointer 的示例,然后将其与 generic-on-Pointer 变体进行比较。在判断该行为是 bug 还是类型系统的一项有意规则之前,先阅读 issue 中关于结构比较和变更安全性的说明。兼容性问题已有记录在案的解决方案,且报告的示例按照该解决方案运行,即表示完成。

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

评估

技术栈
typescript
领域
compilers
Issue 类型
缺陷
难度
5/5
预计耗时
一周以上
活跃度
停滞
描述清晰度
需要澄清
新手友好度
25/100

把新 issue 发到你的邮箱

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