Skip to content

Commit 91b971c

Browse files
committed
Update readonly example for ES2025 iterator chains
1 parent 89b8e3c commit 91b971c

2 files changed

Lines changed: 17 additions & 0 deletions

File tree

packages/common/src/Function.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,10 @@ export const identity = <A>(a: A): A => a;
8686
*
8787
* const lookup = readonly(new Map([["key", "value"]]));
8888
* // Type: ReadonlyMap<string, string>
89+
*
90+
* // ES2025 iterator chains: use .toArray() then readonly
91+
* const doubled = readonly([1, 2, 3].values().map((x) => x * 2).toArray());
92+
* // Type: ReadonlyArray<number>
8993
* ```
9094
*
9195
* @experimental

packages/common/test/Function.test.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -153,6 +153,19 @@ describe("readonly", () => {
153153
expectTypeOf(map).toEqualTypeOf<ReadonlyMap<string, number>>();
154154
});
155155
});
156+
157+
describe("with ES2025 iterator .toArray()", () => {
158+
test("converts iterator chain to ReadonlyArray", () => {
159+
const result = readonly(
160+
[1, 2, 3]
161+
.values()
162+
.map((x) => x * 2)
163+
.toArray(),
164+
);
165+
expect(result).toEqual([2, 4, 6]);
166+
expectTypeOf(result).toEqualTypeOf<ReadonlyArray<number>>();
167+
});
168+
});
156169
});
157170

158171
describe("exhaustiveCheck", () => {

0 commit comments

Comments
 (0)