fix: add TypeScript overload signatures for accepts methods by claygeo · Pull Request #494 · tinyhttp/tinyhttp · GitHub
Skip to content

fix: add TypeScript overload signatures for accepts methods#494

Open
claygeo wants to merge 3 commits into
tinyhttp:masterfrom
claygeo:fix/accepts-type-overloads
Open

fix: add TypeScript overload signatures for accepts methods#494
claygeo wants to merge 3 commits into
tinyhttp:masterfrom
claygeo:fix/accepts-type-overloads

Conversation

@claygeo

@claygeo claygeo commented Mar 27, 2026

Copy link
Copy Markdown

Problem

The Request interface defines accepts, acceptsEncodings, acceptsCharsets, and acceptsLanguages with a single signature:

accepts: (...types: string[]) => string | boolean | string[]

This means TypeScript can't narrow the return type based on usage. Users must cast or type-guard every call:

// Current: always returns string | boolean | string[]
const type = req.accepts('json', 'html'); // string | boolean | string[]
// User has to cast: (type as string)

const all = req.accepts(); // string | boolean | string[] (should be string[])

The underlying Accepts class already handles all call patterns correctly and returns different types based on input.

Solution

Add TypeScript overload signatures that narrow the return type:

accepts: {
  (): string[]                           // no args = all accepted types
  (type: string): string | false         // single string = best match or false
  (types: string[]): string | false      // array = best match or false
  (...types: string[]): string | false   // variadic = best match or false
}

Also narrows AcceptsReturns from boolean to the literal false type, since these methods return false (not true) when no match is found. This matches Express's type definitions.

No runtime changes. The implementation already supports all call patterns. This is purely a type-narrowing improvement.

Changes

  • packages/app/src/request.ts — Added overload signatures for all four accepts methods, narrowed AcceptsReturns to use false instead of boolean

Closes #396

claygeo and others added 3 commits June 19, 2026 09:45
The Request interface defines accepts, acceptsEncodings,
acceptsCharsets, and acceptsLanguages with a single signature
that returns the broad union string | boolean | string[].

The underlying implementation already supports multiple call
patterns with different return types:
- No args: returns all accepted values as string[]
- With args: returns best match as string, or false

This adds overload signatures so TypeScript narrows the return
type based on how the method is called, matching Express behavior.

Also narrows AcceptsReturns from boolean to the literal false type
since these methods never return true.

Closes tinyhttp#396
…asts

After the four accepts methods switched to inline overload signatures, the
AcceptsReturns alias is no longer referenced anywhere; remove the dead code.
Also wrap the acceptsCharsets/Encodings/Languages cast lines to satisfy
biome's 120-char lineWidth.
@claygeo claygeo force-pushed the fix/accepts-type-overloads branch from fcee88b to 30f3021 Compare June 19, 2026 19:43
@claygeo

claygeo commented Jun 19, 2026

Copy link
Copy Markdown
Author

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Does it have to be invoked on every call, or it could be just statically defined and then reused for all Accepts methods?

@v1rtl v1rtl left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Overall good, one tiny question

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.

Add missing accepts TypeScript signatures

2 participants