Types the checker could not see: undeclared returns, aliases, receivers behind utility types, optional calls, calls through a slot #123
Loading…
Reference in a new issue
No description provided.
Delete branch "return-types"
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?
Based on
promise-statics(#121), and contains #119, #120 and #121: the diff shown is this piece alone. The open pieces above #118 form one chain, each branch containing the ones below it: #119, #120, #121, #123, then #116 and #122. Merge them in that order.Combined with the pieces below. Three pieces had each taught the checker that
x?.m()is a call, and two had their own way of reading a receiver throughReadonly<...>orOption<...>. Here they become one helper and one opener. With #120,const r = repo?.check(id); console.log(r)is a keptResult.SAFE-TS has no type checker. A rule that depends on what a value is goes by the type that is written. Where none is written, or the written one is a name the checker did not open, the rule said nothing. All of these were silent, with
tscclean:One answer for every rule
A function's result is known without being declared, when every
returnis plain: a literal, an operator with one kind of result, a declared binding, a member of a plain value, a call whose result is known (by name, method, getter, static, object-literal member, namespace / default / re-exported import),newof a class, an object or array of plain values, and Vue'sref/computed/reactiveand React'suseState/useRef/useMemo. Anything else stops the reading: the result is unknown, never guessed. Generic, recursive, overloaded and reassigned functions stay unknown.A written type is opened the same way wherever it is read: aliases of aliases, imported aliases, generic aliases with their arguments,
Readonly/Partial/Required/Pick/Omit/NonNullable/Awaited, the value of aMaporRecord, an array's or tuple's item, a promise's value, a union withundefinedornull. It stops at conditional and mapped types and says unknown.Literals keep their type where TypeScript keeps it.
const icon = ok ? "icon-a" : undefined; if (icon)is no longer reported: the empty string is impossible there. That was a false positive before this change.Receivers and slots go through that answer. The failure rules follow a method through any of those written forms of its receiver's type, and
failure-contractchecks what is put into a slot behind them.An optional call is a call.
repo?.check(id)is followed likerepo.check(id).A call propagates what its callee's written function type declares, however the callee was reached: a
Recordmember or key, aMap's value, an array or tuple item, a local taken out by key. The function type is the contract; a plain function type reached the same way propagates nothing.New idiomatic rule
undeclared-return-type(warning, on with"idiomatic": true): a function whose result the checker cannot read and which does not declare it. Exempt: a body that is a single test (TypeScript infers a predicate only while no return type is written), a function typed by where it is put, a function that returns nothing, and a component. Quick fix where the type can be written.Tests that hold it
tscchecks that the declared type really is the inferred one. Before this change, 36 of my own 37 held-out pairs differed; now one does, the documentedswitchwithoutdefault.On an adopter's project
Nothing is lost. New: 161
undeclared-return-type(warnings), 125strict-truthiness, and one each ofexhaustive-dispatch,negative-member-guardandcompare-misses-undefined.strict-truthinessfindings were read one by one: every type is right. They are tests likeif (!pending.size),if (dx || dy)andif (bits & mask), where0means "none" on purpose.if (bits & mask)stays reported, as it already was where the operand is declared; the rewrite to!== 0is mechanical.Pick<...>and writes noThrowing, so there the routing is proven by the tables, not by findings.Whole-program pass on its largest entry: 905 ms before, 957 ms after. A name filter keeps the new receiver forms from being resolved where no method of that name matters (100,456 full resolutions at the first attempt, 2,454 now). About 580 ms of that pass is the existing
sentinel-errorcheck, which is the target for separate speed work.What turns red on upgrade
"idiomatic": true:undeclared-return-typewarnings, andstrict-truthinesson results that were unknown before.on...member of a project type behindMut<...>is now afailure-contractfinding, as it already was for the plain type; it wasuncaught-propagation.JSON.stringify(f(n))wherefplainly returns a list of numbers is no longer reported.What stays unseen
typeof, indexed access types.switchwithoutdefaultwhose cases all return, and a body TypeScript knows never ends.Created with Claude Opus 5.5 (implementation) and Claude Fable 5.2 (review and acceptance tests) in Claude Code.
.oxlintrc.json, the file that sets themOpen (review): `const r = flag ? save(1) : save(2)` and `const [a, b] = await Promise.all([...])` weren't seen as kept Results; the kept-Result analysis built a graph for one function in five on the adopter's largest entry (2,561 of ~10,500) and use-after-consume for 539, mostly for calls that can't matter; and the layer was held only by ten unit tests and the rules' fixtures. Closes: - Kept: each call of a `?:` / `??` / `||` / `&&` / parenthesised / awaited initialiser, and each element of an array destructuring of `Promise.all` / `allSettled` over a list written in place, in the binding at its place (fixtures). `const { result } = pair()` isn't (only `pair`'s return type says what `result` is). - The cheap test first. Kept Results: the local pass only lists the kept calls (no graph), after two syntactic tests (a const looked at unconditionally by the very next statement; a call whose callee the file shows returns a plain type, or a method of a value declared with one); the whole-program pass keeps those whose function returns a Result and only then parses the file again and builds graphs for them (`unsafe(...)` / `parseJson` stay local). use-after-consume: a function gets a graph only if an operation there would use a binding up (`Array.from(list)` of an array doesn't); the generator walk runs only in a file with a `function*`. On the adopter's `apps/client/src/start.ts`: 3,119 graphs before (2,561 kept, 539 consumed, 19 number-parse-nan), 40 after (21, 0, 19). - flow::tests::path_cases: 39 shapes with their answers on some / every path, independent of any rule (labelled break and continue, do...while, a loop's update clause, for await, continue in try/finally, finally after a return, nested finally, switch with default in the middle and falling into it, && / || in a statement and a test, ?:, ??, ||=, optional calls, yield and await between, reset, an exception into catch, nested functions, unreachable code). - Docs: the libraries guide says a pack's `consumes` is read from its safe-ts.json (and the manifest example shows it); use-after-consume's page covers views, second names and optional calls; must-use-result's covers branches and Promise.all; README gaps, CHANGELOG, tool gaps. Co-Authored-By: Claude Opus 5.5 <[email protected]>asynconeConflicts, all kept from both sides: ecmascript-api.json gets both new sections (`callbacks` and `consumes`, merged as JSON: each side only added its own key); package.json takes `test:std` from this side (vue-tsc tools for the TypeScript-versions test) and `test:browsers` from the other (the browsers' consumes test); the soundness class list imports both new groups; the migration skill has both sections ("A promise used as its value", "A value used after it was used up") and census.mjs both pointers; the CHANGELOG's Added and Changed keep every entry. Co-Authored-By: Claude Opus 5.5 <[email protected]>Resultgets one finding, not twodeclareis a cast #119Resultkept in a local #120promise-as-value,floating-promise#121this, parts handed out, kept copies, wider aliases, loans alive at once #122