Skip to content

Array/Observable filter does not infer chained type #19640

Description

@topaxi

TypeScript Version: 2.6.1 using strict: true

Code

[true, true, false, null]
  .filter(thing => thing !== null)
  .map(thing => thing.toString())

Expected behavior:

Compiles

Actual behavior:

3 col 17 error| 2531[QF available]: Object is possibly 'null'.

Activity

  1. topaxi commented on Nov 1, 2017

    @topaxi
    Author

    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 😕

  2. topaxi commented on Nov 1, 2017

    @topaxi
    Author

    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.

  3. typescript-bot commented on Nov 17, 2017

    @typescript-bot
    Contributor

    Automatically closing this issue for housekeeping purposes. The issue labels indicate that it is unactionable at the moment or has already been addressed.

  4. wclr commented on Jan 17, 2018

    @wclr

    Not sure why this not working:

    const list: (string | null)[] = ['A', null, 'C']
    
    list.filter<string>((_) => _ !== null).map(x => x.)

    image

    Tried latest dev 2.7 still not working and not inferring

  5. mhegazy commented on Jan 17, 2018

    @mhegazy
    Contributor

    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.)
  6. wclr commented on Jan 17, 2018

    @wclr

    A user-defined type guard is not inferred automatically, a type annotation is expected. i.e.:

    It is good to hear, but still complicated, not clear whey it not possible to have auto inference here.

  7. locked and limited conversation to collaborators on Jul 3, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    DuplicateAn existing issue was already createdFixedA PR has been merged for this issue

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions