schema-auto-type
v0.0.0
Published
Automatically infer TypeScript types from schema definitions in popular libraries like Zod, TypeBox, Valibot, and more!
Downloads
9
Readme
📘 schema-auto-type
Automatically infer TypeScript types from schema definitions in popular libraries like Zod, TypeBox, Valibot, and more!
🚀 Features
- Automatic Type Generation: No need to manually write type definitions for your schemas.
- Supports Multiple Libraries: Works with Zod, TypeBox, Valibot, and any custom schema libraries.
- Customizable: Easily extendable to support additional schema libraries via configuration.
- TypeScript Transformer: Integrates seamlessly into your TypeScript compilation process.
📦 Installation
Install the transformer via npm:
npm install --save-dev auto-schema-type
🔧 Configuration
To use the transformer, you need to configure it in your tsconfig.json
file under the plugins
section.
Basic Setup with Predefined Libraries
Here's how to set it up using predefined library configurations:
{
"compilerOptions": {
"plugins": [
{
"transform": "auto-schema-type",
"libraries": ["zod", "tinybuf"] // List of libraries to support
}
]
}
}
Advanced Setup with Custom Libraries
You can also add custom library configurations directly in your tsconfig.json
:
{
"compilerOptions": {
"plugins": [
{
"transform": "auto-schema-type",
"libraries": ["zod"], // Use predefined 'zod' library config
"customLibraries": [ // Add custom library configs
{
"name": "myCustomLibrary",
"baseTypes": ["MyBaseSchema"],
"modulePath": "my-custom-library",
"inference": {
"template": "MyInfer<typeof {schema}>",
"imports": [
{ "name": "MyInfer", "path": "my-custom-library" }
]
},
"transformOptions": {
"addInferenceImport": true
}
}
]
}
]
}
}
🛠️ Usage
Once configured, the transformer will automatically generate type aliases for your schemas during compilation.
Example with Zod
import { z } from 'zod';
const UserSchema = z.object({
name: z.string(),
email: z.string().email(),
});
After running the transformer, the following type alias will be generated:
export type UserSchema = z.infer<typeof UserSchema>;
Example with Custom Library
Assuming you have a custom schema library my-custom-library
:
import { MyBaseSchema, defineSchema } from 'my-custom-library';
const ProductSchema = defineSchema({
id: 'number',
name: 'string',
price: 'number',
});
With the custom library configuration, the transformer will generate:
import { MyInfer } from 'my-custom-library';
export type ProductSchema = MyInfer<typeof ProductSchema>;
✨ Customization
You can extend the transformer to support any schema library by providing custom LibraryConfig
objects in the customLibraries
array in your tsconfig.json
.
LibraryConfig
Structure
interface LibraryConfig {
name: string;
baseTypes: string[];
modulePath: string;
inference: {
template: string;
imports?: Array<{ name: string; path: string }>;
};
transformOptions?: {
addInferenceImport?: boolean;
};
}
Example Configuration
{
"name": "myCustomLibrary",
"baseTypes": ["MyBaseSchema"],
"modulePath": "my-custom-library",
"inference": {
"template": "MyInfer<typeof {schema}>",
"imports": [
{ "name": "MyInfer", "path": "my-custom-library" }
]
},
"transformOptions": {
"addInferenceImport": true
}
}
⚙️ How It Works
- The transformer scans your TypeScript files for schema definitions.
- It uses type-checking to identify variables that extend or satisfy a specified base type from a given module.
- It generates a type alias for each schema using the provided template.
- It adds necessary imports if they are not already present.
📝 Notes
- The transformer only processes TypeScript files (
.ts
and.tsx
). - It integrates with the TypeScript compiler API and works during the compilation process.
- Ensure all properties in your custom
LibraryConfig
are serializable to JSON.
🐞 Troubleshooting
- Library Not Recognized: If you receive a warning about an unrecognized library, ensure the library name matches the predefined ones or is correctly defined in your custom configurations.
- No Type Aliases Generated: Check that your schemas are correctly extending or satisfying the base types specified in the configurations.
- Compilation Errors: Ensure that the transformer is correctly configured in your
tsconfig.json
and that all dependencies are installed.
📄 License
This project is licensed under the MIT License.
🙏 Acknowledgments
Special thanks to all contributors and the TypeScript community for making such integrations possible.
📫 Contact
For questions or support, please open an issue on the GitHub repository or reach out to the maintainer.
Happy coding! 🎉