https://github.com/krikera/json-typescript
CLI tool to convert JSON structures into TypeScript interfaces and classes with automatic type detection
https://github.com/krikera/json-typescript
cli converter interfaces json-to-typescript nodejs type-generation typescript
Last synced: 5 months ago
JSON representation
CLI tool to convert JSON structures into TypeScript interfaces and classes with automatic type detection
- Host: GitHub
- URL: https://github.com/krikera/json-typescript
- Owner: krikera
- License: mit
- Created: 2025-04-03T11:23:25.000Z (over 1 year ago)
- Default Branch: main
- Last Pushed: 2025-04-03T11:38:26.000Z (over 1 year ago)
- Last Synced: 2025-11-15T18:23:11.316Z (8 months ago)
- Topics: cli, converter, interfaces, json-to-typescript, nodejs, type-generation, typescript
- Language: TypeScript
- Homepage: https://www.npmjs.com/package/json-to-ts-cvr
- Size: 46.9 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# json-to-ts-cvr
[](https://www.npmjs.com/package/json-to-ts-cvr)
[](https://opensource.org/licenses/MIT)
[](https://github.com/krikera/json-typescript/actions/workflows/ci.yml)
> A CLI tool to convert JSON structures into TypeScript interfaces or classes
## Installation
```bash
# Install globally
npm install -g json-to-ts-cvr
# Or use with npx
npx json-to-ts-cvr [options]
```
## Features
- 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
## Usage
### Convert JSON string to TypeScript interface
```bash
json-to-ts-cvr '{"name": "John", "age": 25}'
```
Output:
```typescript
interface Root {
name: string;
age: number;
}
```
### Convert JSON from a file and save to a TypeScript file
```bash
json-to-ts-cvr -i data.json -o types.ts
```
### Generate TypeScript classes instead of interfaces
```bash
json-to-ts-cvr -i data.json --class
```
Output:
```typescript
class Root {
name: string;
age: number;
constructor(data: any) {
this.name = data.name;
this.age = data.age;
}
}
```
### Add a prefix to all interface/class names
```bash
json-to-ts-cvr -i data.json --prefix I
```
Output:
```typescript
interface IRoot {
name: string;
age: number;
}
```
### Handle null values as union types
```bash
json-to-ts-cvr -i data.json --union-null
```
Output:
```typescript
interface Root {
name: string;
nickname: null;
}
```
## Options
| Option | Alias | Description |
|--------|-------|-------------|
| `--input ` | `-i` | Input JSON file path |
| `--output ` | `-o` | Output TypeScript file path |
| `--class` | | Generate TypeScript classes instead of interfaces |
| `--union-null` | | Represent nullable fields as union types (field: string \| null) |
| `--prefix ` | | Prepend a custom prefix to all interface/class names |
| `--help` | `-h` | Display help information |
| `--version` | `-v` | Display version information |
## Examples
### Complex nested objects
Input:
```json
{
"user": {
"name": "John",
"address": {
"city": "New York",
"zipCode": 10001
},
"hobbies": ["reading", "gaming"]
}
}
```
Output:
```typescript
interface Root {
user: User;
}
interface User {
name: string;
address: Address;
hobbies: string[];
}
interface Address {
city: string;
zipCode: number;
}
```
## Development
```bash
# 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
```
## Contributing
Contributions are welcome! Please feel free to submit a Pull Request.
1. Fork the repository
2. Create your feature branch (`git checkout -b feature/amazing-feature`)
3. Commit your changes (`git commit -m 'Add some amazing feature'`)
4. Push to the branch (`git push origin feature/amazing-feature`)
5. Open a Pull Request
## Publishing
To publish to npm:
```bash
# Login to npm
npm login
# Publish package
npm publish
```
## License
This project is licensed under the MIT License - see the [LICENSE](LICENSE) file for details.