2.0.0 โข Published 5 months ago
path-typegen v2.0.0
path-typegen
path-typegen
is a library that converts file paths to TypeScript types
๐ฆ Installation
npm i path-typegen
๐ Usage
๐ฐ ph
Provides flexible path type processing using method chaining with map
and filter
.
Basic
// ๐ฆ 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'
Processing
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
}
}
Configuration
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'
๐ป CLI
Options
- -h, --help โ Prints help information
- -V, --version โ Prints version information
Command
init
command
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
generate
command
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
2.0.0
5 months ago
2.0.0-alpha.3
5 months ago
2.0.0-alpha.2
5 months ago
2.0.0-alpha.1
5 months ago
2.0.0-alpha.0
5 months ago
1.2.0
7 months ago
1.1.0
8 months ago
1.0.1
8 months ago
1.0.0
8 months ago