Thoughts on the CSV ParseOptions type

Abierto
#6,739 0 comentarios 0 reacciones 0 asignados Ver en GitHub

Los mantenedores suelen responder en 1 día

Nadie ha tomado este issue todavía.

Evaluación

Dificultad
3/5
Tiempo estimado
1-2 días
Aptitud para principiantes
35/100
Tipo de issue
Error
Claridad
Bastante claro
Estado de actividad
Estancado
Stack tecnológico
typescript
Área
data

Línea de trabajo

Comienza en el punto de entrada de parse del módulo CSV e inspecciona el tipo ParseOptions y la firma genérica actual. Compara la restricción propuesta para claves no válidas con el comportamiento de tipos existente; el trabajo estará terminado cuando se rechacen errores tipográficos como lacyQuotes, mientras las opciones válidas y la inferencia de columns sigan funcionando.

Escrito por el modelo de indexación a partir del texto del issue.

Descripción

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 }' ...
Lenguaje dominante
TypeScript
Estrellas
3.6k
Forks
713
Métricas de merge de PR
Sin PR fusionados en 30 d

Preparar el entorno

Primeros pasos

  1. Lee el issue completo y luego la guía de contribución del proyecto.
  2. Comenta en el issue que vas a ocuparte — evita que dos personas hagan lo mismo.
  3. Haz un fork del repositorio y trabaja en una rama.
  4. Abre un pull request que haga referencia al número del issue.

Más de denoland/std

Todos los issues de denoland/std

Issues similares

Más issues de TypeScript

Recibe los nuevos issues en tu correo

Un resumen breve de issues de GitHub para principiantes.