microsoft / microsoft/TypeScript

Add `--strictObjectIterables` (bikeshed): exclude primitive (`string`) from `Iterable` types

Aperta
#59,676 4 commenti 1 reazione 0 assegnatari Vedi su GitHub

Nessuno ha ancora preso questa issue.

Needs Proposal Suggestion
Lingua principale
Go
Stelle
111k
Fork
14.4k
Merge medio
1g 19h
PR unite (30g)
117

Descrizione

🔍 Search Terms

"iterable object", "string"

✅ Viability Checklist
⭐ Suggestion

At the TC39 meeting in 2024-07, it was decided that Iterable expects objects.

Reject primitives in iterable-taking positions

Any time an iterable or async-iterable value (a value that has a Symbol.iterator or Symbol.asyncIterator method) is expected, primitives should be treated as if they were not iterable. Usually, this will mean throwing a TypeError. If the user provides a primitive wrapper Object such as a String Object, however, it should be treated like any other Object.

Although primitive Strings are default iterable (String.prototype has a Symbol.iterator method which enumerates code points), it is now considered a mistake to iterate a String without specifying whether the String is providing an abstraction over code units, code points, grapheme clusters, or something else.

NB: This convention is new as of 2024, and most earlier parts of the language do not follow it. In particular, positional destructuring (both binding and assignment), array spread, argument spread, for-of loops, yield *, the Set and AggregateError constructors, Object.groupBy, Map.groupBy, Promise.all, Promise.allSettled, Promise.any, Promise.race, Array.from, the static from methods on typed array constructors, and Iterator.from (Stage 3 at time of writing) all accept primitives where iterables are expected.

https://github.com/tc39/how-we-work/blob/main/normative-conventions.md#reject-primitives-in-iterable-taking-positions

To follow this decision, the Iterable and string types should be separated, and APIs that accept both types should be required to explicitly specify Iterable<any> | string. Since this would be a breaking change, how about adding a new --strictObjectIterables (bikeshed) option?

In practice, the upcoming ReadableStream.from will accept Iterable<any> and AsyncIterable<any>, but will be restricted to objects. https://github.com/whatwg/streams/pull/1310

📃 Motivating Example

Enabling the --strictObjectIterables option raises a type error in the following example:

const iterable: Iterable<any> = "string";
💻 Use Cases
  1. What do you want to use this for?

    Used for ReadableStream.from and other APIs to be added in the future.

  2. What shortcomings exist with current approaches?

    Iterable object and string are not distinguished by default.

  3. What workarounds are you using in the meantime?

    Maybe Iterable<any> & object can reject string.

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

Inizia esaminando il comportamento proposto di --strictObjectIterables e la convenzione TC39 collegata, quindi traccia il modo in cui Iterable e string sono rappresentati e verificati in TypeScript. Usa l’assegnazione motivante come caso di controllo dei tipi e determina le API interessate. Il lavoro è completato quando l’opzione rifiuta le stringhe primitive come Iterable, consentendo però l’uso esplicito di Iterable | string.

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

Valutazione

Stack tecnologico
javascript, typescript
Ambito
compilers
Tipo di issue
Funzionalità
Difficoltà
5/5
Tempo stimato
Più di una settimana
Stato di attività
Ferma
Chiarezza
Abbastanza chiara
Idoneità per principianti
25/100

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.