Add typing to support readonly array properties of AST Node (#15127)

* Add typing to support `readonly` array properties of AST Node

* add changelog

* fix changelog

* add test case
This commit is contained in:
auvred authored and GitHub committed 2023-07-22 01:43:26 +09:00
1 parent 5b38f6ec4f
commit ceed405395
3 files changed
+36 -1

No files matched your search

+32
View File
@@ -0,0 +1,32 @@
#### Add typing to support `readonly` array properties of AST Node (#15127 by @auvred)
<!-- prettier-ignore -->
```tsx
// Input
interface TestNode {
readonlyArray: readonly string[];
}
declare const path: AstPath<TestNode>;
path.map(() => "", "readonlyArray");
// Prettier stable
interface TestNode {
readonlyArray: readonly string[];
}
declare const path: AstPath<TestNode>;
path.map(() => "", "readonlyArray");
// ^ Argument of type '"readonlyArray"' is not assignable to parameter of type '"regularArray"'. ts(2345)
// Prettier main
interface TestNode {
readonlyArray: readonly string[];
}
declare const path: AstPath<TestNode>;
path.map(() => "", "readonlyArray");
```
+1 -1
View File
@@ -33,7 +33,7 @@ type ArrayElement<T> = T extends Array<infer E> ? E : never;
// A union of the properties of the given object that are arrays.
type ArrayProperties<T> = {
[K in keyof T]: NonNullable<T[K]> extends any[] ? K : never;
[K in keyof T]: NonNullable<T[K]> extends readonly any[] ? K : never;
}[keyof T];
// A union of the properties of the given array T that can be used to index it.
+3
View File
@@ -12,6 +12,7 @@ interface Nested2 {
kind: "2";
item3: Nested3;
list3: Nested3[];
list5: readonly Nested3[];
}
interface Nested3 {
kind: "3";
@@ -155,5 +156,7 @@ function print(
// @ts-expect-error
path.map(print, "item2", "item3");
path.map(print, "item2", "list5");
return "";
}