Skip to content

Array filter() by element constructor returns never[]Β #58987

Closed as not planned
Closed as not planned
@iknowcss-invenco

Description

@iknowcss-invenco

πŸ”Ž Search Terms

array never filter

πŸ•— Version & Regression Information

  • This changed between versions 5.4.5 and 5.5.2

⏯ Playground Link

https://www.typescriptlang.org/play/?ts=5.5.2#code/IYIwzgLgTsDGEAJYBthjAg4gUwHbagEtYBhVdBAbwCgEEAHKAewm3mwBMkndIoBXeEygAKevxDJiCAG7Bk-bAC4EfQrgDmASioBfavuoo0GEj1hRsrMiYTYAHq1wcMOfEVLkMNOrB59BCGEROQVlVWh1bSpaOlV+egIRAAMzXAsrcIASSlDFXWStAG5Y-UM-XkRsZGwAWzwIMBU3AmIbdABtAF0EAF4EDvwAdwQ0jOsvEQByYCmtLpKjf0QAM0JkVksOAFEa+txGvrs9hrAAOjWNpJFquoadXoA+Y7uDs4qAoSg+3v6xywmJmK1Gol02nF2r0aHQADF0znlsGcggBlSKaETAoA

πŸ’» Code

abstract class GenericClass {
  protected constructor(public value: string) {}
}

class ConcreteClass extends GenericClass {
  constructor(value: string) {
    super(`Concrete: ${value}`);
  }
}

const elements: GenericClass[] = [new ConcreteClass('a')];

const filteredElements = elements.filter((element) => element.constructor === ConcreteClass);

filteredElements[0].value.toString();
//                  ~~~~~ 
// Property 'value' does not exist on type 'never'.

πŸ™ Actual behavior

The return type from the .filter() expression is never[]

πŸ™‚ Expected behavior

The return type from the .filter() expression is GenericClass[]

Additional information about the issue

I searched through the open issues in the days since 5.5.2 were released but I could not find any which matched this case. Apologies if I missed anything. I've tried to reduce this down to the most minimal example that I can.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Design LimitationConstraints of the existing architecture prevent this from being fixed

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions