Closed
Description
π Search Terms
filter is type narrow negative
π Version & Regression Information
- This starts on version 5.5
β― Playground Link
π» Code
type Animal = {
breath: true,
};
type Rock = {
breath: false,
};
type Something = Animal | Rock;
function isAnimal(something: Something): something is Animal {
return something.breath
}
const test1: Something = {
breath: true,
}
const test2: Something = {
breath: false,
};
const thisIsOk = [test1, test2].filter(t => isAnimal(t));
// ^? Animal[]
const thisIsWrong = [test1, test2].filter(t => !isAnimal(t));
// ^? (Animal | Rock)[]
π Actual behavior
const thisIsOk = [test1, test2].filter(t => isAnimal(t));
// ^? Animal[]
const thisIsWrong = [test1, test2].filter(t => !isAnimal(t));
// ^? (Animal | Rock)[]
π Expected behavior
const thisIsOk = [test1, test2].filter(t => isAnimal(t));
// ^? Animal[]
const thisIsShouldBe = [test1, test2].filter(t => !isAnimal(t));
// ^? Rock[]
Additional information about the issue
Filtering is working only if "positive", but if the "is" is used as a negative then it don't type narrow.