Type Generation
Relevant source files
The following files were used as context for generating this wiki page:
Type Generation in the nuxt-i18n-micro project ensures end-to-end TypeScript safety by transforming translation dictionaries and module configurations into strongly typed interfaces. The system primarily facilitates type-safe translation keys for developers and maintains synchronized documentation based on the underlying source code types.
The architecture prioritizes a "types-first" development order, where shared types are defined in packages/types before implementation occurs in runtime packages. This approach prevents duplication of logic and ensures consistency across the framework-agnostic core and framework-specific bindings.
Sources: AGENTS.md:16-20, AGENTS.md:90
Translation Key Type Generation
The @i18n-micro/types-generator package automates the creation of TypeScript definitions for translation keys. It monitors local JSON translation files and generates corresponding .d.ts files, allowing IDEs to provide autocompletion for i18n keys.
Core Technologies
The generator utilizes several key libraries to manage file systems and build tool integration:
- Chokidar: Watches the file system for changes in translation dictionaries to trigger regenerations.
- Globby: Scans the
translationDirfor JSON files. - Unplugin: Provides a unified interface for integration with Vite and Nuxt build pipelines.
Sources: packages/types-generator/package.json:84-88, packages/types-generator/CHANGELOG.md:73-75
Generation Logic Flow
The diagram shows the automated flow from translation file updates to TypeScript definition output.
Sources: packages/types-generator/package.json:44-65, packages/types-generator/CHANGELOG.md:12-25
Documentation Type Extraction
The project employs a specialized command, docs-generate, to synchronize the documentation with the current TypeScript types. This prevents the "drift" often seen between code implementation and manual documentation.
Processing Components and Symbols
The system parses different source types using specific tools:
- Vue Components: Uses
vue-docgen-apito extract props, slots, and events from.vuefiles. - Composables & Helpers: Uses
TypeDocto read interfaces (likePluginsInjectionsandNuxtAppOnlyInjections) and extract signatures, parameters, and return types. - Module Options: Reads the
ModuleOptiontype to generate grouped configuration tables.
Sources: scripts/src/commands/docs-generate.ts:7-14, scripts/src/commands/docs-generate.ts:431-455
Data Extraction Flow
This diagram illustrates how the documentation CLI extracts metadata from source files to update Markdown files.
Sources: scripts/src/commands/docs-generate.ts:400-470
API Surface Management
The project maintains API snapshots to track public-facing types and members. This ensures that re-exported types across different packages (like core, types, and utils) remain consistent without redundant documentation.
| Component | Functionality |
|---|---|
parseSnapshot | Folds class members under their parent export for cleaner documentation. |
ApiPackage | Tracks export counts and entry points for each workspace package. |
memberKey | Generates unique IDs for member lists to detect and link duplicate types. |
Sources: scripts/src/commands/docs-generate.ts:182-210, scripts/src/commands/docs-generate.ts:241-260
Type Validation and Auditing
The docs-audit command validates that every public module option is documented. It checks for:
- Undocumented Options: Options defined in
ModuleOptionsbut missing from Markdown. - Stale Options: Documentation referring to options that no longer exist in the TypeScript interfaces.
- Handwritten Drift: Detects if an option section is written by hand instead of using the generated regions.
Sources: scripts/src/commands/docs-audit.ts:163-200, scripts/src/commands/docs-audit.ts:205-220
Audit Finding Types
| Code | Level | Description |
|---|---|---|
undocumented-option | Error | Option exists in packages/types but not in /docs. |
handwritten-option | Error | Option documented manually; requires generation region. |
stale-option | Warning | Documentation exists for a non-existent ModuleOptions key. |
Sources: scripts/src/commands/docs-audit.ts:173-220
Type Generation in nuxt-i18n-micro serves as the foundation for both developer experience (DX) and documentation accuracy, leveraging automation to keep the large-scale project performant and maintainable.