Skip to content

TS 2.4.1: ReadonlyArray<Identifiable> not assignable to type ReadonlyArray<T> even though <T extends Identifiable> #16805

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
mmc41 opened this issue Jun 28, 2017 · 4 comments
Labels
Duplicate An existing issue was already created

Comments

@mmc41
Copy link

mmc41 commented Jun 28, 2017

TypeScript Version: 2.4.1

Code

export interface Identifiable {
 id: string;
}

export function test<T extends Identifiable>(): ReadonlyArray<T> {
    const result: ReadonlyArray<Identifiable> = [];
    return result;
}

Expected behavior:
Should compile ok.

Actual behavior:

Error: 'Type 'ReadonlyArray<Identifiable>' is not assignable to type 'ReadonlyArray<T>'.
  Type 'Identifiable' is not assignable to type 'T'.
```'

@kitsonk
Copy link
Contributor

kitsonk commented Jun 28, 2017

export interface Identifiable {
 id: string;
}

export function test<T extends Identifiable>(): ReadonlyArray<T> {
    const result: ReadonlyArray<T> = [];
    return result;
}

2.4 is correct, Identifiable is not assignable to T. Previously the compiler ignored these types of errors. Now it correctly flags them. See #16368 and #15104.

@mmc41
Copy link
Author

mmc41 commented Jun 28, 2017

@kitsonk Thanks for info. Then the Q is, how to change the signature of test method above to work returning an readonly array of either Identifiables or subclasses of Identifiables ?

@mmc41
Copy link
Author

mmc41 commented Jun 28, 2017

Ok. Got it myself:

export function test<T extends Identifiable>(): ReadonlyArray<T> {
    const result: ReadonlyArray<T> = [];
    return result;
}

@mmc41 mmc41 closed this as completed Jun 28, 2017
@kitsonk
Copy link
Contributor

kitsonk commented Jun 28, 2017

For the record, I did exactly that in the example I provided back... 😁

@RyanCavanaugh RyanCavanaugh added the Duplicate An existing issue was already created label Jun 29, 2017
@microsoft microsoft locked and limited conversation to collaborators Jun 14, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Duplicate An existing issue was already created
Projects
None yet
Development

No branches or pull requests

3 participants