Visitar URL original
TypesScript types issue with node with readonly array and `.map`, `.each` on `AstPath` · Issue #15034 · prettier/prettier · GitHub
Skip to content

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

Closed

Description

@timotheeguerin

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

Activity

  1. changed the title [-]TypesScript types issue with node with readonly array[/-] [+]TypesScript types issue with node with readonly array and `.map`, `.each` on `AstPath`[/+] on Jul 7, 2023
  2. auvred commented on Jul 7, 2023

    @auvred
    Contributor

    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

  3. timotheeguerin commented on Jul 7, 2023

    @timotheeguerin
    Author

    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.

  4. locked as resolved and limited conversation to collaborators on Nov 23, 2023
  5. added theissue type on Mar 3, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    area:type definitionIssues with *d.ts fileslocked-due-to-inactivityPlease open a new issue and fill out the template instead of commenting.

    Type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions