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

Open
#4,864 0 comments 0 reactions 0 assignees View on GitHub

Maintainers usually reply within 1 day

Nobody has claimed this yet.

Assessment

Difficulty
4/5
Estimated time
3-5 days
Newbie friendliness
58/100
Issue type
Bug
Clarity
Clearly specified
Activity status
Active
Tech stack
node.js, typescript
Domain
compilers

Research direction

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.

Written by the indexing model from the issue text.

Description

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.

Dominant language
TypeScript
Stars
6.7k
Forks
229
Avg merge
14h 29m
Merged PRs (30d)
787

Getting set up

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from vercel-labs/scriptc

All issues in vercel-labs/scriptc

Similar issues

More TypeScript issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.