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
Based on https://www.typescriptlang.org/docs/handbook/release-notes/typescript-3-4.html#a-new-syntax-for-readonlyarray, it looks like readonly Foo[] is syntactic sugar for ReadonlyArray. It looks like this is effectively an array that just can't have new values assigned/appended, but the elements themselves are still mutable - so typescript wants to be sure that a readonly array is never passed to code that is expecting to be able to mutate that array.
A mutable array can always be assigned to an immutable array, but the converse is not true. It seems like by this principle, any method argument of type array should probably be declared as readonly unless the method is expected to write to it?
My best guess here is that we could introduce a @TsReadonly annotation to add the keyword readonly to parameters. Creating it as a target=TYPE_USE would let this be allowed on return type and generic args as well.
The text was updated successfully, but these errors were encountered:
I did a quick check,made a quick implementation for method parameters, and figured that readonly is only allowed for array types, so maybe we should error out or emit a warning when the annotation is added to none array types.
Based on https://www.typescriptlang.org/docs/handbook/release-notes/typescript-3-4.html#a-new-syntax-for-readonlyarray, it looks like readonly Foo[] is syntactic sugar for ReadonlyArray. It looks like this is effectively an array that just can't have new values assigned/appended, but the elements themselves are still mutable - so typescript wants to be sure that a readonly array is never passed to code that is expecting to be able to mutate that array.
A mutable array can always be assigned to an immutable array, but the converse is not true. It seems like by this principle, any method argument of type array should probably be declared as readonly unless the method is expected to write to it?
My best guess here is that we could introduce a
@TsReadonly
annotation to add the keywordreadonly
to parameters. Creating it as a target=TYPE_USE would let this be allowed on return type and generic args as well.The text was updated successfully, but these errors were encountered: