Thoughts on the CSV ParseOptions type

Aperta
#6,739 0 commenti 0 reazioni 0 assegnatari Vedi su GitHub

I maintainer di solito rispondono entro 1 giorno

Nessuno ha ancora preso questa issue.

Valutazione

Difficoltà
3/5
Tempo stimato
1-2 giorni
Idoneità per principianti
35/100
Tipo di issue
Bug
Chiarezza
Abbastanza chiara
Stato di attività
Ferma
Stack tecnologico
typescript
Ambito
data

Direzione di ricerca

Inizia dal punto di ingresso di parse del modulo CSV ed esamina il tipo ParseOptions e la firma generica corrente. Confronta il vincolo proposto per le chiavi non valide con il comportamento esistente dei tipi; il lavoro è completato quando gli errori di battitura come lacyQuotes vengono rifiutati, mentre le opzioni valide e l’inferenza di columns continuano a funzionare.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Descrizione

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 }' ...
Lingua principale
TypeScript
Stelle
3.6k
Fork
713
Metriche di merge delle PR
Nessuna PR unita negli ultimi 30g

Preparare l'ambiente

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Altre issue di denoland/std

Tutte le issue di denoland/std

Issue simili

Altre issue su TypeScript

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.