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 摘要。