-
Notifications
You must be signed in to change notification settings - Fork 12.9k
Closed
Labels
Help WantedYou can do thisYou can do thisPossible ImprovementThe current behavior isn't wrong, but it's possible to see that it might be better in some casesThe current behavior isn't wrong, but it's possible to see that it might be better in some cases
Milestone
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.
jcalz
Metadata
Metadata
Assignees
Labels
Help WantedYou can do thisYou can do thisPossible ImprovementThe current behavior isn't wrong, but it's possible to see that it might be better in some casesThe current behavior isn't wrong, but it's possible to see that it might be better in some cases