microsoft / TypeScript

TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
https://www.typescriptlang.org
Apache License 2.0
100.57k stars 12.43k forks source link

Codefix: convert `Type[]` to `readonly Type[]` #31923

Open JoshuaKGoldberg opened 5 years ago

JoshuaKGoldberg commented 5 years ago

Search Terms

codefix readonlyarray readonly array immutable

Suggestion

It'd be nice to have a codefix for converting non-readonly arrays to readonly.

Use Cases

When converting/refactoring code to be more immutable/readonly-friendly, it's a bit of a pain to manually add these annotations.

Examples

Given this:

function takesArrays(items: /*| |*/string[], moreItems: /*| |*/string[]) { /* ... */ }

...having a codefix to make one or all parameters readonly would be useful:

function takesArrays(items: readonly string[], moreItems: readonly string[]) { /* ... */ }

Checklist

My suggestion meets these guidelines:

jcalz commented 5 years ago

I agree, especially with the introduction of const assertions (#29510) and their readonly tuples.