You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
classFoo{x(): void{}notReadonly=1;}functionfoo1(arg: Readonly<Foo>){arg.x=()=>{};// error ✅arg.notReadonly=2;// error ✅}functionfoo2(arg: readonlystring[]){arg.length=1;// error ✅arg.forEach=()=>{};// no error ❌ - #22315}functionfoo3(arg: Readonly<ReadonlyArray<string[]>>){arg.length=1;// error ✅arg.forEach=()=>{};// no error ❌}interfaceMyArray<T>extendsArray<T>{}functionfoo4(arg: MyArray<string>){arg.length=1;// no error ✅arg.forEach=()=>{};// no error ✅}functionfoo5(arg: Readonly<MyArray<string>>){arg.length=1;// error ✅arg.forEach=()=>{};// error ✅}
Expected behavior: ReadonlyArray<T> and readonly T[] methods should be immutable by default, or should be able to be marked as immutable via Readonly<T>.
Actual behavior:
The typechecker has special handling under the hood which detects an array inside of Readonly<T> converts it to the ReadonlyArray type.
Classes/objects can, however, have their methods marked as readonly via Readonly<Class>.
If you extend the array type (interface MyArray<T> extends Array<T> {}), then it works as expected, because the typechecker no longer thinks the type is an array type (checker.isArrayType(type) === false).
I don't think it's a duplicate, from my understanding of that issue.
This is specifically about being able to mark methods on the array type as readonly via Readonly<T>, so that you cannot reassign the methods.
#35313 is about making Readonly<T>remove methods from object types.
I don't want to remove anything - I just want to be add the readonly modifier to methods via Readonly<T>, which works fine for everything except array types.
TypeScript Version: 3.7.5
Search Terms: array, methods, readonly
Code
Expected behavior:
ReadonlyArray<T>
andreadonly T[]
methods should be immutable by default, or should be able to be marked as immutable viaReadonly<T>
.Actual behavior:
The typechecker has special handling under the hood which detects an array inside of
Readonly<T>
converts it to theReadonlyArray
type.I.e.
More Info:
Readonly<Class>
.interface MyArray<T> extends Array<T> {}
), then it works as expected, because the typechecker no longer thinks the type is an array type (checker.isArrayType(type) === false
).Playground Link:
https://www.typescriptlang.org/play/?ssl=1&ssc=1&pln=30&pc=2#code/MYGwhgzhAEBiD29oG8BQ0PQB4AoCUAXNAG7wCWAJitAL7qYB28ALgEoCmYF8DIAntAC80AIwBuVHVQAzAK4NgzMj2jTEInGABOAcyIcuPfgB4E8AHx4U9DNp0A6LEOj4h56jTGYA9N+jstLXgtaEBQchtoO3smNk5uXgFhACYvX39A4LDJVBl5RWUGVUQkzV0iLTijAQhmLTIGHQBtAF0rNExI3XsQdgbmAAtncQ6R6DSAoJDwjqi1LQBRMGBB4VdBd2RaVL8mdMnoQBlybLkFJRU1eABmUr1oA3iTe6qAQUCwPmMauoaW80trGZdHp9FaiLyjHx+CaZaaYWbBRbLZxrDZbMY7JDQkJHKT1ZgBaRLdjQACyfFeWnexgAKu52Fh8QwKDAKVTadYpCd8udEAAWG5EMmsj5feo6f7tOFA3o6AZDcEQ9HQXZYrKAhxzRGglEebbKzEZKbHPJnQoXACsArulQSxiFbxFtTFfzaESiwNloOGiqVqthti6mqW2qs611vsNWRoQA
Related Issues:
#22315
The text was updated successfully, but these errors were encountered: