microsoft / microsoft/TypeScript
Add `--strictObjectIterables` (bikeshed): exclude primitive (`string`) from `Iterable` types
Dieses Issue hat noch niemand übernommen.
- Vorherrschende Sprache
- Go
- Sterne
- 111k
- Forks
- 14.4k
- Ø Merge
- 1 T. 19 Std.
- Gemergte PRs (30 T.)
- 117
Beschreibung
🔍 Search Terms
"iterable object", "string"
✅ Viability Checklist
- This wouldn't be a breaking change in existing TypeScript/JavaScript code
- This wouldn't change the runtime behavior of existing JavaScript code
- This could be implemented without emitting different JS based on the types of the expressions
- This isn't a runtime feature (e.g. library functionality, non-ECMAScript syntax with JavaScript output, new syntax sugar for JS, etc.)
- This isn't a request to add a new utility type: https://github.com/microsoft/TypeScript/wiki/No-New-Utility-Types
- This feature would agree with the rest of our Design Goals: https://github.com/Microsoft/TypeScript/wiki/TypeScript-Design-Goals
⭐ 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.iteratororSymbol.asyncIteratormethod) is expected, primitives should be treated as if they were not iterable. Usually, this will mean throwing aTypeError. 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.prototypehas aSymbol.iteratormethod 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 *, theSetandAggregateErrorconstructors,Object.groupBy,Map.groupBy,Promise.all,Promise.allSettled,Promise.any,Promise.race,Array.from, the staticfrommethods on typed array constructors, andIterator.from(Stage 3 at time of writing) all accept primitives where iterables are expected.
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
-
What do you want to use this for?
Used for
ReadableStream.fromand other APIs to be added in the future. -
What shortcomings exist with current approaches?
Iterableobject andstringare not distinguished by default. -
What workarounds are you using in the meantime?
Maybe
Iterable<any> & objectcan rejectstring.
Beitragsleitfaden
Erste Schritte
- Lies das ganze Issue und danach den Beitragsleitfaden des Projekts.
- Schreib ins Issue, dass du es übernimmst — das erspart doppelte Arbeit.
- Forke das Repository und arbeite in einem Branch.
- Öffne einen Pull Request, der die Issue-Nummer nennt.
Rechercherichtung
Beginne damit, das vorgeschlagene Verhalten von --strictObjectIterables und die verlinkte TC39-Konvention zu prüfen, und verfolge anschließend, wie Iterable und string in TypeScript dargestellt und geprüft werden. Verwende die motivierende Zuweisung als Fall für die Typprüfung und ermittle die betroffenen APIs. Die Arbeit ist abgeschlossen, wenn die Option primitive strings als Iterable ablehnt, aber die explizite Verwendung von Iterable | string zulässt.
Vom Indexierungsmodell aus dem Issue-Text verfasst.
Bewertung
- Tech-Stack
- javascript, typescript
- Bereich
- compilers
- Issue-Typ
- Feature
- Schwierigkeit
- 5/5
- Geschätzter Aufwand
- Über eine Woche
- Aktivitätsstatus
- Veraltet
- Klarheit
- Größtenteils klar
- Anfängerfreundlichkeit
- 25/100