#5140Medium

Trunc

Implement the type version of ```Math.trunc```, which takes string or number and returns the integer part of a number by removing any fractional digits. Master advanced TypeScript type manipulation in this medium-level challenge on TypeScriptPro.

In this medium-level challenge, you'll implement a type-level version of Math.trunc that takes a string or number type and returns the integer part by removing any fractional digits.

Challenge Instructions: Trunc

Medium

Implement the type version of Math.trunc, which takes string or number and returns the integer part of a number by removing any fractional digits.

For example:

type A = Trunc<12.34> // 12

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

Loading...

Detailed Explanation

type Trunc<T extends string | number> =
  `${T}` extends `${infer Int}.${string}`
    ? Int extends '' | '-'
      ? `${Int}0`
      : Int
    : `${T}`;

How it works:

This challenge helps you understand template literal type inference and string parsing at the type level, and how to apply these concepts 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