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

Array filter/map predicate not type checking properly #12498

Closed
OliverJAsh opened this issue Nov 25, 2016 · 3 comments
Closed

Array filter/map predicate not type checking properly #12498

OliverJAsh opened this issue Nov 25, 2016 · 3 comments
Labels
Duplicate An existing issue was already created Fixed A PR has been merged for this issue

Comments

@OliverJAsh
Copy link
Contributor

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
}
@aluanhaddad
Copy link
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

@OliverJAsh
Copy link
Contributor Author

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

@RyanCavanaugh RyanCavanaugh added the Duplicate An existing issue was already created label Nov 28, 2016
@RyanCavanaugh
Copy link
Member

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Duplicate An existing issue was already created Fixed A PR has been merged for this issue
Projects
None yet
Development

No branches or pull requests

4 participants