Skip to content

Support ReadonlyArray #77

@niloc132

Description

@niloc132

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions