Casts in disguise: hand-written type predicates, assertion signatures and overloads are reported like as #113

Merged
sionzee merged 7 commits from casts-in-disguise into main 2026-11-21 23:23:44 +01:00
Owner

Stacked on #109 (the soundness corpus): this PR removes two lines from its list. Merge #109 first; I'll rebase this one onto main afterwards. It is independent of #110 and #111. It and #112 both add rules, so whichever of the two merges second needs a rebase over the rule lists.

no-unchecked-cast bans as. Three TypeScript constructs do the same job, and pass it along with tsc in strict mode:

function isUser(v: unknown): v is User { return true; }        // TypeScript never checks the body against the claim
function assertUser(v: unknown): asserts v is User {}           // an empty body compiles
function parse(t: string): User;                                // the implementation only has to be loosely related
function parse(t: unknown): unknown { return t; }

The only coverage was partial-type-guard, an opt-in warning that counted which field names a guard touched.

What changes

no-type-predicate (error). A hand-written type predicate or assertion signature is reported. Since TypeScript 5.5, a function with no return type whose body is a test TypeScript narrows by gets that predicate inferred, and checked. So where the body is such a test, the quick fix removes the annotation and the compiler takes over: filter(isString) still narrows, and a claim that said more than the body checked stops compiling. Everywhere else the message names the replacement: validate<T>, find on a constant list, std's guards.

One assertion form is allowed: asserts x with no type, whose body is exactly if (!x) throw or panic(...). It claims nothing about a type, and TypeScript has no inferred form for it.

no-overload (error). Overloads of the project's own functions and methods are reported, except the kind that only narrows what callers may pass: the same type parameters and result as the implementation, each parameter a member of the implementation's union. Those return what they say. .d.ts files and declare blocks describe other people's code and stay allowed.

brand-constructor now also reports a predicate that claims a proof type (s is SafeUrl).

partial-type-guard is removed; a config or ignore naming it is pointed at no-type-predicate.

What an adopter will notice

On an adopter's project: 99 no-type-predicate and 2 no-overload, all new errors, and nothing else changes. The implementer read all 99; this is its reading, which I did not repeat.

Verdict Count
A lie (for example, any function claimed to be a specific constructor) 2
Claim wider than the check about 14
Wrong on the false side 6
Sound about 77

24 of the 99 get the quick fix. Applied in a copy, 21 compile unchanged and 3 stop compiling, each because the claim was wider than its test.

A docs pattern changes with this: the codesOf helper was an overload over an unknown implementation, which is a cast. It is now one signature using trust<T>(...), which check counts as an escape hatch.

How it was checked

  • The Rust suite (330 tests), test:std, typecheck, the docs examples (825 compile), test:vite, the docs site build and the soundness corpus pass.
  • Ten declarations that must be reported are, and a second set the implementation had not seen (assertions that log before returning or test another value, overloads whose results differ). Legitimate narrowing stays clean.
  • The quick fix was run on six predicates of different shapes (typeof, a discriminant, instanceof, a generic !== undefined, a literal union, an inline filter callback) with their callers; tsc passes before and after and the callers still narrow.
  • The soundness corpus drops from 29 to 27 escaping classes.

To know

  • A function fixed this way has no declared return type. A later rule requiring declared return types will have to leave single-test functions alone.
  • no-overload compares types as written text, so a union behind an alias does not count as its members.
  • safe-ts fix was slow on the adopter (19 CPU-minutes over the whole project before the implementer stopped it), because it re-checks file by file. That predates this PR and is worth a look once #111 is in.

Created with Claude Opus 5.5 (implementation) and Claude Fable 5.2 (review, acceptance tests, and the optional-parameter case for overloads) in Claude Code.

