Skip to content

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

@timotheeguerin

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

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