nodejs / nodejs/node

`FormData` implementation/types mismatch

Aperta
#58,481 6 commenti 0 reazioni 0 assegnatari Vedi su GitHub

Nessuno ha ancora preso questa issue.

Lingua principale
JavaScript
Stelle
122k
Fork
37.4k
Merge medio
4g 3h
PR unite (30g)
272

Descrizione

Version

v22.6.0

Platform
Darwin esmbp2023 23.5.0 Darwin Kernel Version 23.5.0: Wed May  1 20:17:33 PDT 2024; root:xnu-10063.121.3~5/RELEASE_ARM64_T6031 arm64
Subsystem

Fetch

What steps will reproduce the bug?

Regarding FormData type definition (https://github.com/nodejs/node/blob/main/deps/undici/src/types/formdata.d.ts):

The majority of methods are defined as instance methods (e.g., append, set, getAll, etc.):

// EXAMPLE
export declare class FormData {
  // ...
  append (name: string, value: unknown, fileName?: string): void
  // ...
}

But the following methods are defined as properties:

  • forEach
  • keys
  • values
  • entries
  • Symbol.iterator
// EXAMPLE
export declare class FormData {
  // ...
  forEach: (
    callbackfn: (value: FormDataEntryValue, key: string, iterable: FormData) => void,
    thisArg?: unknown
  ) => void
  // ...
}

This prevents extending those methods when extending FormData:

// EXAMPLE
class StrictFormData<T> extends FormData {
    /** [MDN Reference](https://developer.mozilla.org/docs/Web/API/FormData/entries) */
    entries = <TK extends keyof T & string>(): IterableIterator<
        [TK, T[TK]]
    > => {
        // ERROR: TS2855: Class field entries defined by the parent class is not accessible in the child class via super.
        return super.entries() as IterableIterator<[TK, T[TK]]>;
    };
}
// EXAMPLE
class StrictFormData<T> extends FormData {
    /** [MDN Reference](https://developer.mozilla.org/docs/Web/API/FormData/entries) */
    // ERROR: TS2425: Class FormData defines instance member property entries, but extended class StrictFormData<T> defines it as instance member function.
    entries<TK extends keyof T & string>(): IterableIterator<[TK, T[TK]]> {
        // ERROR: TS2855: Class field entries defined by the parent class is not accessible in the child class via super.
        return super.entries() as IterableIterator<[TK, T[TK]]>;
    }
}

Is it a mistake in the type definitions?

When looking at the implementation (https://github.com/nodejs/node/blob/main/deps/undici/src/lib/web/fetch/formdata.js) I don't see any of those methods added, neither "statically", nor dynamically upon construction. I might be missing something because exactly those atypically defined methods are the ones that I cannot find in the implementation.

Thanks!

How often does it reproduce? Is there a required condition?

See above.

What is the expected behavior? Why is that the expected behavior?

See above.

What do you see instead?

See above.

Additional information

See above.

Guida per i contributori

Apri la guida per i contributori

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.

Direzione di ricerca

Confronta deps/undici/src/types/formdata.d.ts con deps/undici/src/lib/web/fetch/formdata.js, concentrandoti su forEach, keys, values, entries e Symbol.iterator. Riproduci gli errori di subclassing di TypeScript riportati nell’issue e determina se le dichiarazioni o l’implementazione differiscono dal comportamento previsto. Il lavoro è concluso quando i tipi FormData e il comportamento a runtime sono coerenti e il caso di estensione segnalato è stato risolto.

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

Valutazione

Stack tecnologico
javascript, nodejs, typescript
Ambito
api, backend
Tipo di issue
Bug
Difficoltà
3/5
Tempo stimato
1-2 giorni
Stato di attività
Tranquilla
Chiarezza
Abbastanza chiara
Idoneità per principianti
55/100

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.