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

TypesScript types issue with node with readonly array and .map, .each on AstPath #15034

Closed
timotheeguerin opened this issue Jul 7, 2023 · 2 comments · Fixed by #15127
Closed
Labels
area:type definition Issues with *d.ts files locked-due-to-inactivity Please open a new issue and fill out the template instead of commenting. type:bug Issues identifying ugly output, or a defect in the program

Comments

@timotheeguerin
Copy link

Environments:

  • Prettier Version: 3.0.0
  • Running Prettier via: n/a / TypeScript
  • Runtime: Node v18.16.0, Typescript 5.1
  • Operating System: MacOS
  • Prettier plugins (if any): Issue happen when writing a plugin

Typescript produce an error when trying to use .map for a property that is a readonly array.

Steps to reproduce:

Using the following typescript code will produce an error when trying to .map a property that is a readonly array(same for .each)

import { AstPath } from "prettier";

interface TestNode {
  regularArray: string[];
  readonlyArray: readonly string[];
}

const path: AstPath<TestNode> = undefined as any;

// Works fine
path.map(() => "", "regularArray");
// Ts error
path.map(() => "", "readonlyArray");
//                 ^ Argument of type '"readonlyArray"' is not assignable to parameter of type '"regularArray"'.ts(2345)

Expected behavior:
Should not be any difference between a readonly and regular array.

Actual behavior:
Typescript produce an error when trying to use .map for a property that is a readonly array.
image

@timotheeguerin timotheeguerin changed the title TypesScript types issue with node with readonly array TypesScript types issue with node with readonly array and .map, .each on AstPath Jul 7, 2023
@auvred
Copy link
Contributor

auvred commented Jul 7, 2023

It guess that to fix this issue it is enough to add readonly here:

[K in keyof T]: NonNullable<T[K]> extends any[] ? K : never;

type ArrayProperties<T> = { 
   [K in keyof T]: NonNullable<T[K]> extends readonly any[] ? K : never; 
 }[keyof T];  //                               ^ here

TS Playground

If this is correct, I can submit a PR

@timotheeguerin
Copy link
Author

timotheeguerin commented Jul 7, 2023

yeah, i copied the types after and adding that readonly works, though got into some very bad performance issue in the IDE. Not sure if that was related or due to something else at the same time.

Edit: perf issue seems to have been caused by using both prettier.AstPath and my copy if it at the same time. Typescript didn't seem to like converting between those 2 complex types.

@sosukesuzuki sosukesuzuki added area:type definition Issues with *d.ts files type:bug Issues identifying ugly output, or a defect in the program labels Jul 16, 2023
@github-actions github-actions bot added the locked-due-to-inactivity Please open a new issue and fill out the template instead of commenting. label Nov 23, 2023
@github-actions github-actions bot locked as resolved and limited conversation to collaborators Nov 23, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
area:type definition Issues with *d.ts files locked-due-to-inactivity Please open a new issue and fill out the template instead of commenting. type:bug Issues identifying ugly output, or a defect in the program
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants