A TypeScript utility to infer JavaScript/TypeScript object structures and dynamically generate TypeScript types from runtime values.
MIT License
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.
Date
and Promise
objects in JavaScript.'mixed'
for accurate representation, and infers empty arrays as ['unknown']
.npm install typescript_scribe
or yarn add typescript_scribe
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']
}
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", "sleep", "repeat"] };
// Generate a TypeScript type from the object
const tsType = generateTypeScriptType(myObj);
console.log(tsType);
// Output:
type GeneratedType = {
id: number;
name: string;
tasks: string[];
};
When working with dynamic API responses, typescript_scribe
can infer complex nested structures, saving you time on manual type definition.
const apiResponse = {
user: {
id: 1,
name: "John Doe",
address: {
city: "New York",
postalCode: 10001,
},
tags: ["admin", "contributor"],
settings: {
newsletter: true,
timezone: "EST",
},
},
};
// Infer the structure of the response
const inferredApiResponse = inferType(apiResponse);
console.log(inferredApiResponse);
// Output:
{
user: {
id: 'number',
name: 'string',
address: {
city: 'string',
postalCode: 'number',
},
tags: ['string'],
settings: {
newsletter: 'boolean',
timezone: 'string',
}
}
}
Working with dynamic forms? Let typescript_scribe infer the structure of the collected data for you.
const formData = {
name: "Jane Doe",
age: 29,
preferences: {
theme: "dark",
notifications: true,
},
items: [
{ id: 1, name: "Item 1", price: 9.99 },
{ id: 2, name: "Item 2", price: 19.99 },
],
};
// Generate the TypeScript type from the dynamic form data
const tsFormDataType = generateTypeScriptType(formData, "FormData");
console.log(tsFormDataType);
// Output:
type FormData = {
name: string;
age: number;
preferences: {
theme: string;
notifications: boolean;
};
items: {
id: number;
name: string;
price: number;
}[];
};
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", "sleep", "repeat"] };
const tsType = generateTypeScriptType(myObj, 'CustomType');
console.log(tsType);
// Output:
type CustomType = {
id: number;
name: string;
tasks: string[];
};
typescript_scribe
can infer nested objects and arrays, handling complex structures seamlessly.
const complexObj = {
id: 1,
profile: {
name: "Anthony",
details: {
age: 35,
skills: ["TypeScript", "JavaScript", "AWS"]
}
}
};
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[];
};
};
};
The InferredType
returned by inferType
can be one of the following:
'string'
, 'number'
, 'boolean'
for primitives.'Date'
for Date
objects.'Promise'
for Promise objects.'null'
, 'undefined'
for null and undefined values.'mixed'
for arrays with mixed types.['unknown']
for empty arrays.[1, 'a', true]
), the inferred type will be ['mixed']
.{}
, and empty arrays will return ['unknown']
.inferType
.Date
objects are inferred as 'Date'
to ensure accurate type inference, reflecting their use as a specialized type in TypeScript.['unknown']
, representing an array with unknown element types.inferType
, an error will be thrown because functions are not supported for type inference.Map
, Set
) are not yet supported for type inference.If you find this library helpful and want to support its development, consider buying me a coffee . Your support fuels my ability to keep building great tools for the community!
git checkout -b feature-branch
).git commit -m 'Add some feature'
).git push origin feature-branch
).For large changes, open an issue first to discuss the potential implementation.
We are currently implementing a GitHub Actions pipeline (work in progress) to automate the testing, linting, and publishing process for the project. This will ensure:
We use XO as our linter because it enforces best practices and works seamlessly with TypeScript and JavaScript projects.
To run the linter:
npm run lint
For more details, check out the official XO documentation.
We use Vitest as the test framework because it's modern, fast, and supports both TypeScript and ES Modules seamlessly.
To run the tests:
npm run test
For more details, check out the official Vitest documentation.
This project is licensed under the MIT License.