A powerful tool to infer types from JavaScript values and generate code from these inferred types.
Note
More generators, including Zod and Valibot, will be coming soon.
To install the package locally in your project, run:
npm install def-gen
To install the package globally so you can use the CLI, run:
npm install -g def-gen
Import the inferValue
, inferValues
, and TypeScriptGenerator
from the package.
import { inferValue, inferValues, TypeScriptGenerator } from 'def-gen';
Create an instance of the TypeScriptGenerator
with your desired configuration.
const generator = new TypeScriptGenerator({
indentationSpaces: 2,
exportType: true,
typeName: "MyType"
});
Use inferValue
or inferValues
to infer types from your JavaScript data and generate the corresponding code.
const jsValue = {
name: "Alice",
age: 30
};
const output = inferValue(jsValue, {
generator: generator,
literalKeys: ["name"]
});
console.log(output);
// Output:
// export type MyType = {
// name: "Alice";
// age: number;
// }
const jsValues = [
{ name: "Alice", age: 30 },
{ name: "Bob", age: 25 }
];
const output = inferValues(jsValues, {
generator: generator,
literalKeys: ["name"]
});
console.log(output);
// Output:
// export type MyType = {
// name: "Alice" | "Bob";
// age: number;
// }
If you have installed def-gen
globally, you can use the CLI to generate types from a JSON file.
def-gen [flags...] <json file path>
-e, --export
: Export any declarations or variables outputted.-g, --generator <value>
: Type of format to be output (ts) (default: "ts").-h, --help
: Show help.-i, --indentation <number>
: Amount of spaces used for indentation (default: 2).-l, --literal-keys <string>
: Dot separated keys (object.key) to be interpreted as a literal (default: []).-n, --name <string>
: Name used for the final variable or type.
def-gen -e -g ts -i 4 -l "user.name" -n "UserType" path/to/data.json
Infers the type of a single value and generates code using the specified generator.
value: unknown
: The value to infer the type from.config: Config
: The configuration for type inference and code generation.
string
: The generated code.
Infers the types of multiple values and generates code using the specified generator.
values: unknown[]
: The values to infer the types from.config: Config
: The configuration for type inference and code generation.
string
: The generated code.
A class for generating TypeScript code from inferred types.
config?: TypeScriptGeneratorConfig
: Optional configuration for the TypeScript generator.indentationSpaces?: number
: Number of spaces to use for indentation (default: 2).typeName?: string
: The name of the generated type.exportType?: boolean
: Whether to export the generated type (default: false).
Configuration for type inference and code generation.
literalKeys?: string[]
: An array of dot-separated keys that will be turned into literal values.generator: CodeGenerator
: The code generator to use for generating code from inferred types.
Configuration for the TypeScript code generator.
indentationSpaces?: number
: Number of spaces to use for indentation (default: 2).typeName?: string
: The name of the generated type.exportType?: boolean
: Whether to export the generated type (default: false).
MIT