Comments (4)
@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 any
type annotation to the filter predicate argument fakes typescript out somehow. Doing the same with Iterable yields roughly the same behavior:
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 value: T
argument, rather than this: Iterable<T>
. Changing value: T
to value: any
for the filter type-guard signature fixes this problem, but I worry we'd be defeating the compiler if this were to be fixed in a future release.
from ixjs.
@trxcllnt Good investigation! I opened an issue on TypeScript with regards to this. See microsoft/TypeScript#20186.
from ixjs.
@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);
from ixjs.
(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);
from ixjs.
Related Issues (20)
- Adding a helper module for bringing in everything HOT 3
- What happened to the function signature on reduce (and scan)? HOT 4
- takeUntil should complete iterator immediately
- docs(readme): `Array#extras` link is defunct HOT 2
- Spurious 'unhandledRejection' events HOT 1
- Weird memory behaviour with merge HOT 1
- async timeout operator does not pass through its input HOT 2
- pipe() should work recursively, and the free standing pipe() should work inside the pipe chain method HOT 4
- Merge with iter-tools? HOT 3
- Typescript >= 4.3.2 HOT 1
- [Suggestion]: Update `@types/node`
- vite environment ix package load error
- Usage with Skypack CDN? HOT 2
- ES modules do not work HOT 1
- Build errors with Typescript 4.7.4 HOT 4
- Take while inclusive
- Operator flat concurrency issue with async iterables HOT 7
- from with abort signal HOT 1
- merge function can cause unhandled rejected promise condition
- async iterable merge function that ends upon the first "done"
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from ixjs.