Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Merged
merged 4 commits into from
Jul 21, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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");
```
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We use Prettier stable and Prettier main instead of before/after. https://github.com/prettier/prettier/blob/main/changelog_unreleased/TEMPLATE.md

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 "";
}