A CLI tool to convert JSON structures into TypeScript interfaces or classes
# Install globally
npm install -g json-to-ts-cvr
# Or use with npx
npx json-to-ts-cvr [options]
- Convert JSON to TypeScript interfaces or classes
- Support for nested objects and arrays
- Generate proper type definitions (string, number, boolean, etc.)
- Auto-generate interface names from property names
- Customize interface/class names with prefixes
- Handle nullable fields
json-to-ts-cvr '{"name": "John", "age": 25}'
Output:
interface Root {
name: string;
age: number;
}
json-to-ts-cvr -i data.json -o types.ts
json-to-ts-cvr -i data.json --class
Output:
class Root {
name: string;
age: number;
constructor(data: any) {
this.name = data.name;
this.age = data.age;
}
}
json-to-ts-cvr -i data.json --prefix I
Output:
interface IRoot {
name: string;
age: number;
}
json-to-ts-cvr -i data.json --union-null
Output:
interface Root {
name: string;
nickname: null;
}
Option | Alias | Description |
---|---|---|
--input <file> |
-i |
Input JSON file path |
--output <file> |
-o |
Output TypeScript file path |
--class |
Generate TypeScript classes instead of interfaces | |
--union-null |
Represent nullable fields as union types (field: string | null) | |
--prefix <prefix> |
Prepend a custom prefix to all interface/class names | |
--help |
-h |
Display help information |
--version |
-v |
Display version information |
Input:
{
"user": {
"name": "John",
"address": {
"city": "New York",
"zipCode": 10001
},
"hobbies": ["reading", "gaming"]
}
}
Output:
interface Root {
user: User;
}
interface User {
name: string;
address: Address;
hobbies: string[];
}
interface Address {
city: string;
zipCode: number;
}
# Clone the repository
git clone https://github.com/krikera/json-typescript.git
cd json-typescript
# Install dependencies
npm install
# Build the project
npm run build
# Run tests
npm test
Contributions are welcome! Please feel free to submit a Pull Request.
- Fork the repository
- Create your feature branch (
git checkout -b feature/amazing-feature
) - Commit your changes (
git commit -m 'Add some amazing feature'
) - Push to the branch (
git push origin feature/amazing-feature
) - Open a Pull Request
To publish to npm:
# Login to npm
npm login
# Publish package
npm publish
This project is licensed under the MIT License - see the LICENSE file for details.