Search code examples
typescripteslintprettier

Prettier format Array<T> to T[]


I have a project with react typescript and I'm using prettier to format the code In TS files I have some interfaces with Array<T> and I would like to format to T[]

How can I do this?


Solution

  • This is outside the scope of Prettier, and more in the realm of eslint.

    In fact @typescript-eslint/eslint-plugin has a rule for this already named array-type. See: https://typescript-eslint.io/rules/array-type/.

    Which labels this as incorrect:

    const x: Array<string> = ['a', 'b'];
    const y: ReadonlyArray<string> = ['a', 'b'];
    

    And this as correct:

    const x: string[] = ['a', 'b'];
    const y: readonly string[] = ['a', 'b'];