Four TypeScript holes closed: method bivariance, constructors calling overrides, lists spread into variadic built-ins, optional properties #112

Merged
sionzee merged 8 commits from four-typescript-holes into main 2026-11-21 23:22:52 +01:00
Owner

Stacked on #109 (the soundness corpus): this PR removes four lines from its list. Merge #109 first; I'll rebase this one onto main afterwards. It is independent of #110 and #111.

Four programs that pass tsc in strict mode, safe-ts check --idiomatic and oxlint with the preset, with no cast and no any, and then fail at run time. Each gets a rule.

1. no-method-signature (error, with a quick fix)

interface Handler { handle(animal: Animal): string }
const h: Handler = { handle: (dog: Dog) => dog.bark() };   // accepted
h.handle({ kind: "cat" });                                 // TypeError: dog.bark is not a function

strictFunctionTypes does not apply to a member written as a method. The rule reports a method in an interface or type literal that has a parameter; the fix writes readonly handle: (animal: Animal) => string. Measured with tsc: once the member is a property, an arrow, a method-shorthand literal and a class instance are all refused. A method with no parameters has nothing to vary and is left alone.

safe-ts fix no-method-signature --write converted 48 sites in this repository. tsc then found one place that had only compiled because of the bivariance, in pack-kit, which is fixed.

What it does not close, stated on the rule page: classes used as types (an override with a narrower parameter is still accepted) and the platform's own types (Dog[] as Animal[]).

2. constructor-calls-override (error)

A constructor or field initializer that calls, directly or through the class's own methods, a method some subclass in the program overrides, or an abstract one. The override runs before the subclass's fields are set. class extends itself stays allowed. It needs the whole program, so safe-ts check reports it and the editor does not.

3. unbounded-spread (warning)

Math.max(...values), target.push(...extra), String.fromCharCode(...codes): a RangeError past roughly 100,000 elements, and Math.max of nothing is -Infinity. Reported when the list's length is unknown; a literal, a tuple, a rest parameter and slice(0, 16) are fine, and so is a project type's own method that happens to be called push. std gains maxOf and minOf, which return Option<number>.

It is a warning, like bounded-concurrency and no-accumulating-copy: the limit takes a very long list to reach, and many sites are bounded by something the checker can't see.

4. no-optional-field (idiomatic, off by default)

const tagged: { id: string; tag?: string } = base;   // accepted when base's real value has tag: 42

An optional property lets a value through whose real property has another type. With the idiomatic rules on, key?: T in a type the project declares is reported; key: Option<T> is what a value without the key is not assignable to. Exempt: optional parameters, types handed to the JSON validators, Vue and React props, types built on a library's, and declarations.

What an adopter will notice

On an adopter's project (checked as directories, tests included):

Rule New findings
no-method-signature 184, all fixable with safe-ts fix
constructor-calls-override 0
unbounded-spread 298 warnings, about two thirds in tests
no-optional-field 708 (the project has the idiomatic rules on); 548 more are exempt

In a sample of 13 unbounded-spread findings outside tests, nine were Math.max or Math.min with the empty-list trap, and one was a live bug: a guard that returns Infinity when its list is empty. About five were bounded in practice.

How it was checked

  • The Rust suite (326 tests), test:std (381), typecheck, the docs examples, test:vite, the docs site build and the soundness corpus pass.
  • Lines that must be reported are, in the cases given to the implementation and in a second set it had not seen (generic and overloaded methods, a call reaching an override through a private helper or from another file, an abstract call, spreads through .map).
  • The quick fix was run on a file with generic, optional, overloaded and this-typed methods and their implementers; tsc passes before and after.
  • Two false positives from the first version are fixed: a project class's own push, and a const holding slice(0, 3).
  • The soundness corpus drops from 29 to 25 escaping classes.

Not done

  • No rule yet that an overriding method's parameters match its parent's, which would close the class half of the first hole.
  • no-optional-field has no quick fix: it needs the whole program, and safe-ts fix works a file at a time.
  • A spread of a bounded but possibly empty list into Math.max is not reported; the empty case would need its own finding.

Created with Claude Opus 5.5 (implementation) and Claude Fable 5.2 (review, acceptance tests, and the warning level) in Claude Code.

