Casts in disguise: hand-written type predicates, assertion signatures and overloads are reported like as #113
Loading…
Reference in a new issue
No description provided.
Delete branch "casts-in-disguise"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
Stacked on #109 (the soundness corpus): this PR removes two lines from its list. Merge #109 first; I'll rebase this one onto
mainafterwards. 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-castbansas. Three TypeScript constructs do the same job, and pass it along withtscin strict mode: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>,findon a constant list, std's guards.One assertion form is allowed:
asserts xwith no type, whose body is exactlyif (!x) throworpanic(...). 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.tsfiles anddeclareblocks describe other people's code and stay allowed.brand-constructornow also reports a predicate that claims a proof type (s is SafeUrl).partial-type-guardis removed; a config or ignore naming it is pointed atno-type-predicate.What an adopter will notice
On an adopter's project: 99
no-type-predicateand 2no-overload, all new errors, and nothing else changes. The implementer read all 99; this is its reading, which I did not repeat.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
codesOfhelper was an overload over anunknownimplementation, which is a cast. It is now one signature usingtrust<T>(...), whichcheckcounts as an escape hatch.How it was checked
test:std,typecheck, the docs examples (825 compile),test:vite, the docs site build and the soundness corpus pass.typeof, a discriminant,instanceof, a generic!== undefined, a literal union, an inlinefiltercallback) with their callers;tscpasses before and after and the callers still narrow.To know
no-overloadcompares types as written text, so a union behind an alias does not count as its members.safe-ts fixwas 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]>