refactor(sse): narrow three media-generation result unions - #8645
Merged
diegosouzapw merged 1 commit intoJul 27, 2026
Merged
diegosouzapw merged 1 commit into
diegosouzapw merged 1 commit into
Conversation
Seven diagnostics across five files, one cause — the `strictNullChecks: false` limitation again: `ok: true | false` narrows the positive branch but leaves the negative one as the whole union, so `if (!result.ok)` cannot reach `status`/`error`. Three unions, and the treatment differs per the rule recorded on diegosouzapw#8499: SegmindRequestResult exported -> type predicate (4: image + video) downloadGeneratedImage private -> retag (2: freepik) ValidatedMediaGenerationBody private -> retag (1: videos route) `SegmindRequestResult` gets `isSegmindFailure()` rather than a retag because it is exported and its `ok` shape is the published contract of `segmindRequest()`; the other two are module-local with no external consumer, so retagging is the cheaper fix. Both consumers of the retagged body union (videos + music routes) updated with it. 208 -> 201, zero new, on a line-number-agnostic diff of the full tsc error set. Deliberately excluded: `MediaGenerationResult` and its `failedMediaGenerationResponse` parameter, which look like the same shape. Retagging it fixes 2 more but breaks the callers, because the provider handlers that produce these results widen `success` to `boolean` rather than a literal — a different root cause (literal widening, not discriminant narrowing) that needs the producers fixed first. I tried it, measured 10 fixed / 1 new, and reverted that part to keep the zero-new-errors invariant. Left for its own slice. No new tests: the failure arms are already exercised — segmind-image-video-provider-6656.test.ts drives 500 and 502 upstream responses through both segmind providers, and video-generation-handler.test.ts covers 503 and a malformed-output 500. 234/234 across the 27 segmind / freepik / video / music / image / media suites; typecheck:core, eslint and check:file-size clean.
backryun
force-pushed
the
chore/ts7-types-media-result-unions
branch
from
July 26, 2026 20:53
73c37ff to
83554fd
Compare
Owner
|
Validated in local merge-train /tmp/train1d-20260727-090022-suite.log on .113 @ 029cdf4215cf465f0e1716ac9f84a84692b1e881 (full unit suite green on CI-equivalent host) |
Merged
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Part of #8484. Seven diagnostics across five files, one cause — and the three unions get three different treatments, which is the interesting part.
The cause, again
strictNullChecks: falsemeansok: true | falsenarrows the positive branch but leaves the negative one as the whole union. Soif (!result.ok)cannot reachstatus/error. Fifth time this campaign (#8483, #8499, #8531, #8638).Three unions, three treatments
SegmindRequestResultdownloadGeneratedImage's returnValidatedMediaGenerationBodyPer the rule recorded on #8499 — predicate when exported, retag when private.
SegmindRequestResultis the published return contract ofsegmindRequest(), so itsokshape stays put and gets:The other two have no consumer outside their module, so retagging is cheaper and clearer. Both consumers of the retagged body union — the videos and music routes — were updated with it.
What I tried, measured, and reverted
MediaGenerationResultin the same shared file looks like the same shape:Retagging it and narrowing
failedMediaGenerationResponse's parameter to the failed arm fixes 2 more — but introduces one new error: the provider handlers that produce these results widensuccesstobooleanrather than a literal, so the route'sresultno longer matches the narrowed parameter.That is a different root cause — literal widening at the producers, not discriminant narrowing at the consumer — and fixing it properly means going after the producers first. Measured 10 fixed / 1 new, reverted that part, kept the invariant. Left for its own slice.
Verification
208 → 201, zero new errors, on a line-number-agnostic diff of the full
tscerror set.npm run typecheck:core— cleaneslinton all seven changed files — cleancheck:file-size— cleanNo new tests — the failure arms are exercised
Checking the arms the change touches is standard here, and both retagged/predicated failure paths already run under test:
segmind-image-video-provider-6656.test.tsdrives a 500 (text/plainbody) and a 502 through both segmind providers — the exactok: falsepathisSegmindFailurenow guardsvideo-generation-handler.test.tscovers a 503 and a malformed-output 500The freepik download failure and the invalid-JSON body arm are narrower; they are reachable and unchanged in behaviour (retagging a discriminant has no runtime effect beyond the literal string compared), so I did not manufacture coverage for them.