Skip to content

223 - IsAny #232

Open
Open

Description

@B2D1
type IsAny<T> = 0 extends (1 & T) ? true : false;

Activity

  1. added
    answer
    en
    on Sep 20, 2020
  2. zhaoyao91 commented on Dec 10, 2022

    @zhaoyao91

    This is the most elegant solution. Though logically I haven't figured out why 1 & any should be any

  3. JesusTheHun commented on Jun 13, 2023

    @JesusTheHun
    Contributor

    @zhaoyao91 any 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.

  4. psxcode commented on Apr 5, 2024

    @psxcode

    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

  5. Gravity2333 commented on Aug 21, 2025

    @Gravity2333

    666

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    223answeren

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions