Thoughts on the CSV ParseOptions type

未关闭
#6,739 0 条评论 0 个 reaction 已指派 0 人 在 GitHub 查看

维护者通常 1 天内回复

还没有人认领这个 Issue。

评估

难度
3/5
预计耗时
1-2 天
新手友好度
35/100
Issue 类型
缺陷
描述清晰度
基本清楚
活跃度
停滞
技术栈
typescript
领域
data

调研方向

从 CSV 模块的 parse 入口开始,检查 ParseOptions 类型和当前的泛型签名。将提议的无效键约束与现有的类型行为进行比较;当 lacyQuotes 之类的拼写错误会被拒绝,同时有效选项和 columns 推断仍能继续工作时,即表示完成。

由索引模型根据 Issue 内容生成。

描述

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 }' ...
主要语言
TypeScript
星标
3.6k
派生
713
PR 合并指标
30 天内没有已合并 PR

环境准备

  • 没有 Dockerfile 或 Docker Compose 文件
  • 没有 Pull Request 模板
  • 阅读贡献指南

从这里开始

  1. 先读完整个 Issue,再读项目的贡献指南。
  2. 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
  3. Fork 仓库,在一个分支上完成修改。
  4. 提交 Pull Request,并在描述里引用这个 Issue 编号。

denoland/std 的其他 Issue

查看 denoland/std 的全部 Issue

相似的 Issue

更多 TypeScript Issue

把新 issue 发到你的邮箱

精选适合新手参与的 GitHub issue 摘要。