Stacked on #109 (the soundness corpus): this PR removes two lines from its list. Merge #109 first; I'll rebase this one onto `main` afterwards. It is independent of #110 and #111. It and #112 both add rules, so whichever of the two merges second needs a rebase over the rule lists. `no-unchecked-cast` bans `as`. Three TypeScript constructs do the same job, and pass it along with `tsc` in strict mode: ```ts function isUser(v: unknown): v is User { return true; } // TypeScript never checks the body against the claim function assertUser(v: unknown): asserts v is User {} // an empty body compiles function parse(t: string): User; // the implementation only has to be loosely related function parse(t: unknown): unknown { return t; } ``` The only coverage was `partial-type-guard`, an opt-in warning that counted which field names a guard touched. ## What changes **`no-type-predicate` (error).** A hand-written type predicate or assertion signature is reported. Since TypeScript 5.5, a function with no return type whose body is a test TypeScript narrows by gets that predicate inferred, and checked. So where the body is such a test, the quick fix removes the annotation and the compiler takes over: `filter(isString)` still narrows, and a claim that said more than the body checked stops compiling. Everywhere else the message names the replacement: `validate<T>`, `find` on a constant list, std's guards. One assertion form is allowed: `asserts x` with no type, whose body is exactly `if (!x) throw` or `panic(...)`. It claims nothing about a type, and TypeScript has no inferred form for it. **`no-overload` (error).** Overloads of the project's own functions and methods are reported, except the kind that only narrows what callers may pass: the same type parameters and result as the implementation, each parameter a member of the implementation's union. Those return what they say. `.d.ts` files and `declare` blocks describe other people's code and stay allowed. **`brand-constructor`** now also reports a predicate that claims a proof type (`s is SafeUrl`). **`partial-type-guard` is removed**; a config or ignore naming it is pointed at `no-type-predicate`. ## What an adopter will notice On an adopter's project: 99 `no-type-predicate` and 2 `no-overload`, all new errors, and nothing else changes. The implementer read all 99; this is its reading, which I did not repeat. | Verdict | Count | |---|---| | A lie (for example, any function claimed to be a specific constructor) | 2 | | Claim wider than the check | about 14 | | Wrong on the false side | 6 | | Sound | about 77 | 24 of the 99 get the quick fix. Applied in a copy, 21 compile unchanged and 3 stop compiling, each because the claim was wider than its test. A docs pattern changes with this: the `codesOf` helper was an overload over an `unknown` implementation, which is a cast. It is now one signature using `trust<T>(...)`, which `check` counts as an escape hatch. ## How it was checked - The Rust suite (330 tests), `test:std`, `typecheck`, the docs examples (825 compile), `test:vite`, the docs site build and the soundness corpus pass. - Ten declarations that must be reported are, and a second set the implementation had not seen (assertions that log before returning or test another value, overloads whose results differ). Legitimate narrowing stays clean. - The quick fix was run on six predicates of different shapes (`typeof`, a discriminant, `instanceof`, a generic `!== undefined`, a literal union, an inline `filter` callback) with their callers; `tsc` passes before and after and the callers still narrow. - The soundness corpus drops from 29 to 27 escaping classes. ## To know - A function fixed this way has no declared return type. A later rule requiring declared return types will have to leave single-test functions alone. - `no-overload` compares types as written text, so a union behind an alias does not count as its members. - `safe-ts fix` was slow on the adopter (19 CPU-minutes over the whole project before the implementer stopped it), because it re-checks file by file. That predates this PR and is worth a look once #111 is in. Created with Claude Opus 5.5 (implementation) and Claude Fable 5.2 (review, acceptance tests, and the optional-parameter case for overloads) in Claude Code.
What was open: `no-unchecked-cast` bans `as`, but three constructs hand out a type nobody
checked and passed every gate, `tsc` strict included. TypeScript never checks a type predicate's
or an assertion signature's body against its claim (`(v: unknown): v is User => true`,
`asserts v is User {}` compile), and an overload signature only has to be loosely related to
the implementation (`function parse(t: string): User; function parse(t: unknown): unknown`).
A predicate could also mint a brand. The only coverage was the opt-in `partial-type-guard`,
which looked at field names in guards over `unknown`. The docs recommended the overload and the
"complete" guard as the cast-free way.

What closes it:

- `no-type-predicate` (error): every hand-written `x is T`, `this is T`, `asserts x is T` and
  `asserts x` on a function, method or arrow with a body. Nothing is verified and allowed:
  since TypeScript 5.5 a predicate whose body is one test TypeScript narrows by is inferred,
  and checked, once the annotation is gone. The quick fix "Let TypeScript infer the narrowing
  from the body" removes it for those bodies (typeof, instanceof, Array.isArray, `in`,
  literal and constant comparisons, discriminants, `!!x`, std's guards, joined by || or &&);
  a claim wider than its test then fails to compile where it was used. The message names the
  form for the rest (validate<T>, `find` for a lookup, std's isJsonObject, filter(isSome)).
  The one form allowed: `asserts x` whose body is exactly `if (!x) throw`/`panic(...)`.
- A predicate that claims a brand is `brand-constructor`.
- `no-overload` (error): overloads of the project's functions and methods, once per function.
  Allowed: overloads that only narrow the implementation (same type parameters and result,
  each parameter one of its union's members), `.d.ts`, `declare` blocks, interface call
  signatures. A result the body can't type is `trust<T>(value)`, which `check` counts.
- `partial-type-guard` is removed; a config or ignore naming it is told what replaced it.

On an adopter's project (346 files) the run adds 99 no-type-predicate and 2 no-overload
findings and changes nothing else; 24 of the predicates get the fix, and with it applied 21
compile unchanged and 3 surface a claim their body didn't make. The fix is held back where
TypeScript would infer something else: `Array.isArray` on a readonly array, `instanceof` for
a claim with type arguments (`Map<any, any>`), `Record<string, unknown>`. The repo's examples are
unchanged; the docs, the skill, the fixtures and std's test fixture write `codesOf` with
`trust` instead of an overload.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
no-overload: an overload may leave out the implementation's optional parameters; the corpus drops two classes
All checks were successful
Benchmarks / bench (pull_request) Successful in 3m45s
Tests / test (pull_request) Successful in 8m33s
d1624298a1
`area(shape: "circle", r)` / `area(shape: "rect", w, h)` over
`area(shape, a, b?)` returns what it says and accepts only what the body
handles, so it is the allowed kind; the rule compared parameter counts and
reported it. An overload that leaves out a required parameter is still reported.

`disguised-cast-asserts-predicate` and `disguised-cast-overload` come off the
soundness corpus's list: `no-type-predicate` and `no-overload` report them.
27 of 31 classes escape on this branch.

Co-Authored-By: Claude Fable 5.2 <[email protected]>
Fixture: write the interface's predicate member as a property
All checks were successful
Tests / test (pull_request) Successful in 7m4s
Benchmarks / bench (pull_request) Successful in 3m2s
30ceca8ae8
Another open change reports a member with parameters written as a method
(no-method-signature); the property form is valid either way.

Co-Authored-By: Claude Fable 5.2 <[email protected]>
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 3m39s
Tests / test (pull_request) Successful in 7m28s
dff6506c94
Co-Authored-By: Claude Fable 5.2 <[email protected]>
Merge main
Some checks failed
Benchmarks / bench (pull_request) Failing after 3s
Tests / test (pull_request) Successful in 29m15s
0c41853a03
Co-Authored-By: Claude Fable 5.2 <[email protected]>
sionzee deleted branch casts-in-disguise 2026-11-21 23:23:44 +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!113
No description provided.