Iterator.prototype.flatMap accepts primitive string results

未關閉 適合新手
#65,370 0 則留言 0 個 reaction 已指派 0 人 在 GitHub 檢視

維護者通常 1 天內回覆

還沒有人認領這個 Issue。

評估

難度
2/5
預估耗時
1-3 小時
新手友好度
84/100
Issue 類型
缺陷
描述清晰度
描述清楚
活躍度
活躍
技術堆疊
typescript

研究方向

從 lib.es2025.iterator.d.ts 開始,比較 flatMap 回呼的回傳型別與 Iterator.concat 使用的物件限制。issue 提供了嚴格的 TypeScript 重現範例和預期的型別變更;執行提供的 tsc 命令,檢查原始字串會被拒絕,而陣列、迭代器物件和裝箱字串仍會被接受。

由索引模型根據 Issue 內容生成。

描述

Compilation target

ES5 (default).

Library

es2025

Missing / Incorrect Definition

lib.es2025.iterator.d.ts accepts a primitive string as the mapper's return value because a string satisfies Iterable<U>. ECMAScript rejects that value when the iterator is consumed.

TypeScript 7.1.0-dev.20261106.1 accepts the sample below. Node 26.13.0 throws TypeError: Iterator.prototype.flatMap called on non-object when toArray() consumes the iterator. TypeScript should reject the callback's return type.

Copy the code under Sample Code into repro.ts in an empty directory. Run the following commands there.

npx --yes [email protected] tsc --noEmit --strict --lib es2025 repro.ts
node repro.ts

Intersect the callback's existing return union with object. This matches the object constraint used by Iterator.concat and preserves arrays, iterator objects, and boxed strings.

-        flatMap<U>(callback: (value: T, index: number) => Iterator<U, unknown, undefined> | Iterable<U, unknown, undefined>): IteratorObject<U, undefined, unknown>;
+        flatMap<U>(callback: (value: T, index: number) => (Iterator<U, unknown, undefined> | Iterable<U, unknown, undefined>) & object): IteratorObject<U, undefined, unknown>;
Sample Code
Iterator.from([1]).flatMap(() => "ab").toArray();
Documentation Link

ECMAScript Iterator.prototype.flatMap

主要語言
Go
星號
112k
分支
15.4k
平均合併
3 天 2 小時
30 天內合併 PR
149

環境準備

在 Codespaces 中開啟

在瀏覽器裡用你自己的 GitHub 帳號啟動這個專案的開發容器。

從這裡開始

  1. 先讀完整個 Issue,再讀專案的貢獻指南。
  2. 在 Issue 下留言說明你要接手 —— 這能避免兩個人做同樣的事。
  3. Fork 儲存庫,在一個分支上完成修改。
  4. 送出 Pull Request,並在描述裡引用這個 Issue 編號。

microsoft/TypeScript 的其他 Issue

查看 microsoft/TypeScript 的全部 Issue

相似的 Issue

更多 Go Issue

把新 issue 寄到你的電子郵件信箱

精選適合新手參與的 GitHub issue 摘要。