Microsoft/TypeScript

Incorrect type inference for generic class in JavaScript

Open

#61,552 创建于 2025年4月5日

在 GitHub 查看
 (0 评论) (0 反应) (0 负责人)TypeScript (6,726 fork)batch import
BugDomain: check: Type InferenceHelp Wanted

仓库指标

Star
 (48,455 star)
PR 合并指标
 (平均合并 6天 17小时) (30 天内合并 9 个 PR)

描述

Does this issue occur when all extensions are disabled?: Yes

  • VS Code Version: 1.99.0
  • OS Version: Windows 10 Home 22H2

Steps to Reproduce:

  1. Define a generic class as below:
    /** @template T */
    class C {
      /** @param {T} a */
      constructor(a) { }
      f() { const v = new C(/** @type {T[]} */ ([])); }
    }
    
  2. The type of the variable v is incorrectly inferred to be C<T[][]>.

If the constructor call is outside of the class C<T> (as in the example below), the return type of the call is correctly inferred to be C<T[]>.

/** @template T */
class D {
  f() { const v = new C(/** @type {T[]} */ ([])); }
}
/** @template T; @param {C<T>} c */
const f = c => { const v = new C(/** @type {T[]} */ ([])); }

贡献者指南