mihaifm / linq

linq.js - LINQ for JavaScript

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Support User-Defined Type Guards in where()

kasugaiasuka opened this issue · comments

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...