You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
readonly mapped type modifiers for Map / ReadonlyMap #30633
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 would actually be a breaking change if someone is using a type like Readonly<Map<Foo, Bar>>, because right now methods like Map.prototype.put exist on that type, but after this change they would not.
Also, instead of just adding a special case for Map, why not do this in general?
classSomeClass{publicreadonlyreadonlyField: any;publicmutableField: any;publicgetproperty(): any{}publicsetproperty(value: any){}publicsetpropertyOnlySet(value: any){}publicreadonlyreadonlyMethod(){}publicmutationMethod(){}}// Readonly<SomeClass> will be the same as:classSomeClass{publicreadonlyreadonlyField: any;publicreadonlymutableField: any;publicgetproperty(): any{}publicreadonlyreadonlyMethod(){}}
That would be incredibly useful, though it would benefit from a new error checking that readonly methods don't mutate class state.
classSomeClass{publicmutableField: any;publicsetproperty(value: any){}publicmutationMethod(){}publicreadonlyreadonlyMethod(){this.mutableField=0;// Error: Cannot mutate a property inside a readonly methodthis.property=0;// Error: Cannot mutate a property inside a readonly methodthis.mutationMethod();// Error: Cannot call a non-readonly instance method from inside a readonly method}}
Hmm... you are correct that it has the potential to break existing code. However, it seems like the same argument would apply to the following change, which made similar changes to ReadonlyArray:
Activity
JakeTunaley commentedon Mar 28, 2019
This would actually be a breaking change if someone is using a type like
Readonly<Map<Foo, Bar>>, because right now methods likeMap.prototype.putexist on that type, but after this change they would not.Also, instead of just adding a special case for
Map, why not do this in general?That would be incredibly useful, though it would benefit from a new error checking that readonly methods don't mutate class state.
bjchambers commentedon Mar 28, 2019
Hmm... you are correct that it has the potential to break existing code. However, it seems like the same argument would apply to the following change, which made similar changes to
ReadonlyArray:#29435