jonaskello / tslint-immutable

TSLint rules to disable mutation in TypeScript.
MIT License
417 stars 14 forks source link

Add support for new `readonly` keyword syntax for arrays and types in typescript 3.4 #129

Closed jonaskello closed 5 years ago

jonaskello commented 5 years ago

It seems typescript 3.4 adds more support for the readonly keyword regarding arrays and tuples :-). See the release notes.

So the readonly-keyword rule probably needs to be updated to support these scenarios:

function foo(arr: readonly string[]) {
    arr.slice();        // okay
    arr.push("hello!"); // error!
}

function foo(pair: readonly [string, string]) {
    console.log(pair[0]);   // okay
    pair[1] = "hello!";     // error
}
RebeccaStevens commented 5 years ago

With regard to the readonly-array rule and automatic fixes for it, should

function foo(arr: string[]) {
}

continue to be fixed to

function foo(arr: ReadonlyArray<string>) {
}

or should it now fix to

function foo(arr: readonly string[]) {
}

?

Same question with

function foo(arr: Array<string>) {
}

I'm feeling the fix for Array<string> should continue to be ReadonlyArray<string> and the fix for string[] should change to readonly string[].

RebeccaStevens commented 5 years ago

For nested arrays, the syntax looks like this:

function foo(arr: readonly (readonly string[])[]) {
    arr[0] = [1];     // error!
    arr[0][0] = 1;    // error!
}
jonaskello commented 5 years ago

I agree on the Array<T> -> ReadonlyArray<T>, and T[] -> readonly T[] fixes. If you have opted for one syntax you probably want to fix to that same syntax. Another way would be to have multiple fix options and let the user choose, but I'm not sure if that is possible.