Skip to content

Commit

Permalink
Add typing to support readonly array properties of AST Node (pretti…
Browse files Browse the repository at this point in the history
…er#15127)

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

* add changelog

* fix changelog

* add test case
  • Loading branch information
auvred authored and medikoo committed Feb 15, 2024
1 parent 133425d commit 7c2769a
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 1 deletion.
32 changes: 32 additions & 0 deletions changelog_unreleased/api/15127.md
Original file line number Diff line number Diff line change
@@ -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");
```
2 changes: 1 addition & 1 deletion src/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
3 changes: 3 additions & 0 deletions tests/dts/unit/cases/print.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ interface Nested2 {
kind: "2";
item3: Nested3;
list3: Nested3[];
list5: readonly Nested3[];
}
interface Nested3 {
kind: "3";
Expand Down Expand Up @@ -155,5 +156,7 @@ function print(
// @ts-expect-error
path.map(print, "item2", "item3");

path.map(print, "item2", "list5");

return "";
}

0 comments on commit 7c2769a

Please sign in to comment.