Skip to content

.tip

· one of the small things

Type your routes with template literal types

A function that takes a string route will happily accept /blgo. A template literal type only accepts the shapes you declared.

type Route = `/blog/${string}` | `/tips/${string}` | "/" | "/blog";
function push(route: Route) { ... }

It isn't a router, and it doesn't validate anything at runtime. It stops the typo at the call site, which is where typos happen.

typescript