TypeScript type that converts string to number

Dec 15, 2023 5 min read

I found a handy way to convert string to number in TypeScript while solving a Advent of Typescript challenge.

While solving a Advent of Typescript challenge, I stumbled upon a problem where I almost solved it but I had to convert a string constant to a number. Initially it seemed impossible to do it in TypeScript but eventually I found a way to do it using type template literals.

Solution

type ToNumber<T> = T extends `${infer U extends number}` ? U : never;

const input = "123" as const;
type Input = typeof input;
//    ^? type Input = "123"
type Output = ToNumber<typeof input>;
//     ^? type Output = 123

To find and solve more TypeScript challenges like this, check out Advent of Typescript.

Made with Astro, Tailwind CSS, and Vercel.