Skip to content

readonly mapped type modifiers for Map / ReadonlyMap #30633

Open

Description

Search Terms

ReadonlyMap
ReadWrite
Writable

Suggestion

3.4.0-rc introduced the ability to use readonly mapped type modifiers with arrays.

(https://devblogs.microsoft.com/typescript/announcing-typescript-3-4-rc/)

It would be useful if the same syntax worked on

Use Cases

This is useful in any library dealing with immutable versions of types. It would allow mapped types to easily convert between Map and ReadonlyMap.

Examples

type ReadWrite<T> = { -readonly [P in keyof T]: T[P] };
const map: ReadWrite<ReadonlyMap<string, string>> = new Map();

map.put('a', 'c'); // this should work, since readonly has been removed.
type Readonly<T> = { readonly [P in keyof T]: T[P] };
const map: Readonly<Map<string, string>> = new Map();

map.put('a', 'c'); // this should not work, since readonly has been applied

Similar examples using Array instead of Map didn't work before 3.4 and did work afterwards.

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.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Assignees

No one assigned

    Labels

    In DiscussionNot yet reached consensusSuggestionAn idea for TypeScript

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions