Narrowing of generic this is inconsistent with variable narrowing

未关闭
#64,186 0 条评论 0 个 reaction 已指派 0 人 在 GitHub 查看

还没有人认领这个 Issue。

评估

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

调研方向

从链接的 TypeScript Playground 开始,精简针对 direct this、aliased this 和 parameter narrowing 提供的 generic-union 示例。Issue 中没有指定 repository 文件或 test,因此请定位 compiler narrowing 的入口点,并添加一个覆盖所示案例的 regression test。当 direct this 和 aliased this 的 narrowing 产生一致的 diagnostics 时,即视为完成。

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

描述

Possible Improvement
🔎 Search Terms

generic union constraint narrowing this

🕗 Version & Regression Information
  • This is the behavior in every version I tried
⏯ Playground Link

https://www.typescriptlang.org/play/?noImplicitReturns=false#code/C4TwDgpgBAKu0F4oEkDMAmKAfKBlYATgNwBQAlgHbAQEBmAhgMbT4FQDeJUUA5gQPYBXMAC4oAIgr8KEcaW4BrSgBMx4gM6E5JAL7kqNBsxQYOXXgOFr1jegBt6BbYpVqyGbXpLKIjBwWhaQQpGYDJpKGB6BQg0dAAKADd7QQgxOIBKMUT+MmVSHz9HQODQ8IooSloaAGkVAB4YAD4klLSOKCUKVVgoHSzYUhIgkLCI6k0YAAsydUaoCAAPam71WHgW4Bn1MRgMs251AHcyYEYpqHit2YA6PiEwfc5ublt1aElpWRFzF6gA4CCAgUeQvN4fGz2RziH5-Q4nM4XK7bG5dZRPX5w8ESTROMQAenxUCkCwIAgIAH5MX9seJ3OgYdS+uY9F4RmVxhBNAAFRz0AC2EGoBHmSxWyjWcEgLX4uwx8NO50u-DulkeBzB9HeEikMkZcP+QqBIMxtMh-n1cOOiqRKrR8oNUDNWgJRJo5IAhEzXlqPvTLX89NxWSRhqUxhUJsAAIJ2Mi+5TTWai5YQVbraXI2ZyjWMaSaKD8KBIa7qUHWxHK1UPB0+7WfPWwuEA42gusQ2wWpt-CtK+J2lS1rG+nEuqCE0me71Okd0jzdl5B5ls8PlSJc4BJ9ToFPiyUbLM7WADOLYKDBHy0SgQZQasi0S6l6vCYsIJAaTvQod5igF5J2VJ0lMEttiIcc3TJfgCExKIYjiQ8MjAid3SgmDtigdR6DCdQry5Ex0CQiDyVNfNgCgRwBCOYtLhPYCmkiUDwMnVC-hbYEGNmQjmOg4NdFDdkI3XHk+UFYUdxgBZU3TKUIBlOUgMwHALwgK8ZFvZ5KgfftnzAV933NL8NVeUioH-QD8Oo-guP4BQYOiWIMH7RCmJszEi0w7DcLWOJrNsmkTIo-gqKQeJaMwBB6Ksly-JeNiKiiidXN4ldRjXKNY3jd5E22cTJL3DNZMPeSLKU7oVOvdTzB-AsixAzjzHvKt7hfN99M-JxvxMsz2lPJAEqJJKXlghyEn4ZzEpi7h3Kw2YvPw3ySN-MjAuCmiFOLSKFtYo12P6wsYpDIA

💻 Code
type Type = I32 | Str;
interface Str {
  group: "none";
  kind: "str";
}
interface I32 {
  group: "scalar";
  kind: "i32";
}

declare function takeI32(value: I32): void;
declare function inferKind<T>(value: { kind: T }): T;

function testThis<T extends Type>(this: T) {
  switch (this.group) {
    case "none":
      return;
    case "scalar":
      switch (this.kind) {
        case "str": // no error?
        case "i32":
      }
  }
}

function testParameter<T extends Type>(o: T) {
  switch (o.group) {
    case "none":
      return;
    case "scalar":
      switch (o.kind) {
        case "str": // error!
        case "i32":
      }
  }
}

function testAliasedThis<T extends Type>(this: T) {
  const o = this;
  switch (o.group) {
    case "none":
      return;
    case "scalar":
      switch (o.kind) {
        case "str": // error!
        case "i32":
      }
  }
}

function testThis2<T extends Type>(this: T): I32 | undefined {
  if (this.group === "scalar") {
    const value: I32 = this; // error
    takeI32(this); // error
    this satisfies I32; // error
    const arrow = (): I32 => this; // error
    return this; // error
  }
}

function testParameter2<T extends Type>(o: T): I32 | undefined {
  if (o.group === "scalar") {
    const value: I32 = o; // ok
    takeI32(o); // ok
    o satisfies I32; // ok
    const arrow = (): I32 => o; // ok
    return o; // ok
  }
}

function testAliasedThis2<T extends Type>(this: T): I32 | undefined {
  const o = this;
  if (o.group === "scalar") {
    const value: I32 = o; // ok
    takeI32(o); // ok
    o satisfies I32; // ok
    const arrow = (): I32 => o; // ok
    return o; // ok
  }
}

🙁 Actual behavior

The errors (or lack of them) are inconsistent between narrowing this directly vs narrowing an "aliased" this (const alias = this;)

🙂 Expected behavior

They should be consistent

Additional information about the issue

No response

主要语言
Go
星标
111k
派生
14.4k
平均合并
1 天 19 小时
30 天内合并 PR
117

贡献指南

打开贡献指南

从这里开始

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

microsoft/TypeScript 的其他 Issue

查看 microsoft/TypeScript 的全部 Issue

相似的 Issue

更多 Go Issue

把新 issue 发到你的邮箱

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