Skip to content
This repository has been archived by the owner on Nov 11, 2024. It is now read-only.
/ path-types Public archive

TypeScript utility types for URL pathnames

Notifications You must be signed in to change notification settings

alloc/path-types

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

6 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

@alloc/path-types

A simple package for type-level manipulation of route paths (i.e. /foo/:bar)

Usage

The two types included are InferParams and PathTemplate.

The InferParams type expects a route path string literal. It tells the type checker how to parse route paths in order to extract “route parameter names” (the :bar part in /foo/:bar) from them. It returns an object type whose keys are the route parameters. By default, the property values are a string type, but this can be customized with the 2nd type parameter.

type Params = InferParams<'/foo/:bar'>
// => { bar: string }

The PathTemplate type also expects a route path string literal. It returns a template string type where the route parameters are replaced with ${string}, so the type is useful for pathname construction.

type Pathname = PathTemplate<'/foo/:bar'>
// => `/foo/${string}`

About

TypeScript utility types for URL pathnames

Resources

Stars

Watchers

Forks