Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

2.4: Readonly<Map<k,v>> vs. ReadonlyMap<k,v> #16840

Open
BurtHarris opened this issue Jun 29, 2017 · 9 comments
Open

2.4: Readonly<Map<k,v>> vs. ReadonlyMap<k,v> #16840

BurtHarris opened this issue Jun 29, 2017 · 9 comments
Labels
Bug A bug in TypeScript Domain: lib.d.ts The issue relates to the different libraries shipped with TypeScript Help Wanted You can do this
Milestone

Comments

@BurtHarris
Copy link

TypeScript Version: 2.4.1

Code

function GenMap() : ReadonlyMap<string, number> {
    const theMap = new Map<string, number>();
    theMap.set("one", 1);
    theMap.set("two", 2);
    return Object.freeze(theMap);
}

Expected behavior:
Compiles without error in tsc@<2.4.0

Actual behavior:

TryX.ts(5,5): error TS2322: Type 'Readonly<Map<string, number>>' is not assignable to type 'ReadonlyMap<s
tring, number>'.
  Property '[Symbol.iterator]' is missing in type 'Readonly<Map<string, number>>'.
@sylvanaar
Copy link

You cannot make a Map read only with Object.freeze. Sounds like this is working as intended to me.

@BurtHarris
Copy link
Author

That's surprising. @sylvanaar can you provide a reference supporting this?

No matter what the runtime-behavior, Its is a change in compile-time behavior from TypeScript 2.3.x.,

@sylvanaar
Copy link

sylvanaar commented Jun 30, 2017

https://stackoverflow.com/questions/35747325/is-there-a-way-to-freeze-a-es6-map

Also, you can test it yourself: http://jsbin.com/zorumaceyu/edit?js,console

I think they gave a reasonable type for a Map returned from Object.freeze in the current release Readonly<Map>

#12377 they added support for ReadOnlyArray, buit shouldn't have.

@mhegazy
Copy link
Contributor

mhegazy commented Aug 28, 2017

We need new overloads to freeze,:

    freeze<K,V>(a: Map<K,V>): ReadonlyMap<K,V>;

and set as well since we are at it:

    freeze<T>(a: Set<T>): ReadonlySet<T>;

@mhegazy mhegazy added Bug A bug in TypeScript Help Wanted You can do this Domain: lib.d.ts The issue relates to the different libraries shipped with TypeScript labels Aug 28, 2017
@mhegazy mhegazy added this to the Community milestone Aug 28, 2017
@mhegazy
Copy link
Contributor

mhegazy commented Aug 28, 2017

PRs welcomed

@NaridaL
Copy link
Contributor

NaridaL commented Sep 18, 2017

@mhegazy , as @sylvanaar 's comment/link shows, calling Object.freeze on a map doesn't actually stop new keys being added with .set(). Do the suggested overloads still make sense in this case?

@ThomasdenH
Copy link
Contributor

Readonly<Map<_, _>> is not the same as ReadonlyMap<_, _>. I think the latter is a subset of the former, so the best solution would be to make Readonly<Map<_, _>> assignable to ReadonlyMap<_, _>.

@RyanCavanaugh RyanCavanaugh modified the milestones: Community, Backlog Mar 7, 2019
@marcin-serwin
Copy link
Contributor

This issue is caused by the fact that keyof does not include well known symbols (in this case Symbol.iterator). This problem has been already reported in issue #24622 and will be resolved with #24738.

@kevinbarabash
Copy link

kevinbarabash commented Nov 3, 2022

Interestingly ReadonlyArray<T> and Readonly<Array<T> are assignable to each other. I wish that Readonly<Foo> would alias to ReadonlyFoo as long as it's in scope. Ideally, we should also check that Foo is assignable to ReadonlyFoo.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug A bug in TypeScript Domain: lib.d.ts The issue relates to the different libraries shipped with TypeScript Help Wanted You can do this
Projects
None yet
Development

No branches or pull requests

8 participants