Skip to content

Array not assignable to ReadonlyArray with subclass items #10368

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

Closed
stephanedr opened this issue Aug 16, 2016 · 5 comments
Closed

Array not assignable to ReadonlyArray with subclass items #10368

stephanedr opened this issue Aug 16, 2016 · 5 comments
Assignees
Labels
Bug A bug in TypeScript Domain: lib.d.ts The issue relates to the different libraries shipped with TypeScript Fixed A PR has been merged for this issue

Comments

@stephanedr
Copy link

TypeScript Version: 2.0.0

Code

class A {
    a: string;
}
class B extends A {
    b: string;
}
function f(a: ReadonlyArray<A>): void { }
f([new A()]);           // OK
f([new A(), new B()]);  // OK
f([new B()]);           // Error

Expected behavior:
To compile.

Actual behavior:
error TS2345: Argument of type 'B[]' is not assignable to parameter of type 'ReadonlyArray'.
Types of property 'concat' are incompatible.
Type '(...items: (B | B[])[]) => B[]' is not assignable to type '{ <U extends ReadonlyArray>(...items: U[]): A[]; (...items: A[]): A[]; }'.
Types of parameters 'items' and 'items' are incompatible.
Type 'A' is not assignable to type 'B | B[]'.
Type 'A' is not assignable to type 'B[]'.
Property 'length' is missing in type 'A'.

@sandersn
Copy link
Member

This sounds related to the concat fix in #9997. It may have another problem, or it may need to be propagated to ReadonlyArray as well.

Have you tried with typescript@next?

@stephanedr
Copy link
Author

Just checked with 2.1.0-dev.20160816. Same error.

@sandersn sandersn self-assigned this Aug 16, 2016
@sandersn
Copy link
Member

sandersn commented Aug 16, 2016

I copied the improved signatures from Array<T>.concat and it works now. I'll create a PR for the fix.

@sandersn
Copy link
Member

Fix is up at #10374

@RyanCavanaugh RyanCavanaugh added Bug A bug in TypeScript Fixed A PR has been merged for this issue Domain: lib.d.ts The issue relates to the different libraries shipped with TypeScript labels Aug 16, 2016
@RyanCavanaugh RyanCavanaugh added this to the TypeScript 2.0.1 milestone Aug 16, 2016
@stephanedr
Copy link
Author

Confirmed fixed (2.1.0-dev.20160818). Thanks.

@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 Domain: lib.d.ts The issue relates to the different libraries shipped with TypeScript Fixed A PR has been merged for this issue
Projects
None yet
Development

No branches or pull requests

3 participants