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

Support User-Defined Type Guards in where() #78

Open
kasugaiasuka opened this issue May 21, 2020 · 0 comments
Open

Support User-Defined Type Guards in where() #78

kasugaiasuka opened this issue May 21, 2020 · 0 comments

Comments

@kasugaiasuka
Copy link

kasugaiasuka commented May 21, 2020

In forEach(), x is string because filter() respects User-Defined Type Guards and returns string[].

const values = ["a", null, "b", null, "c"];

values
    .filter((value): value is string => typeof value === "string")
    .forEach((x) => { console.log(x.toUpperCase()); });

This makes a error because where() returns IEnumerable<string | null> in spite of the enumerable does not contain null.

const values = ["a", null, "b", null, "c"];

Enumerable.from(values)
    .where((value): value is string => typeof value === "string")
    .forEach((x) => { console.log(x.toUpperCase()); });
                                  ~~~~~~~~~~~~~~~

In lib.es5.d.ts, filter() is defined like this.

filter<S extends T>(callbackfn: (value: T, index: number, array: T[]) => value is S, thisArg?: any): S[];

So we should add an definition to make it better.

where<S extends T>(predicate: (element: T, index: number) => element is S): IEnumerable<S>;

I don't know S is suitable for linq.d.ts or not...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant