Skip to content

reply.code().send() does not infer correct type #4883

@pedroescumalha

Description

@pedroescumalha

Prerequisites

  • I have written a descriptive issue title
  • I have searched existing issues to ensure the regression has not already been reported

Last working version

4.18.0

Stopped working in version

4.19.0

Node.js version

18.16.0

Operating system

macOS

Operating system version (i.e. 20.04, 11.3, 10)

13.2.1

💥 Regression Report

Upgrading from 4.18.0 to 4.19.x reply.code().send() can no longer infer the correct type if the provided type is an Array<T>. Seems to do with this commit: 7d02f76.

type ReplyTypeConstrainer<RouteGenericReply, Code extends ReplyKeysToCodes<keyof RouteGenericReply>, ReplyKey = CodeToReplyKey<Code>> =
  Code extends keyof RouteGenericReply ? RouteGenericReply[Code] :
    [ReplyKey] extends [never] ? unknown :
      ReplyKey extends keyof RouteGenericReply ? RouteGenericReply[ReplyKey] :
        unknown;
type TestReplyType = ReplyTypeConstrainer<string[], 200>; // TestReplyType is a string because it fulfils the first condition of ReplyTypeConstrainer

Is the intended behaviour to force users to implement the reply type as { [code: number]: any }, or are users still allowed to provide any type as a reply?

Steps to Reproduce

import fastify from '../fastify'

const server = fastify()

type TestResponse = string[];

server.get<{ Reply: TestResponse }>(
  '/ping',
  { schema: {} },
  async (request, reply) => {
    reply.code(200).send(['']) // type error
  }
)

server.listen({ port: 8080 }, (err, address) => {
  if (err) {
    console.error(err)
    process.exit(1)
  }
  console.log(`Server listening at ${address}`)
})

Expected Behavior

The correct type should be inferred, or strict even more the reply type to be a Record<HttpStatusCode, unknown>.

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugConfirmed bugtypescriptTypeScript related

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions