Skip to content

fix: add TypeScript overload signatures for accepts methods#494

Open
claygeo wants to merge 1 commit intotinyhttp:masterfrom
claygeo:fix/accepts-type-overloads
Open

fix: add TypeScript overload signatures for accepts methods#494
claygeo wants to merge 1 commit intotinyhttp:masterfrom
claygeo:fix/accepts-type-overloads

Conversation

@claygeo
Copy link
Copy Markdown

@claygeo claygeo commented Mar 27, 2026

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

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
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

1 participant