Stacked on #109 (the soundness corpus): this PR removes four lines from its list. Merge #109 first; I'll rebase this one onto `main` afterwards. It is independent of #110 and #111. Four programs that pass `tsc` in strict mode, `safe-ts check --idiomatic` and oxlint with the preset, with no cast and no `any`, and then fail at run time. Each gets a rule. ## 1. `no-method-signature` (error, with a quick fix) ```ts interface Handler { handle(animal: Animal): string } const h: Handler = { handle: (dog: Dog) => dog.bark() }; // accepted h.handle({ kind: "cat" }); // TypeError: dog.bark is not a function ``` `strictFunctionTypes` does not apply to a member written as a method. The rule reports a method in an interface or type literal that has a parameter; the fix writes `readonly handle: (animal: Animal) => string`. Measured with `tsc`: once the member is a property, an arrow, a method-shorthand literal and a class instance are all refused. A method with no parameters has nothing to vary and is left alone. `safe-ts fix no-method-signature --write` converted 48 sites in this repository. `tsc` then found one place that had only compiled because of the bivariance, in pack-kit, which is fixed. What it does not close, stated on the rule page: classes used as types (an override with a narrower parameter is still accepted) and the platform's own types (`Dog[]` as `Animal[]`). ## 2. `constructor-calls-override` (error) A constructor or field initializer that calls, directly or through the class's own methods, a method some subclass in the program overrides, or an abstract one. The override runs before the subclass's fields are set. `class extends` itself stays allowed. It needs the whole program, so `safe-ts check` reports it and the editor does not. ## 3. `unbounded-spread` (warning) `Math.max(...values)`, `target.push(...extra)`, `String.fromCharCode(...codes)`: a `RangeError` past roughly 100,000 elements, and `Math.max` of nothing is `-Infinity`. Reported when the list's length is unknown; a literal, a tuple, a rest parameter and `slice(0, 16)` are fine, and so is a project type's own method that happens to be called `push`. std gains `maxOf` and `minOf`, which return `Option<number>`. It is a warning, like `bounded-concurrency` and `no-accumulating-copy`: the limit takes a very long list to reach, and many sites are bounded by something the checker can't see. ## 4. `no-optional-field` (idiomatic, off by default) ```ts const tagged: { id: string; tag?: string } = base; // accepted when base's real value has tag: 42 ``` An optional property lets a value through whose real property has another type. With the idiomatic rules on, `key?: T` in a type the project declares is reported; `key: Option<T>` is what a value without the key is not assignable to. Exempt: optional parameters, types handed to the JSON validators, Vue and React props, types built on a library's, and declarations. ## What an adopter will notice On an adopter's project (checked as directories, tests included): | Rule | New findings | |---|---| | `no-method-signature` | 184, all fixable with `safe-ts fix` | | `constructor-calls-override` | 0 | | `unbounded-spread` | 298 warnings, about two thirds in tests | | `no-optional-field` | 708 (the project has the idiomatic rules on); 548 more are exempt | In a sample of 13 `unbounded-spread` findings outside tests, nine were `Math.max` or `Math.min` with the empty-list trap, and one was a live bug: a guard that returns `Infinity` when its list is empty. About five were bounded in practice. ## How it was checked - The Rust suite (326 tests), `test:std` (381), `typecheck`, the docs examples, `test:vite`, the docs site build and the soundness corpus pass. - Lines that must be reported are, in the cases given to the implementation and in a second set it had not seen (generic and overloaded methods, a call reaching an override through a private helper or from another file, an abstract call, spreads through `.map`). - The quick fix was run on a file with generic, optional, overloaded and `this`-typed methods and their implementers; `tsc` passes before and after. - Two false positives from the first version are fixed: a project class's own `push`, and a `const` holding `slice(0, 3)`. - The soundness corpus drops from 29 to 25 escaping classes. ## Not done - No rule yet that an overriding method's parameters match its parent's, which would close the class half of the first hole. - `no-optional-field` has no quick fix: it needs the whole program, and `safe-ts fix` works a file at a time. - A spread of a bounded but possibly empty list into `Math.max` is not reported; the empty case would need its own finding. Created with Claude Opus 5.5 (implementation) and Claude Fable 5.2 (review, acceptance tests, and the warning level) in Claude Code.
Each of these programs passed tsc strict, safe-ts check --idiomatic and the
oxlint preset, with no cast and no any, and failed at run time.

- no-method-signature (error, quick fix): a member written as a method in an
  interface or type literal is checked bivariantly, so an implementation that
  only takes a Dog is accepted where any Animal is passed. The fix writes
  `readonly m: (x: T) => R` (overloads become one property with call
  signatures). Against a property member, tsc refuses a narrower arrow
  function, object-literal method and class method alike; a class used as a
  type and the platform's method-declared types stay bivariant, as the rule
  page says. Applied to this repository: 48 sites in 10 files; tsc then found
  one assignment in pack-kit that only compiled through the bivariance, now
  typed with the real `Signature`.
- constructor-calls-override (error, safe-ts check only): a constructor or
  instance field initializer that calls, directly or through the class's own
  and inherited methods, a method or accessor that a subclass in the program
  overrides, or an abstract one: the override runs before the subclass's
  fields are set. Subclasses are found across files with the existing binding
  resolution; the program is taken as complete.
- unbounded-spread (error): a list of unknown length spread into Math.max/min/
  hypot, push/unshift/splice/concat, String.fromCharCode/fromCodePoint,
  Array.of and typed arrays' of, Object.assign, append/prepend/replaceChildren
  (also .call/.apply/Reflect.apply): RangeError past ~100,000 elements, and
  -Infinity for an empty Math.max. Literals, tuples, `as const`, rest
  parameters and project functions are fine. std/array gains maxOf and minOf
  (Option<number>), which no built-in form gives.
