Skip to content

Type definition of Array.prototype.filter is not fully correct #23990

Closed
@yenshih

Description

@yenshih

TypeScript Version: 2.8.3

Code

interface Obj {
}
const obj: Obj = {};
const xs: boolean[] = [false, true, true];
const ys: Array<Obj | null> = [null, obj, obj];


function identity(): undefined;
function identity<T>(x: T): T
function identity(...args: any[]): any {
    if (args.length) {
        return args[0];
    }
}

/**
 * Or import {identity} from 'lodash/fp';
 * 
 * Type definition of LodashIdentity is
 * 
 * interface LodashIdentity {
 *     (): undefined;
 *     <T>(value: T): T;
 * }
 */

xs.filter(identity).map(x => x /* x is still boolean */);
ys.filter(identity).map(y => y /* y is still Obj | null */);

Expected behavior:

x is inferred as true.
y is inferred as Obj.

Actual behavior:

x is still boolean.
y is still Obj | null.

Playground Link: Reproduce

Metadata

Metadata

Assignees

No one assigned

    Labels

    DuplicateAn existing issue was already created

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions