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

TS2495: Type 'Readonly<any[]>' is not an array type or a string type #12377

Closed
falsandtru opened this issue Nov 19, 2016 · 6 comments
Closed
Assignees
Labels
Bug A bug in TypeScript Fixed A PR has been merged for this issue

Comments

@falsandtru
Copy link
Contributor

TypeScript Version: master

Code

for (const _ of Object.freeze([])) {}

Expected behavior:

pass

Actual behavior:

$ node built/local/tsc.js -t es5 --lib es6 index.ts
index.ts(1,17): error TS2495: Type 'Readonly<any[]>' is not an array type or a string type.
@aluanhaddad
Copy link
Contributor

aluanhaddad commented Nov 19, 2016

This is because [Symbol.iterator] is not included by the property enumerator when mapping the type. The quick fix is to add an overload of freeze in your lib file.

  /**
    * Prevents the modification of existing property attributes and values, 
    * and prevents the addition of new properties.
    * @param o Object on which to lock the attributes.
    */
  freeze<T, V>(o: T & { [Symbol.iterator]: V }): Readonly<T> & { [Symbol.iterator]: V };
  freeze<T>(o: T): Readonly<T>;

@mhegazy mhegazy added Bug A bug in TypeScript Domain: lib.d.ts The issue relates to the different libraries shipped with TypeScript labels Nov 20, 2016
@mhegazy
Copy link
Contributor

mhegazy commented Nov 20, 2016

we should add an overload to freeze for arrays:

    freeze<T>(o: T[]): ReadonlyArray<T>;
    freeze<T>(o: T): Readonly<T>;

@mhegazy mhegazy removed the Domain: lib.d.ts The issue relates to the different libraries shipped with TypeScript label Nov 20, 2016
@aluanhaddad
Copy link
Contributor

@mhegazy would you be open to a PR?

@mhegazy
Copy link
Contributor

mhegazy commented Nov 21, 2016

PRs welcomed

@mhegazy
Copy link
Contributor

mhegazy commented Nov 21, 2016

also we need to handle functions differently.

let handler = Object.freeze(function(callback, context?) { return 0; });
handler(); // Error now, no call signature

@aluanhaddad
Copy link
Contributor

aluanhaddad commented Nov 22, 2016

Yeah there are a bunch of tricky scenarios. Also something that came to mind when special casing ReadonlyArray<T> is that it's not really the same contract expressed by Readonly<T[]>. While it is convenient to conflate them in this specific case, they don't mean the same thing in terms of how they are used. The point you raise about functions is critical and I assume construct signatures are also excluded. It's probably just a matter of special casing all of them but I wondered what the most expressive way to write the types is.

@mhegazy mhegazy added the Fixed A PR has been merged for this issue label Nov 23, 2016
@mhegazy mhegazy self-assigned this Nov 23, 2016
@mhegazy mhegazy added this to the TypeScript 2.1.3 milestone Nov 23, 2016
@microsoft microsoft locked and limited conversation to collaborators Jun 19, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Bug A bug in TypeScript Fixed A PR has been merged for this issue
Projects
None yet
Development

No branches or pull requests

3 participants