Skip to content

Can't cast array to array subtype #19873

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
ghost opened this issue Nov 9, 2017 · 2 comments
Closed

Can't cast array to array subtype #19873

ghost opened this issue Nov 9, 2017 · 2 comments
Labels
Working as Intended The behavior described is the intended behavior; this is not a bug

Comments

@ghost
Copy link

ghost commented Nov 9, 2017

TypeScript Version: 2.7.0-dev.20171109

Code

interface I extends Array<number> { x: number }
const x = [] as I;

Expected behavior:

No error.

Actual behavior:

src/a.ts(2,11): error TS2352: Type 'never[]' cannot be converted to type 'I'.
  Property 'x' is missing in type 'never[]'.
@mhegazy
Copy link
Contributor

mhegazy commented Nov 9, 2017

The way type assertion works today is that it checks that at least one of the types is assignable to the other.. that usually means you can move up or down to a more specialized or less specialized type, (I => number[]
or number[] => I) but not side ways .

That said, @ahejlsberg is working on changing the comparable relationship to make it more permissive. by basically allowing all casts, unless we can prove that they are disjoint. that implies not checking non-shared propeortes, e.g. x in your example. so this should be allowed under the new proposal.

@mhegazy mhegazy added the Working as Intended The behavior described is the intended behavior; this is not a bug label Nov 9, 2017
@typescript-bot
Copy link
Collaborator

Automatically closing this issue for housekeeping purposes. The issue labels indicate that it is unactionable at the moment or has already been addressed.

@ghost ghost mentioned this issue Jan 12, 2018
@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
Working as Intended The behavior described is the intended behavior; this is not a bug
Projects
None yet
Development

No branches or pull requests

2 participants