Closed
Description
TypeScript Version: 3.3.0-dev.20190119
Search Terms:
filter, type guard, mutually exclusive guard
Code
class MyArray<T> {
constructor(arr: Array<T>) { };
filter<S extends T>(callbackfn: (value: T, index: number, array: ReadonlyArray<T>) => value is S, thisArg?: any): MyArray<S> { return (null as any); };
// Do not include fallback case! This is how MyArray differs from Array's filter type.
// filter(callbackfn: (value: T, index: number, array: ReadonlyArray<T>) => any, thisArg?: any): T[];
}
declare function isBoolean(a: any): a is boolean;
declare const numArr: Array<number>;
const test1: Array<boolean> = numArr.filter(isBoolean);
// Here I'd expect Array<never>
// The actual result is Array<number>
// This is because the type guarded case is falling through completely
declare const myNumArray: MyArray<number>;
const test2: Array<boolean> = myNumArray.filter(isBoolean);
// When there is no such case, the error becomes clear
Expected behavior
When is
guards for incompatible types are assigned to each other, the result should be never
.
Actual behavior
When is
guards for incompatible types are assigned to each other, an error occurs.