0.0.1 ⢠Published 4 months ago
autotypex v0.0.1
autotypex š
Automatically infer TypeScript types from JavaScript objects!
autotypex
is a lightweight CLI tool and NPM package that dynamically analyzes JavaScript/JSON files and generates TypeScript type definitions. Perfect for developers migrating from JS to TS or handling dynamic data structures.
šŗ Installation
Using Yarn
yarn add autotypex
Using NPM
npm install autotypex
Or, run it directly via npx
:
npx autotypex <file.json>
š Usage
CLI Usage
npx autotypex <file.json> [options]
Example: Inferring a Type from a JSON File
Given sample.json
:
{
"id": 1,
"name": "Alice",
"isActive": true
}
Run:
npx autotypex sample.json
š Output:
type InferredType = {
id: number;
name: string;
isActive: boolean;
}
Custom Type Name
npx autotypex sample.json --name UserType
š Output:
type UserType = {
id: number;
name: string;
isActive: boolean;
}
Saving Output to a .d.ts
File
npx autotypex sample.json --name UserType --save
š Creates types/UserType.d.ts
with:
type UserType = {
id: number;
name: string;
isActive: boolean;
}
Working with JavaScript Files
If sample.js
contains:
module.exports = {
id: 1,
name: "Alice",
isActive: true
};
Run:
npx autotypex sample.js
š Output:
type InferredType = {
id: number;
name: string;
isActive: boolean;
}
š Available Options
Option | Description |
---|---|
-n, --name | Specify the TypeScript type name (default: InferredType ). |
-s, --save | Save the output as a .d.ts file inside types/ . |
-f, --format | Format the output (default: true , can be false ). |
š Programmatic Usage
const { inferType } = require("autotypex");
const obj = { id: 1, name: "Alice", isActive: true };
console.log(inferType(obj));
š Output:
type InferredType = {
id: number;
name: string;
isActive: boolean;
}
š License
MIT License Ā© 2025 Rashad
š Contributing
Contributions are welcome! Feel free to submit an issue or pull request.
š Happy Coding!