Skip to content

Array filter/map predicate not type checking properly #12498

Description

TS 2.0.3

See the comments and last line in the code below for expected/actual.

{
    type Foo = { foo: true };
    const isFoo = (foo: Foo) => foo.foo;

    const xs = [{ foo: true }, undefined];

    isFoo(xs[1]) // good, errors
    const x = undefined as Foo | undefined;
    isFoo(x) // good, errors
    xs.filter(isFoo) // bad, didn't fail, expected filter arg to fail, should be Foo | undefined
}

Activity

  1. aluanhaddad commented on Nov 25, 2016

    @aluanhaddad
    Contributor

    This is expected. Consider

    declare function f(x: number): any;
    declare function g(x?: number): any;
    declare function h(x: number | undefined): any;
    let i = f;
    i = g;
    i = f;
    i = h;
    let k = f;
    k = h;
    k = g;

    Also, see this section of the handbook. type-compatibility

  2. OliverJAsh commented on Nov 25, 2016

    @OliverJAsh
    ContributorAuthor

    In the example above, I would receive a type error at runtime. Is there any way to enforce this at compile time?

  3. locked and limited conversation to collaborators on Jun 19, 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

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions