A generic class instantiated with two types reads the second instance as the first, natively (regressed in 0.4.16)

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

维护者通常 1 天内回复

还没有人认领这个 Issue。

评估

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

调研方向

Start by reproducing the failure from the issue’s gm6.js example with allowJs, checkJs, and strict enabled; compare native output with the Node.js and scriptc 0.4.12 results. The issue points to packages/light/native/probes/p-g4-generic-composition.js as another reproducer. Trace how the compiler handles the two Lazy specializations and confirm the fix by getting both examples to print 42 ab without diagnostics.

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

描述

A generic class instantiated with a number and then with a string miscompiles the second instance natively, with no diagnostic. It builds 100% static and throws at run time. It regressed between 0.4.12 and 0.4.16 and is still in 0.5.0.

Reproduction

gm6.js, built with a tsconfig that has allowJs, checkJs and strict (so the JSDoc types reach the compiler):

/** @template T */
class Lazy {
  /** @param {() => T} fn */
  constructor(fn) {
    this._fn = fn;
    /** @type {T} */
    this._value = fn();
  }
  /** @param {T} value */
  _assign(value) {
    this._value = value;
  }
  /** @returns {T} */
  get value() {
    this._assign(this._fn());
    return this._value;
  }
}
const n = new Lazy(() => 42);
const s = new Lazy(() => "ab");
console.log(n.value, s.value);
Build Output Exit
Node.js v25.3.0 42 ab 0
scriptc 0.4.12 no diagnostics 42 ab 0
scriptc 0.4.16 no diagnostics Uncaught TypeError: expected number at $, got string 1
scriptc 0.5.0 no diagnostics Uncaught TypeError: expected number at $, got string 1

Without checkJs all three releases print 42 ab. The two instantiations of Lazy seem to share one specialization, so the string instance is read as a number.

Environment

Linux x86_64, glibc 2.39, scriptc's linux-x64-gnu packages from npm.

Found through light's probe packages/light/native/probes/p-g4-generic-composition.js, which fails the same way.

主要语言
TypeScript
星标
6.7k
派生
229
平均合并
14 小时 29 分钟
30 天内合并 PR
787

环境准备

  • 没有 Dockerfile 或 Docker Compose 文件
  • 没有 Pull Request 模板
  • 阅读贡献指南

从这里开始

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

vercel-labs/scriptc 的其他 Issue

查看 vercel-labs/scriptc 的全部 Issue

相似的 Issue

更多 TypeScript Issue

把新 issue 发到你的邮箱

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