Package Exports
- typescript_scribe
Readme
typescript_scribe
typescript_scribe is a lightweight library that automatically infers the structure of JavaScript objects and generates corresponding TypeScript types. It helps engineers quickly generate type definitions from dynamic data, reducing manual work and improving code quality.
Table of Contents
Installation
To install the library:
npm install typescript_scribeBasic Usage
Infer Type
Use the inferType function to deduce the structure of your JavaScript object.
import { inferType } from 'typescript_scribe';
const myObj = {
id: 1,
name: "Anthony",
time: new Date(),
tasks: ["code", "sleep", "repeat"]
};
// Infer the structure of the object
const inferred = inferType(myObj);
console.log(inferred);
/* Output:
{
id: 'number',
name: 'string',
time: 'Date',
tasks: ['string']
}
*/Generate TypeScript Type
Use generateTypeScriptType to generate a TypeScript type definition based on the inferred structure.
import { generateTypeScriptType } from 'typescript_scribe';
const myObj = { id: 1, name: "Anthony", tasks: ["code", "test"] };
// Generate a TypeScript type from the object
const tsType = generateTypeScriptType(myObj);
console.log(tsType);
/* Output:
type GeneratedType = {
id: number;
name: string;
tasks: string[];
};
*/Advanced Usage
Custom Type Names
You can customize the name of the generated TypeScript type by passing a second argument to generateTypeScriptType.
const myObj = { id: 1, name: "Anthony", tasks: ["code", "test"] };
const tsType = generateTypeScriptType(myObj, 'CustomType');
console.log(tsType);
/* Output:
type CustomType = {
id: number;
name: string;
tasks: string[];
};
*/Nested Object Structures
typescript_scribe can infer nested objects and arrays, handling complex structures seamlessly.
const complexObj = {
id: 1,
profile: {
name: "Anthony",
details: {
age: 30,
skills: ["TypeScript", "JavaScript"]
}
}
};
const inferredComplex = inferType(complexObj);
console.log(inferredComplex);
/* Output:
{
id: 'number',
profile: {
name: 'string',
details: {
age: 'number',
skills: ['string']
}
}
}
*/
const tsComplexType = generateTypeScriptType(complexObj, 'ComplexType');
console.log(tsComplexType);
/* Output:
type ComplexType = {
id: number;
profile: {
name: string;
details: {
age: number;
skills: string[];
};
};
};License
This project is licensed under the MIT License.