Skip to content

Support readonly types with inference #32812

Closed
@orta

Description

@orta

Search Terms

readonly without type

Suggestion

Today, in order to use a readonly type with inference you need to know typeof exists

const user = {
  name: "orta"
}

const readonlyUsers: readonly typeof user[] = [user] 

It would be nice to support shorthand syntax for this:

const user = {
  name: "orta"
}

const readonlyUsers: readonly = [user] 

Where the lack of a definition outside of readonly implies you want to keep the inferred type.

Use Cases

Making it cheaper syntactically to use readonly more often without using slightly more esoteric typescript features.

Could be negated by these though:

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

    Needs ProposalThis issue needs a plan that clarifies the finer details of how it could be implemented.SuggestionAn idea for TypeScript

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions