From ceed4053952f471fe9fffca0df0d8da220bd5c5a Mon Sep 17 00:00:00 2001 From: auvred <61150013+auvred@users.noreply.github.com> Date: Fri, 21 Jul 2023 19:43:26 +0300 Subject: [PATCH] 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 --- changelog_unreleased/api/15127.md | 32 +++++++++++++++++++++++++++++++ src/index.d.ts | 2 +- tests/dts/unit/cases/print.ts | 3 +++ 3 files changed, 36 insertions(+), 1 deletion(-) create mode 100644 changelog_unreleased/api/15127.md diff --git a/changelog_unreleased/api/15127.md b/changelog_unreleased/api/15127.md new file mode 100644 index 000000000..4e341bd97 --- /dev/null +++ b/changelog_unreleased/api/15127.md @@ -0,0 +1,32 @@ +#### Add typing to support `readonly` array properties of AST Node (#15127 by @auvred) + + +```tsx +// Input +interface TestNode { + readonlyArray: readonly string[]; +} + +declare const path: AstPath; + +path.map(() => "", "readonlyArray"); + +// Prettier stable +interface TestNode { + readonlyArray: readonly string[]; +} + +declare const path: AstPath; + +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; + +path.map(() => "", "readonlyArray"); +``` diff --git a/src/index.d.ts b/src/index.d.ts index 0b0c989a8..c36839544 100644 --- a/src/index.d.ts +++ b/src/index.d.ts @@ -33,7 +33,7 @@ type ArrayElement = T extends Array ? E : never; // A union of the properties of the given object that are arrays. type ArrayProperties = { - [K in keyof T]: NonNullable extends any[] ? K : never; + [K in keyof T]: NonNullable extends readonly any[] ? K : never; }[keyof T]; // A union of the properties of the given array T that can be used to index it. diff --git a/tests/dts/unit/cases/print.ts b/tests/dts/unit/cases/print.ts index 25679963e..fb75c93fb 100644 --- a/tests/dts/unit/cases/print.ts +++ b/tests/dts/unit/cases/print.ts @@ -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 ""; }