Hacktoberfest 2026: những issue maintainer đã đánh dấu cho tháng Mười, đang mở và phù hợp người mới. Xem issue Hacktoberfest

Incorrect / overly strict types in 21.4.5

Đang mở
#4,565 4 bình luận 1 reaction 0 người được giao Xem trên GitHub

Chưa có ai nhận issue này.

Đánh giá

Độ khó
4/5
Thời gian dự kiến
3-5 ngày
Mức phù hợp với người mới
35/100
Loại issue
Lỗi
Độ rõ ràng
Khá rõ ràng
Mức độ hoạt động
Đình trệ
Lĩnh vực
api, backend

Hướng nghiên cứu

Bắt đầu bằng cách đọc các thay đổi kiểu nghiêm ngặt hơn được tham chiếu trong #4562 và tái hiện lỗi TypeScript với ví dụ request.params được Joi xác thực. Theo dõi các kiểu của request.params và request.query, sau đó xác minh rằng tham số số đã được xác thực được báo cáo không còn tạo ra lỗi chuyển từ chuỗi sang số, mà không làm yếu các kiểm tra kiểu không liên quan.

Do mô hình lập chỉ mục viết ra từ nội dung của issue.

Mô tả

bug
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.

Ngôn ngữ chính
JavaScript
Star
14.8k
Fork
1.4k
Chỉ số merge pull request
Không có pull request nào được merge trong 30 ngày

Chuẩn bị môi trường

Bắt đầu từ đâu

  1. Đọc hết issue, rồi đọc hướng dẫn đóng góp của dự án.
  2. Bình luận trên issue rằng bạn sẽ nhận — tránh hai người làm cùng một việc.
  3. Fork repository và làm thay đổi trên một nhánh.
  4. Mở pull request có tham chiếu số hiệu của issue.

Issue khác của hapijs/hapi

Tất cả issue của hapijs/hapi

Issue tương tự

Thêm issue về JavaScript

Nhận issue mới trong hộp thư của bạn

Bản tóm tắt ngắn những issue GitHub phù hợp với người mới.