Repository navigation
Type getJson() for every JSON value, with an optional type argument - #216
Open
theluckystrike wants to merge 1 commit into
Open
theluckystrike wants to merge 1 commit into
theluckystrike wants to merge 1 commit into
Conversation
getJson() was typed as Promise<object | undefined>, so bodies like 42, "text", true or null got the wrong type, and callers couldn't name the type they expect without casting through unknown. It's now generic: getJson<R = object | string | number | boolean | null>(), still returning undefined when the body can't be decoded or parsed (httptoolkit#155). New .json tests in request-utils.spec.ts cover an object, an array, a number, null, invalid JSON, an unknown encoding and an explicit type argument. On main the spec doesn't compile (TS2558: Expected 0 type arguments, but got 1). With this change it passes (16 passing), tsc builds cleanly and the full test:node run gives 842 passing, 0 failing.
This branch has not been deployed
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.
This follows the shape you suggested in #155.
getJson()onCompletedBodywas typed asPromise<object | undefined>, so a JSON array was fine but a body of42,"text",trueornullwas typed wrong, and callers had to cast throughunknownto name their own type.It's now
getJson<R = object | string | number | boolean | null>(): Promise<R | undefined>. The default covers every valueJSON.parsecan return, with noany. Callers can pass their own type, likegetJson<{ id: number }>().undefinedstill means the body couldn't be decoded or parsed, and the doc comment now says so.I used
objectrather than{} | []because of the point leumasme raised:{}also accepts primitives and[]is only the empty tuple. Arrays are still covered byobject. The one break is code likeif (json && 'x' in json), since the type now includes primitives.Added
.jsontests to request-utils.spec.ts for an object, an array, a number, null, invalid JSON and an unknown encoding, plus one call with an explicit type argument. On main that spec fails to compile (TS2558, expected 0 type arguments) becausegetJsontakes no type argument there. With this changenpm run build:srcis clean andnpm run test:nodegives 842 passing, 0 failing. I didn't run the browser tests.Closes #155