-
Notifications
You must be signed in to change notification settings - Fork 12.6k
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/Observable filter does not infer chained type #19640
Comments
Looks like a duplicate of #5101. function mapDefined<T, U>(iter: Iterable<T>, mapFn: (x: T) => U | undefined): U[] {
const out: U[] = [];
for (const x of iter) {
const res = mapFn(x);
if (res) {
out.push(res);
}
}
return out;
}
mapDefined([true, true, false, null], thing => thing === null ? undefined : thing.toString()); EDIT: The |
I noticed this by a different case actually, probably should have posted that one. ['foo', 'bar', null]
.filter(Boolean)
.map(s => s.toUpperCase()); This worked with TS ~2.3 and is not working anymore with TS ~2.6. I thought the above would be a simpler reproduction but it seems it is something completely different :) Thanks for your hint though, this helps but still required quite a few new guards in our codebase which worked in a previous TS version 😕 |
I just tried that with |
You're right doesn't work on arrays, we do have some rxjs observables where this works though, no idea what exactly is different there or if it's project specific.. I will report back later this week. |
Automatically closing this issue for housekeeping purposes. The issue labels indicate that it is unactionable at the moment or has already been addressed. |
A user-defined type guard is not inferred automatically, a type annotation is expected. i.e.: const list: (string | null)[] = ['A', null, 'C']
function isNotNull<T>(a: T | null): a is T { return a !== null; }
list.filter(isNotNull).map(x => x.) |
It is good to hear, but still complicated, not clear whey it not possible to have auto inference here. |
@whitecolor See #5101 |
TypeScript Version: 2.6.1 using
strict: true
Code
Expected behavior:
Compiles
Actual behavior:
The text was updated successfully, but these errors were encountered: