You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Reacted by dqn, Emmanuel Haguet, Nils Eriksson, evanlc, Yoann Lagache, Grzegorz Rozdzialik, Olzhas Seitmagambet, Naoto Ikuno, mistlog, yizhiyuyou and 21 more
@zhaoyao91any is a set of types that includes every single type. So if you add the type 1 to all the types, it's still all the types.
You can view any as a mathematical Infinity. Infinity + 1 is still Infinity.
Reacted by Konstantinos Stratis and Peter Riesz
Reacted by Cristopher, estebannn and Konstantinos Stratis
The way I like to see it is type ranges (like ranges in math)
any is Infinity range of all possible types. & operator narrows down the range, from wider to narrower.
Imagine number & 1: number is very wide range of possible types, and 1 is a subset of that range. When we apply & operator - the resulting range narrows down to the intersection of these two ranges of types, which becomes 1.
But in the solution above we check whether 0 type range extends 1 & T, which should never happen, because narrowing down to 1 makes no possible intersection with 0. These are sibling ranges.
But in Typescript any intersecting with <your_type> still returns any, which I dont understand, since we are trying to get intersection here, and Infinity intersecting with not-infinite range should get us that range
Activity
zhaoyao91 commentedon Dec 10, 2022
This is the most elegant solution. Though logically I haven't figured out why
1 & anyshould beanyJesusTheHun commentedon Jun 13, 2023
@zhaoyao91
anyis a set of types that includes every single type. So if you add the type1toall the types, it's stillall the types.You can view
anyas a mathematicalInfinity.Infinity + 1is stillInfinity.psxcode commentedon Apr 5, 2024
The way I like to see it is type ranges (like ranges in math)
anyis Infinity range of all possible types.&operator narrows down the range, from wider to narrower.Imagine
number & 1:numberis very wide range of possible types, and1is a subset of that range. When we apply&operator - the resulting range narrows down to the intersection of these two ranges of types, which becomes1.But in the solution above we check whether
0type range extends1 & T, which should never happen, because narrowing down to1makes no possible intersection with0. These are sibling ranges.But in Typescript
anyintersecting with<your_type>still returnsany, which I dont understand, since we are trying to get intersection here, and Infinity intersecting with not-infinite range should get us that rangeGravity2333 commentedon Aug 21, 2025
666