Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support ReadonlyArray #77

Open
niloc132 opened this issue May 9, 2023 · 1 comment
Open

Support ReadonlyArray #77

niloc132 opened this issue May 9, 2023 · 1 comment
Milestone

Comments

@niloc132
Copy link
Member

niloc132 commented May 9, 2023

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.

@niloc132 niloc132 added this to the 1.1 milestone May 9, 2023
@vegegoku
Copy link
Member

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants