-
Notifications
You must be signed in to change notification settings - Fork 74
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
Narrowed types from user defined type guards are lost in subsequent method calls #162
Comments
@OliverJAsh ah, so I think this is a typescript bug. Check this out: // no error
const arr1: Array<number> = ['foo', 1]
.filter((x): x is number => typeof x === 'number');
/*
[ts]
Type '(string | number)[]' is not assignable to type 'number[]'.
Type 'string | number' is not assignable to type 'number'.
Type 'string' is not assignable to type 'number'.
*/
const arr2: Array<number> = ['foo', 1]
.filter((x): x is number => typeof x === 'number')
.map((x) => x);
// no error
const arr3: Array<number> = ['foo', 1]
.filter((x: any): x is number => typeof x === 'number')
.map((x) => x); It looks like adding an import { Iterable as IterableX } from "ix";
// no error
const itr1: IterableX<number> = IterableX.from(['foo', 1])
.filter((x): x is number => typeof x === 'number');
/*
[ts]
Type 'IterableX<string | number>' is not assignable to type 'IterableX<number>'.
Type 'string | number' is not assignable to type 'number'.
Type 'string' is not assignable to type 'number'.
*/
const itr2: IterableX<number> = IterableX.from(['foo', 1])
.filter((x): x is number => typeof x === 'number')
.map(x => x);
// no error
const itr3: IterableX<number> = IterableX.from(['foo', 1])
.filter((x: any): x is number => typeof x === 'number')
.map(x => x); And here's something else that's strange; the source Array's generic type is preserved, while the source Iterable's is erased: It looks like TS is inferring T from the filter predicate's |
@trxcllnt Good investigation! I opened an issue on TypeScript with regards to this. See microsoft/TypeScript#20186. |
@OliverJAsh also, looks like defining the predicate before chaining also fixes it: function isNumber(x: string | number): x is number {
return typeof x === 'number';
}
// no error
const arr: Array<number> = ['foo', 1].filter(isNumber).map((x) => x);
// no error
const itr: IterableX<number> = IterableX.from(['foo', 1]).filter(isNumber).map(x => x); |
(cross-posting from microsoft/TypeScript#20186) @OliverJAsh based on microsoft/TypeScript#19640 (comment) it looks like this may be fixed by #17600, as this form also works in 2.6.1: // no error
const arr: Array<number> = ['foo', 1]
.filter<number>((x): x is number => typeof x === 'number')
.map((x) => x); |
IxJS version: 2.3.1
Code to reproduce:
Related #44
The text was updated successfully, but these errors were encountered: