Skip to content
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

Closed
OliverJAsh opened this issue Nov 20, 2017 · 4 comments

Comments

@OliverJAsh
Copy link

IxJS version: 2.3.1

Code to reproduce:

// no error, good!
const xs2: Ix.Iterable<number> = Ix.Iterable
    .from(['foo', 1])
    .filter((x): x is number => typeof x === 'number')

// no error, good!
const xs2b: Ix.Iterable<number> = xs2.map(x => x);

// unexpected error, bad!
const xs3: Ix.Iterable<number> = Ix.Iterable
    .from(['foo', 1])
    .filter((x): x is number => typeof x === 'number')
    .map(x => x)

Related #44

@trxcllnt
Copy link
Member

@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:
screen shot 2017-11-21 at 1 50 00 am
screen shot 2017-11-21 at 1 50 28 am

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.

@OliverJAsh
Copy link
Author

@trxcllnt Good investigation! I opened an issue on TypeScript with regards to this. See microsoft/TypeScript#20186.

@trxcllnt
Copy link
Member

trxcllnt commented Nov 21, 2017

@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);

@trxcllnt
Copy link
Member

(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);

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants