Skip to content

TypeScript RouteOptions interface generics missing defaults #1667

@fox1t

Description

@fox1t

🐛 Bug Report

Working with routes definition I found that RouteOptions interface hasn't default generics declared. (https://github.com/fastify/fastify/blob/master/fastify.d.ts#L266) as opposed to RouteShorthandOptions(https://github.com/fastify/fastify/blob/master/fastify.d.ts#L230) that it extends from.
Is this a wanted behavior? I think this is too verbose since it forces to pass every time a route is declared a full set of same generics: RouteOptions<Server, IncomingMessage, ServerResponse>.

To Reproduce

Removing generics (<Server, IncomingMessage, ServerResponse>) TypeScript pops a compiler error:
Generic type 'RouteOptions<HttpServer, HttpRequest, HttpResponse, Query, Params, Headers, Body>' requires between 3 and 7 type arguments.

import { Server, IncomingMessage, ServerResponse } from 'http'
import { RouteOptions, RouteShorthandOptions } from 'fastify'

const listAttendance: RouteOptions<Server, IncomingMessage, ServerResponse> = {
  method: 'GET',
  url: '/',
  handler: (req, reply) => {
    reply.send([])
  },
}

const listAttendances: RouteShorthandOptions = {}

Expected behavior

Adding the defaults to RouteOptions the error goes away. I think this is the desirable behavior.

Paste the results here:

/**
   * Route configuration options such as "url" and "method"
   */
  interface RouteOptions<
    HttpServer = http.Server,
    HttpRequest = http.IncomingMessage,
    HttpResponse = http.ServerResponse,
    Query = DefaultQuery,
    Params = DefaultParams,
    Headers = DefaultHeaders,
    Body = DefaultBody
  >
    extends RouteShorthandOptions<
      HttpServer,
      HttpRequest,
      HttpResponse,
      Query,
      Params,
      Headers,
      Body
    > {
    method: HTTPMethod | HTTPMethod[]
    url: string
    handler: RequestHandler<HttpRequest, HttpResponse, Query, Params, Headers, Body>
  }

Your Environment

  • node version: 10
  • fastify version: 2.4.1
  • os: Mac

Metadata

Metadata

Assignees

No one assigned

    Labels

    typescriptTypeScript relatedv2.xIssue or pr related to Fastify v2

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions