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

Filter array from multiple to single type #8123

Closed
OliverJAsh opened this issue Apr 16, 2016 · 1 comment
Closed

Filter array from multiple to single type #8123

OliverJAsh opened this issue Apr 16, 2016 · 1 comment
Labels
Duplicate An existing issue was already created

Comments

@OliverJAsh
Copy link
Contributor

OliverJAsh commented Apr 16, 2016

class Element {
    constructor() {}
}

class Image extends Element {
    constructor(public src: string) { super() }
}

const elements: Element[] = [new Element(), new Image('foo')]

const imageEls: Image[] = elements.filter(e => e instanceof Image)
imageEls

This results in:

main.ts(39,7): error TS2322: Type 'Element[]' is not assignable to type 'Image[]'.
  Type 'Element' is not assignable to type 'Image'.
    Property 'src' is missing in type 'Element'.

Is TypeScript able to infer that the filtered elements array now only contains Images?

@DanielRosenwasser
Copy link
Member

Hey @OliverJAsh, check out #7657.

@DanielRosenwasser DanielRosenwasser added the Duplicate An existing issue was already created label Apr 17, 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
Duplicate An existing issue was already created
Projects
None yet
Development

No branches or pull requests

2 participants