- no-optional-field (idiomatic, safe-ts check only): `key?: T` in a type the
  project declares lets a value without the key, holding anything, through;
  write `key: Option<T>`. Exempt: optional parameters, JSON types (validator
  type arguments anywhere in the program and what they name, std JSON types),
  Vue defineProps and React component props, types built on a library's or
  the platform's type, declarations and ambient blocks, decorated fields.

Fixture lines in existing fixtures that now get one of these findings are
annotated with the new rule; no existing expectation was removed. Docs
examples with method signatures are written in the property form.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
no-method-signature: a member written as a method is bivariant only in its
parameters, so a method without any (`destroy(): void`, `channels(): T[]`;
a `this` parameter doesn't count) has nothing for the property form to close.
It is no longer reported (64 of 248 findings on an adopter were of that
kind). Overloads stay one member: reported, and fixed, when any signature has
a parameter. The 8 `expect: no-method-signature` annotations the previous
commit added to existing fixtures on parameterless methods are removed again
(contexts, idiomatic_rules/task, json, node_classes, ownership/fields,
pack_globals/shadow, results/open, test_runners/globals); the rule's own
fixture gains the silent cases (parameterless, optional, `this`-only) and a
mixed overload.

unbounded-spread: `push`, `unshift`, `splice`, `concat`, `append`, `prepend`
and `replaceChildren` were matched on any receiver, so a project type's own
variadic method (`router.push(...names)` with `router: Router`) was reported
with advice meant for arrays. Now a receiver that is a parameter, a variable
or a field `this.x` declared with (or a variable made with `new C(...)` of) a
class, interface or type alias of the project that declares that member
itself, or `this` inside such a class, is not reported. `Readonly<T>`,
`Mut<T>`, `T | undefined` and intersections are looked through. A type of the
same file is decided per file; an imported one is decided by the
whole-program pass (new `Run::project_type_declares`, resolved like
`type_target`: `resolve_binding` / `export_at`, not a package's, not a
`.d.ts`), from each file's `declared_members`. Arrays, tuples, `Array<T>`,
strings, DOM types, type parameters and receivers of unwritten type stay
reported.

Also a `const` whose value has a known length (`const head =
values.slice(0, 3)`) now counts as known, unless something can make it longer:
`push` / `unshift` / `splice` on it, a write to `length` or an index, or the
array handed to anything but a spread, a `for of` or a member read. A
bounded list that may be empty (`Math.max(...xs.slice(0, 16))`) stays
unreported: the rule is about lists too long for the stack, and its message
leads with the RangeError; the rule page now says so under what the rule
can't see.

Rule pages, README and docs index rows, the Unreleased CHANGELOG entries,
module docs, the no-optional-field cross-reference and the migrate skill's
tool-gaps note are updated to match.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
It reports a limit that takes about 100,000 elements to reach, and on an
adopter's project a good share of its sites are bounded in practice by
something the checker can't see. `bounded-concurrency` and
`no-accumulating-copy`, its nearest neighbours, are warnings for the same
reason. A run with `--deny-warnings` still fails on it.

Co-Authored-By: Claude Fable 5.2 <[email protected]>
Soundness corpus: four classes no longer escape
Some checks failed
Benchmarks / bench (pull_request) Has been cancelled
Tests / test (pull_request) Successful in 8m32s
5c95a1359c
`interface-method-bivariance`, `class-base-ctor-uninit-subclass-field`,
`spread-call-stack-overflow` and `width-subtyping-optional-prop` come off the
list: `no-method-signature`, `constructor-calls-override`, `unbounded-spread`
and `no-optional-field` report them. 25 of 31 classes escape.

Co-Authored-By: Claude Fable 5.2 <[email protected]>
sionzee force-pushed four-typescript-holes from 5c95a1359c
Some checks failed
Benchmarks / bench (pull_request) Has been cancelled
Tests / test (pull_request) Successful in 8m32s
to b8ca7a1819
All checks were successful
Benchmarks / bench (pull_request) Successful in 4m6s
Tests / test (pull_request) Successful in 7m44s
2026-11-21 05:12:19 +01:00
Compare
sionzee changed target branch from soundness-corpus to main 2026-11-21 11:11:50 +01:00
Merge main: the soundness corpus and the whole-program memo have landed
All checks were successful
Benchmarks / bench (pull_request) Successful in 3m40s
Tests / test (pull_request) Successful in 8m44s
c95045bc64
Co-Authored-By: Claude Fable 5.2 <[email protected]>
Merge main
Some checks failed
Tests / test (pull_request) Failing after 4s
Benchmarks / bench (pull_request) Successful in 2m46s
f3bcc0690b
Co-Authored-By: Claude Fable 5.2 <[email protected]>
sionzee deleted branch four-typescript-holes 2026-11-21 23:22:52 +01:00
Sign in to join this conversation.
No reviewers
No labels
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set

Reference
Evobug/Safe-Ts!112
No description provided.