hapijs / hapijs/hapi

Incorrect / overly strict types in 21.4.5

Open
#4,565 4 comments 1 reaction 0 assignees View on GitHub
bug
Dominant language
JavaScript
Stars
14.8k
Forks
1.4k
Avg merge
22d 3h
Merged PRs (30d)
1

Description

### 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:

```ts
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.

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.