Skip to content

Commit 7c2769a

Browse files
auvredmedikoo
authored andcommitted
Add typing to support readonly array properties of AST Node (prettier#15127)
* Add typing to support `readonly` array properties of AST Node * add changelog * fix changelog * add test case
1 parent 133425d commit 7c2769a

File tree

3 files changed

+36
-1
lines changed

3 files changed

+36
-1
lines changed

changelog_unreleased/api/15127.md

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
#### Add typing to support `readonly` array properties of AST Node (#15127 by @auvred)
2+
3+
<!-- prettier-ignore -->
4+
```tsx
5+
// Input
6+
interface TestNode {
7+
readonlyArray: readonly string[];
8+
}
9+
10+
declare const path: AstPath<TestNode>;
11+
12+
path.map(() => "", "readonlyArray");
13+
14+
// Prettier stable
15+
interface TestNode {
16+
readonlyArray: readonly string[];
17+
}
18+
19+
declare const path: AstPath<TestNode>;
20+
21+
path.map(() => "", "readonlyArray");
22+
// ^ Argument of type '"readonlyArray"' is not assignable to parameter of type '"regularArray"'. ts(2345)
23+
24+
// Prettier main
25+
interface TestNode {
26+
readonlyArray: readonly string[];
27+
}
28+
29+
declare const path: AstPath<TestNode>;
30+
31+
path.map(() => "", "readonlyArray");
32+
```

src/index.d.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ type ArrayElement<T> = T extends Array<infer E> ? E : never;
3333

3434
// A union of the properties of the given object that are arrays.
3535
type ArrayProperties<T> = {
36-
[K in keyof T]: NonNullable<T[K]> extends any[] ? K : never;
36+
[K in keyof T]: NonNullable<T[K]> extends readonly any[] ? K : never;
3737
}[keyof T];
3838

3939
// A union of the properties of the given array T that can be used to index it.

tests/dts/unit/cases/print.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ interface Nested2 {
1212
kind: "2";
1313
item3: Nested3;
1414
list3: Nested3[];
15+
list5: readonly Nested3[];
1516
}
1617
interface Nested3 {
1718
kind: "3";
@@ -155,5 +156,7 @@ function print(
155156
// @ts-expect-error
156157
path.map(print, "item2", "item3");
157158

159+
path.map(print, "item2", "list5");
160+
158161
return "";
159162
}

0 commit comments

Comments
 (0)