-
Notifications
You must be signed in to change notification settings - Fork 12.7k
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
Array filter() by element constructor returns never[] #58987
Comments
Changed in |
The return type of filter callback |
This comment was marked as resolved.
This comment was marked as resolved.
Related #16035 |
Oops, typo and/or me being in too much of a rush. Fixed now. |
Yes, I agree this works and is more idiomatic. I will change this in my code to fix the problem. I wanted to report it in case other projects unexpectedly break while trying to do a minor bump. I'm not sure if 5.5.2 fixes a long standing bug or not, but regardless it was a surprise to have a broken build after this minor upgrade. Thanks for your work on it π |
@fatcerberus Actually the class A {
a!: number;
}
class B {
b!: number;
}
function fn(input: A | B) {
switch (input.constructor) {
case A:
input.a // <- not work
}
switch (true) {
case input.constructor === A:
input.a; // work!?
}
} But anyway, in the OP's case the narrowing target is not a union type, and |
Seems to me the real issue is this: class GenericClass {
constructor(public value: string) { }
}
class ConcreteClass extends GenericClass {
constructor(value: string) {
super(`Concrete: ${value}`);
}
}
function foo(obj: GenericClass) {
if (obj.constructor === ConcreteClass) {
obj; // never, Wat?
}
} It's obviously possible for |
It goes all the way back to #32774 that implemented "constructor type guards". There is no logic to deal with derived classes. |
This issue has been marked as "Design Limitation" and has seen no recent activity. It has been automatically closed for house-keeping purposes. |
π Search Terms
array never filter
π Version & Regression Information
β― Playground Link
https://www.typescriptlang.org/play/?ts=5.5.2#code/IYIwzgLgTsDGEAJYBthjAg4gUwHbagEtYBhVdBAbwCgEEAHKAewm3mwBMkndIoBXeEygAKevxDJiCAG7Bk-bAC4EfQrgDmASioBfavuoo0GEj1hRsrMiYTYAHq1wcMOfEVLkMNOrB59BCGEROQVlVWh1bSpaOlV+egIRAAMzXAsrcIASSlDFXWStAG5Y-UM-XkRsZGwAWzwIMBU3AmIbdABtAF0EAF4EDvwAdwQ0jOsvEQByYCmtLpKjf0QAM0JkVksOAFEa+txGvrs9hrAAOjWNpJFquoadXoA+Y7uDs4qAoSg+3v6xywmJmK1Gol02nF2r0aHQADF0znlsGcggBlSKaETAoA
π» Code
π Actual behavior
The return type from the
.filter()
expression isnever[]
π Expected behavior
The return type from the
.filter()
expression isGenericClass[]
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.
The text was updated successfully, but these errors were encountered: