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[];
};
};
};Development
For those contributing or working on the project, here are some key details:
Contributing
Contributions are welcome! Feel free to open issues or submit pull requests to improve the library. Whether it's bug fixes, new features, or documentation improvements, all contributions help make the project better.
For more details on contributing, visit the GitHub repository.
Linting with XO
We use XO as our linter for typescript_scribe because it is a zero-config, opinionated linter that works great for TypeScript and JavaScript projects. It enforces best practices with minimal configuration, making it a lightweight and fast tool to keep the codebase clean.
To run the linter:
npm run lintFor more details, check out the official XO documentation.
Testing with Vitest
Vitest was chosen as the test framework for this project because it is modern, fast, and supports both TypeScript and ES Modules seamlessly. Vitest’s API is similar to Jest, but with much better performance, making it perfect for TypeScript libraries.
To run the tests:
npm run testFor more details, check out the official Vitest documentation.
License
This project is licensed under the MIT License.