Skip to content

Default to ReadonlyArray #32467

Closed
Closed
@waterplea

Description

@waterplea

Search Terms

ReadonlyArray, default

Suggestion

By default type for array is inferred as Array. It would be better to default to a more strict ReadonlyArray and if programmer needs mutability — he would explicitly state so. Could be a tsconfig option to not introduce a breaking change.

Use Cases

readonly value = [0, 1, 2, 3]; // <-- type number[] here

Examples

readonly value = [0, 1, 2, 3];
readonly mutableValue: Array<number> =  [0, 1, 2, 3];

Checklist

My suggestion meets these guidelines:

  • [✓] This wouldn't be a breaking change in existing TypeScript/JavaScript code
  • [✓] This wouldn't change the runtime behavior of existing JavaScript code
  • [✓] This could be implemented without emitting different JS based on the types of the expressions
  • [✓] This isn't a runtime feature (e.g. library functionality, non-ECMAScript syntax with JavaScript output, etc.)
  • [✓] This feature would agree with the rest of TypeScript's Design Goals.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Awaiting More FeedbackThis means we'd like to hear from more people who would be helped by this featureSuggestionAn idea for TypeScript

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions