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

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

Closed
yenshih opened this issue May 9, 2018 · 3 comments
Closed

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

yenshih opened this issue May 9, 2018 · 3 comments
Labels
Duplicate An existing issue was already created

Comments

@yenshih
Copy link

yenshih commented May 9, 2018

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

@s-ve
Copy link

s-ve commented May 9, 2018

Possibly related : #7657, #2388, #5101, #10734

@yenshih
Copy link
Author

yenshih commented May 9, 2018

@s-ve You're right. Duplicated.

@yenshih yenshih closed this as completed May 9, 2018
@RyanCavanaugh RyanCavanaugh added the Duplicate An existing issue was already created label May 9, 2018
@RyanCavanaugh
Copy link
Member

@yenshih please fill out the entire template (including search terms) for any future issues. Thanks!

@microsoft microsoft locked and limited conversation to collaborators Jul 31, 2018
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
Projects
None yet
Development

No branches or pull requests

3 participants