Giter Site home page Giter Site logo

Comments (4)

trxcllnt avatar trxcllnt commented on May 18, 2024

@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.

from ixjs.

OliverJAsh avatar OliverJAsh commented on May 18, 2024

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

from ixjs.

trxcllnt avatar trxcllnt commented on May 18, 2024

@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.

trxcllnt avatar trxcllnt commented on May 18, 2024

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

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.