path-typegen
is a library that converts file paths to TypeScript types
npm i path-typegen
Provides flexible path type processing using method chaining with map
and filter
.
// 📦 src/components/
// ┣ 📂Button
// ┃ ┗ 📜index.tsx
// ┣ 📂Card
// ┃ ┗ 📜index.tsx
ph('./src/components', './src/types/components.ts')
.write()
// ./src/types/components.ts
export type PathType = './src/components/Button/index.tsx' | './src/components/Card/index.tsx'
Uses method chaining with map()
and filter()
for path transformations.
- All operations are lazily evaluated until
write()
is called. - Use
typed
tagged template function for object type transformation
// 📦 src/pages/
// ┣ 📜posts/[id].tsx
// ┗ 📜users/[id]/settings.tsx
const paths = ph('./src/pages', './src/types/routes.ts')
.filter(path => path.endsWith('.tsx')) // Lazy evaluation, Iterable is not consumed
.map(path => typed`{
path: ${path},
params: {
id: number,
}
}`) // Lazy evaluation, Iterable is still not consumed
.write(prettier.format) // Iterable is consumed
// ./src/types/routes.ts
export type PathType =
| {
path: './src/pages/posts/[id].tsx',
params: {
id: number
}
}
| {
path: './src/pages/users/[id]/settings.tsx',
params: {
id: number
}
}
ph('./src/components', './src/types/components.ts')
.setConfig({
typeName: 'ComponentPaths',
annotation: '/** @generated This is auto-generated type */\n'
})
.write()
// ./src/types/components.ts
/** @generated This is auto-generated type */
export type ComponentPaths = './src/components/Button/index.tsx' | './src/components/Card/index.tsx'
- -h, --help — Prints help information
- -V, --version — Prints version information
Creates a new configuration file (path-typegen.config.cjs) in your project root with default settings. Types are provided through JSDoc annotations for better DX
path-typegen init
Generates TypeScript types from your input files.
Options:
- -i, --input — Input file path
- -o, --output — Output file path
# Generate TypeScript types
path-typegen generate -i ./assets/images -o ./ImageType.ts