Repository navigation
Closed
Labels
DuplicateAn existing issue was already createdAn existing issue was already created
Description
TypeScript Version: master / 2.6.0-dev20170914 / whatever the playground has currently
I searched for immediate type guard and type guard inference , I hope I didn't miss an existing issue.
The issue is that the type inference fails for overloads with type guards such as the one for Array.filter when the type guard is passed as an immediate value. See also #7657 The problem seems to be that TS infers the type of the argument e of the type guard from the type of the callback parameter of filter. In doing so, the return type of type guard is lost. If you specify the argument e of the type guard explicitly, it works as expected.
Code
// for ref the Array signature.
interface Array<T> {
filter<S extends T>(callbackfn: (value: T, index: number, array: T[]) => value is S, thisArg?: any): S[];
}
declare const arr: (string | number)[]
// Property 'substring' does not exist on type 'string | number'.
arr.filter((e): e is string => 'string' == typeof e)[0].substring(0, 10);
arr.filter((e: any): e is string => 'string' == typeof e)[0].substring(0, 10); // ok
const isStringArrow = (e: any): e is string => 'string' == typeof e;
arr.filter(isStringArrow)[0].substring(0, 10); // ok
// Property 'substring' does not exist on type 'string | number'.
arr.filter(function (e): e is string { return 'string' == typeof e })[0].substring(0, 10);
// Property 'substring' does not exist on type 'string | number'.
arr.filter(function (e: any): e is string { return 'string' == typeof e })[0]
.substring(0, 10);
arr.filter(function (this: void, e: any): e is string { return 'string' == typeof e })[0]
.substring(0, 10); // ok
const isString = function (e: any): e is string { return 'string' == typeof e };
arr.filter(isString)[0].substring(0, 10); // okExpected behavior:
All cases should work.
Actual behavior:
See above.
Reacted by Chaz Gatian, Ghet, Alexandre Rieux, Kasidit Iamthong, Amin Pakseresht, Matthew Desjardins, Nicholas Chang, Elizabeth Byerly, Ville Saarinen, Paul LaMotte and 6 more
Metadata
Metadata
Assignees
Labels
DuplicateAn existing issue was already createdAn existing issue was already created
Activity
dgreene1 commentedon Feb 27, 2018
Since #17600 got merged, can we pick this ticket up again?
Please let me know how I could help.
Example of the workaround, which does not scale well:
Many of us are just used to writing this like
!!xP.S. As always, thank you so much for the wonderful work you're all doing. :)
emilio-martinez commentedon May 2, 2018
Andy (Andrewkraft) (@Andy-MS) can this be picked up again? Filter functions under strictNullChecks are a pain to workaround at this point
mhegazy commentedon May 2, 2018
The examples in the OP should all now be working with #5101 fixed.
The examples in #18562 (comment) require the type guards to be automatically inferred, which is tracked by #16069
typescript-bot commentedon May 16, 2018
Automatically closing this issue for housekeeping purposes. The issue labels indicate that it is unactionable at the moment or has already been addressed.