π Search Terms
filter is type narrow negative
π Version & Regression Information
- This starts on version 5.5
β― Playground Link
https://www.typescriptlang.org/play/?ts=5.5.2#code/C4TwDgpgBAggdgSwLYEMA2UC8UDeAoKKAIwCcIVgALALimBIFcIAaPAXwG489RIoAlAPYBjANZZcBYmQo0oAM3QBnFuy49w0AMqCkEKgjgBzCfGTooAHwEjR6+QzjDgCQXCgIlZ1GgAUS3X1KQyNaHT0DYwBKWgCI4OMPJVhEH0lCMmAGEnc4oJCAOlJyKnZuYTclYDoIKoBGMMDIk2x8QmLZWnomVjZyyurgWuAAJkb4kIk26RK5RTQVXvUKuCq6YKUASSUAeXFsAG0h+uYaqpGAXQL5BDQhkl9qzAA+JO90R6iorgB6H8IAYQAHoAfhS5jQBwu-VWgw22wA6iQ3C0oEdhnVTsdRlcbncIA8nq8AISed5+YBfX7-QHAsG+clWGxiKJQvBAA
π» 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.
π Search Terms
filter is type narrow negative
π Version & Regression Information
β― Playground Link
https://www.typescriptlang.org/play/?ts=5.5.2#code/C4TwDgpgBAggdgSwLYEMA2UC8UDeAoKKAIwCcIVgALALimBIFcIAaPAXwG489RIoAlAPYBjANZZcBYmQo0oAM3QBnFuy49w0AMqCkEKgjgBzCfGTooAHwEjR6+QzjDgCQXCgIlZ1GgAUS3X1KQyNaHT0DYwBKWgCI4OMPJVhEH0lCMmAGEnc4oJCAOlJyKnZuYTclYDoIKoBGMMDIk2x8QmLZWnomVjZyyurgWuAAJkb4kIk26RK5RTQVXvUKuCq6YKUASSUAeXFsAG0h+uYaqpGAXQL5BDQhkl9qzAA+JO90R6iorgB6H8IAYQAHoAfhS5jQBwu-VWgw22wA6iQ3C0oEdhnVTsdRlcbncIA8nq8AISed5+YBfX7-QHAsG+clWGxiKJQvBAA
π» Code
π Actual behavior
π Expected behavior
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.