Type Helpers 9 exercises
Problem

Ensure Type Safety in a Type Helper

This AddRoutePrefix type helper that has already been scaffolded for us.

It takes in TRoute then puts it into a string literal that adds a / to the beginning:


type AddRoutePrefix<TRoute> = `/${TRoute}`;

This is passing most of our tests, but not all.

Using `@ts-expect-err

Loading exercise

Transcript

0:00 In this exercise, we have a type helper that's already scaffolded for us, which is an AddRoutePrefix. It takes in a TRoute. TRoutes, we're not using T for some reason. We can use T, but it doesn't matter. We're putting it inside a string literal. Now, this string literal then...What we're expecting from the test is that it's going to add this little character to the start of it.

0:25 That appears to be working for most of these, except we're getting a few errors here. It looks like, in the tests, we're expecting it to error when we pass this type-helper a Boolean. We're also expecting it to error when we pass it a number. That tells me that this TRoute here, we need to constrain it to be of a certain type.

0:44 If we hover over this, then we're going to see type TRoute is not assignable to type string, number, bigint, Boolean, null, or undefined. Your job is to try and work out what's causing that error, and if we can solve this by somehow constraining the type that we get passed in, in order to make sure this error goes away.