https://github.com/alloc/path-types
TypeScript utility types for URL pathnames
https://github.com/alloc/path-types
Last synced: 9 months ago
JSON representation
TypeScript utility types for URL pathnames
- Host: GitHub
- URL: https://github.com/alloc/path-types
- Owner: alloc
- Archived: true
- Created: 2024-10-04T15:14:22.000Z (over 1 year ago)
- Default Branch: master
- Last Pushed: 2024-10-09T18:05:55.000Z (over 1 year ago)
- Last Synced: 2025-04-24T03:37:21.462Z (10 months ago)
- Language: JavaScript
- Homepage:
- Size: 2.93 KB
- Stars: 2
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# @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.
```ts
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.
```ts
type Pathname = PathTemplate<'/foo/:bar'>
// => `/foo/${string}`
```