fix: add TypeScript overload signatures for accepts methods#494
Open
claygeo wants to merge 1 commit intotinyhttp:masterfrom
Open
fix: add TypeScript overload signatures for accepts methods#494claygeo wants to merge 1 commit intotinyhttp:masterfrom
claygeo wants to merge 1 commit intotinyhttp:masterfrom
Conversation
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
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.
Problem
The
Requestinterface definesaccepts,acceptsEncodings,acceptsCharsets, andacceptsLanguageswith a single signature:This means TypeScript can't narrow the return type based on usage. Users must cast or type-guard every call:
The underlying
Acceptsclass already handles all call patterns correctly and returns different types based on input.Solution
Add TypeScript overload signatures that narrow the return type:
Also narrows
AcceptsReturnsfrombooleanto the literalfalsetype, since these methods returnfalse(nottrue) 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, narrowedAcceptsReturnsto usefalseinstead ofbooleanCloses #396