Skip to content

[supertest] support generic type for expected response body#74967

Open
SchroederSteffen wants to merge 4 commits into
DefinitelyTyped:masterfrom
SchroederSteffen:supertest-generic-expect
Open

[supertest] support generic type for expected response body#74967
SchroederSteffen wants to merge 4 commits into
DefinitelyTyped:masterfrom
SchroederSteffen:supertest-generic-expect

Conversation

@SchroederSteffen
Copy link
Copy Markdown
Contributor

@SchroederSteffen SchroederSteffen commented May 8, 2026

Please fill in this template.

Select one of these and delete the others:

If changing an existing definition:


We declare DTO classes for our services to describe the request & response bodies.
When testing our services using supertest, we want to ensure that the expected bodies fit the return type of the endpoint, e.g. as follows:

interface ResponseBody { a: boolean; b: number; }
request.get("/").expect<ResponseBody>({ a: true, b: 42 })

I also thought about adding generics to the request methods (.get(), .post(), etc.) to inherit these types for the .expect() methods, but I opted for this simpler solution for now.

Fixes forwardemail/supertest#720.
Fixes forwardemail/superagent#1834.

Related:

@typescript-bot typescript-bot moved this to Needs Author Action in Pull Request Status Board May 8, 2026
@SchroederSteffen SchroederSteffen changed the title [supertest] support generic type for expect [supertest] support generic type for expected response body May 8, 2026
@SchroederSteffen SchroederSteffen marked this pull request as ready for review May 8, 2026 19:24
@typescript-bot
Copy link
Copy Markdown
Contributor

typescript-bot commented May 8, 2026

@SchroederSteffen Thank you for submitting this PR!

This is a live comment that I will keep updated.

1 package in this PR

Code Reviews

Because this is a widely-used package, a DT maintainer will need to review it before it can be merged.

You can test the changes of this PR in the Playground.

Status

  • ✅ No merge conflicts
  • ✅ Continuous integration tests have passed
  • 🕐 Most recent commit is approved by a DT maintainer

Once every item on this list is checked, I'll ask you for permission to merge and publish the changes.


Diagnostic Information: What the bot saw about this PR
{
  "type": "info",
  "now": "-",
  "pr_number": 74967,
  "author": "SchroederSteffen",
  "headCommitOid": "1cff41d520d1ecae6246fbf8f829a00c5dad02e3",
  "mergeBaseOid": "dafc26dc3da728f912025f73d2629374a9b1aba9",
  "lastPushDate": "2026-05-08T12:49:38.000Z",
  "lastActivityDate": "2026-05-12T05:22:55.000Z",
  "hasMergeConflict": false,
  "isFirstContribution": false,
  "tooManyFiles": false,
  "hugeChange": false,
  "tooManyCommits": false,
  "tooManyReviews": false,
  "popularityLevel": "Critical",
  "pkgInfo": [
    {
      "name": "supertest",
      "kind": "edit",
      "files": [
        {
          "path": "types/supertest/lib/test.d.ts",
          "kind": "definition"
        },
        {
          "path": "types/supertest/supertest-tests.ts",
          "kind": "test"
        }
      ],
      "owners": [
        "varju",
        "pietu",
        "DavidTanner"
      ],
      "addedOwners": [],
      "deletedOwners": [],
      "popularityLevel": "Critical"
    }
  ],
  "reviews": [
    {
      "type": "changereq",
      "reviewer": "lukaselmer",
      "date": "2026-05-12T02:59:50.000Z"
    }
  ],
  "mainBotCommentID": 4409232256,
  "ciResult": "pass"
}

@typescript-bot
Copy link
Copy Markdown
Contributor

🔔 @varju @pietu @DavidTanner — please review this PR in the next few days. Be sure to explicitly select Approve or Request Changes in the GitHub UI so I know what's going on.

@typescript-bot typescript-bot moved this from Needs Author Action to Waiting for Code Reviews in Pull Request Status Board May 8, 2026
@typescript-bot typescript-bot moved this from Waiting for Code Reviews to Needs Maintainer Review in Pull Request Status Board May 8, 2026
@typescript-bot typescript-bot moved this from Needs Maintainer Review to Waiting for Code Reviews in Pull Request Status Board May 10, 2026
@typescript-bot typescript-bot moved this from Waiting for Code Reviews to Needs Maintainer Review in Pull Request Status Board May 10, 2026

expect(status: number, callback?: CallbackHandler): this;
expect(status: number, body: any, callback?: CallbackHandler): this;
expect<T extends any = any>(status: number, body: T, callback?: CallbackHandler): this;
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.

This is typically a bad idea; this is tantamount to allowing any because anything can infer into it anyway. Adding the type param doesn't really add much more safety, no?

(We are supposed to have a lint rule that complains about this)

Copy link
Copy Markdown
Contributor

@lukaselmer lukaselmer May 12, 2026

Choose a reason for hiding this comment

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

Agree.

expect<T>(...) is enough. extends any = any can be removed

Copy link
Copy Markdown
Contributor

@lukaselmer lukaselmer left a comment

Choose a reason for hiding this comment

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

Let's remove extends any = any

@typescript-bot typescript-bot added the Revision needed This PR needs code changes before it can be merged. label May 12, 2026
@typescript-bot typescript-bot moved this from Needs Maintainer Review to Needs Author Action in Pull Request Status Board May 12, 2026
@typescript-bot
Copy link
Copy Markdown
Contributor

@SchroederSteffen One or more reviewers has requested changes. Please address their comments. I'll be back once they sign off or you've pushed new commits. Thank you!

@jakebailey
Copy link
Copy Markdown
Member

That doesn't change the fact that the type param is only used once

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Critical package Revision needed This PR needs code changes before it can be merged.

Projects

Status: Needs Author Action

Development

Successfully merging this pull request may close these issues.

[feat] Support Typescript generic to type the response body Support Typescript generic to type the response body

4 participants