Thoughts on the CSV ParseOptions type

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

Maintainer thường phản hồi trong vòng 1 ngày

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

Đánh giá

Độ khó
3/5
Thời gian dự kiến
1-2 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ệ
Công nghệ
typescript
Lĩnh vực
data

Hướng nghiên cứu

Bắt đầu từ điểm vào parse của mô-đun CSV và kiểm tra kiểu ParseOptions cùng chữ ký generic hiện tại. So sánh constraint được đề xuất cho các khóa không hợp lệ với hành vi kiểu hiện có; công việc được hoàn tất khi các lỗi chính tả như lacyQuotes bị từ chối, trong khi các tùy chọn hợp lệ và việc suy luận columns vẫn tiếp tục hoạt động.

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

Mô tả

Hello, I don't know if it is intentional and what the gains are of the CSV module's options that are passed in through the parse function.

This is the function signature:

export function parse<const T extends ParseOptions>(
  input: string,
  options: T,
): ParseResult<ParseOptions, T>

I understand the reason for having it extend like above gives the possibility of the parser to extract the columns attribute and use that to make the resulting array "know" which keys are in object.

But the current implementation allows the options to be any record that extends the ParseOptions type, allowing developers to add extra options to the object that are then ignored if just one valid key is given, as in this example:

parse('a;b;c', { skipFirstRow: true, lacyQuotes: true })

This doesn't get caught.

This can become an issue when making typos as the TypeScript compiler doesn't catch any mistakes.

I suggest instead that we make the function signature something like this:

type ExcludeInvalidKeys<T> = Record<
  Exclude<keyof T, keyof ParseOptions>,
  never
>

export function parse<const T extends ParseOptions>(
  input: string,
  options: ExcludeInvalidKeys<T>,
): ParseResult<ParseOptions, T>

Which now correctly rejects invalid keys, but the error could be nicer. I just don't have the TypeScript-fu to do it. The error becomes:

type 'false' is not assignable to 'never'

The expected type comes from property 'lacyQuotes' which is declared here on type '{ readonly skipFirstRow: true; readonly lacyQuotes: true }' ...
Ngôn ngữ chính
TypeScript
Star
3.6k
Fork
713
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 denoland/std

Tất cả issue của denoland/std

Issue tương tự

Thêm issue về TypeScript

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.