Skip to content

🎨 Narrow AbortAddon controller() return type to AbortController#316

Merged
elbywan merged 1 commit into
elbywan:masterfrom
Ruby-Leung:narrow-abort-controller-type
Jun 5, 2026
Merged

🎨 Narrow AbortAddon controller() return type to AbortController#316
elbywan merged 1 commit into
elbywan:masterfrom
Ruby-Leung:narrow-abort-controller-type

Conversation

@Ruby-Leung

Copy link
Copy Markdown
Contributor

What

.controller() is typed as () => [any, this], so the first element of the returned pair is any. At runtime it is always the request's AbortController (created in beforeRequest), so this narrows the type to [AbortController, this].

Why

With any, callers lose all type safety on the returned controller — the documented c.abort() usage isn't checked, and typos slip through silently:

const [c, w] = wretch(url).addon(AbortAddon()).get().controller()
c.abort()   // now type-checked
c.signl     // now a compile error instead of silently `any`

This is a type-only change — runtime behaviour is unchanged. (Same spirit as #314.)

Tests

Adds test/types/abort-controller.ts, which asserts the narrowed return type and includes a @ts-expect-error that fails if the type ever regresses back to any. npm run test:types and npm run test:node both pass locally.

The controller() resolver always returns the request's AbortController at runtime (created in beforeRequest), so type the first element of the returned pair as AbortController instead of any. Consumers keep full type safety on the returned controller (.abort(), .signal, …) instead of an unchecked any. Adds a type-level test under test/types/. Type-only change, no runtime impact.
@coveralls

Copy link
Copy Markdown

Coverage Status

coverage: 97.839%. remained the same — Ruby-Leung:narrow-abort-controller-type into elbywan:master

@elbywan elbywan left a comment

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looking good, thanks! 🙇

@elbywan elbywan merged commit a7c2185 into elbywan:master Jun 5, 2026
12 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants