This solution is generated by ChatGPT, correctness is not guaranteed.

AI solution for TypeScript puzzle on BFE.dev
56. implement Abs<N>

type Abs<N extends number> = N extends `${-infer R}` ? R : N;

Explanation:

The type Abs<N> takes in a number as a type parameter and returns its absolute value as a type.

We use a type conditional (extends) to check if the number is negative. If the number is negative, then it will have a leading - sign. We use template literal types (```) to split the number into two parts - the - sign and the rest of the number.

The infer keyword is used to create a type variable (R) that will capture the rest of the number after the - sign.

Finally, we return the rest of the number (R) if the input number is negative, and return the input number as is if it is positive or zero.

Example usage:

type Positive = Abs<10>; // type Positive = 10
type Negative = Abs<-5>; // type Negative = 5
type Zero = Abs<0>; // type Zero = 0