Skip to content

Incorrect / overly strict types in 21.4.5 #4565

@joshkel

Description

@joshkel

Runtime

Node.js

Runtime version

24.13.1

Module version

21.4.5

Last module version without issue

21.4.4

Used with

No response

Any other relevant information

No response

What are you trying to achieve or the steps to reproduce?

Take advantage of Joi's type handling to directly reference appropriately typed parameters in request.params and request.query. For example:

function greetUser(id: number) {
  assert(typeof id === "number", "ID must be a number");
  return `Hello, user with ID ${id}!`;
}

server.route({
  method: "GET",
  path: "/hello/{id}",
  handler: (request, _h) => {
    // request.params.id is number, not string, because of Joi validation
    return { message: greetUser(request.params.id) };
  },
  options: {
    validate: {
      params: Joi.object({
        id: Joi.number().required(),
      }),
    },
  },
});

What was the result you got?

Starting in Hapi 21.4.5:

Argument of type 'string' is not assignable to parameter of type 'number'.ts(2345)

What result did you expect?

No errors.

I believe that the stricter types in #4562 are too strict. I appreciate stricter typing (even if it breaks compilation in patch releases - #4563) but this seems too strict - from what I understand, relying on Joi to validate queries and route parameters and coerce / convert their types is a core feature of Hapi and is (I assume) a common practice, but the new types act like it isn't permitted / doesn't happen.

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugBug or defect

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions