#10969Medium

Integer

Please complete type `Integer<T>`, type `T` inherits from `number`, if `T` is an integer return it, otherwise return `never`. Master advanced TypeScript type manipulation in this medium-level challenge on TypeScriptPro.

In this medium-level challenge, you'll implement an Integer<T> type that checks whether a numeric type T is an integer, returning T if it is and never otherwise.

Challenge Instructions: Integer

Medium

Please complete type Integer<T>, type T inherits from number, if T is an integer return it, otherwise return never.

Change the following code to make the test cases pass (no type check errors).

Loading...

Detailed Explanation

type Integer<T extends number> =
  number extends T
    ? never
    : `${T}` extends `${bigint}`
      ? T
      : never;

How it works:

An alternative approach uses explicit decimal point detection:

type Integer<T extends number> =
  number extends T
    ? never
    : `${T}` extends `${string}.${string}`
      ? never
      : T;

This alternative checks if the string representation contains a decimal point, but note it would also accept scientific notation numbers like 3e23 as integers. The bigint approach correctly rejects these since "3e+23" does not match bigint.

This challenge helps you understand template literal types and type narrowing with numeric types, and how to apply these techniques in real-world scenarios.

This challenge is originally from here.

Share this challenge

Join early, learn faster.

Be the first to access the course, unlock exclusive launch bonuses, and get special early-bird pricing before anyone else.

No spam, unsubscribe at any time. We respect your privacy.

Limited Availability

Only 27 Spots left

Early Access

Get 1 month early access

>75% Off

Pre-Launch discount