Documentation
TypeScript Type Definitions (.d.ts) Generation
Overview
This document describes the implementation of .d.ts type definition file generation for all TypeScript projects in the repository.
Why .d.ts Files?
While Deno uses TypeScript natively and doesn't require .d.ts files for its own operation, they are useful for:
- npm Publishing: If these packages are published to npm, consumers will need
.d.tsfiles - IDE Support: Better autocomplete and type checking in editors for external consumers
- Documentation: Type definitions serve as documentation of the public API
- Compatibility: Non-Deno tools (like tsc, webpack) may require declaration files
Implementation
Type Generation Script
Each TypeScript project has a generate-types.ts script that:
- Uses only built-in Deno APIs (no external dependencies)
- Recursively walks through the
src/directory - Excludes test files (
*.test.ts) - Generates
.d.tsfiles that re-export from the original.tsfiles - Places all output in a
dist/directory
Location:
src/rules-compiler-typescript/generate-types.tssrc/adguard-api-typescript/generate-types.tssrc/linear/generate-types.ts
Generated Files
The .d.ts files are simple re-exports that leverage TypeScript's type resolution:
/**
* Type definitions for index
* AUTO-GENERATED - do not edit manually
*
* This file re-exports types from the TypeScript source file.
* For Deno projects, the .ts files themselves serve as type definitions.
*/
export * from '../src/index.ts';
This approach:
- Maintains a single source of truth (the
.tsfiles) - Automatically stays in sync when source files change (when regenerated)
- Provides proper type information to consumers
Usage
Generate Type Definitions
From any TypeScript project directory:
deno task generate:types
This command is available in:
src/rules-compiler-typescript/src/adguard-api-typescript/src/linear/
Output
The command will:
- Clean the existing
dist/directory - Walk through all
.tsfiles insrc/ - Generate corresponding
.d.tsfiles - Report the number of files generated
Example output:
🔨 Generating TypeScript declaration files...
📁 Source: /path/to/src
📁 Output: /path/to/dist
✓ index.d.ts
✓ types.d.ts
✓ models/user.d.ts
...
✅ Generated 48 declaration files successfully!
📁 Output: /path/to/dist
Keeping Type Definitions Updated
Manual Updates
When you modify TypeScript source files, regenerate the type definitions:
# From the project directory
deno task generate:types
CI/CD Integration
The GitHub Actions workflow (.github/workflows/typescript.yml) automatically:
- Generates type definitions for each project
- Verifies the
dist/directory was created - Counts the generated
.d.tsfiles - Fails the build if no files are generated
This ensures that type definitions are always validated in CI but are not committed to the repository (since they're generated).
Best Practices
- Regenerate After Changes: Always run
deno task generate:typesafter modifying source files - Don't Edit Manually: Never edit
.d.tsfiles directly - they are auto-generated - Check in CI: The CI workflow will verify type definitions can be generated successfully
- Exclude from Git: The
dist/directories are in.gitignoreto avoid committing generated files
Project-Specific Details
rules-compiler-typescript
- Files: 17
.d.tsfiles - Entry Point:
dist/index.d.ts - Main Exports: RulesCompiler, ConfigurationBuilder, types
adguard-api-typescript
- Files: 48
.d.tsfiles - Entry Point:
dist/index.d.ts - Main Exports: AdGuardDnsClient, API classes, models, repositories
linear
- Files: 5
.d.tsfiles - Entry Point:
dist/mod.d.ts - Main Exports: LinearClient, parser, types
Troubleshooting
"Permission denied" Error
Make sure the script has execute permissions:
chmod +x generate-types.ts
Or run with explicit permissions:
deno run --allow-read --allow-write --no-npm generate-types.ts
Network Errors
If you see npm registry errors, ensure the --no-npm flag is used:
deno run --allow-read --allow-write --no-npm generate-types.ts
Empty dist/ Directory
Check that:
- The
src/directory exists and contains.tsfiles - No syntax errors in TypeScript files prevent them from being read
- File permissions allow reading source files
Future Enhancements
Possible improvements to consider:
- Watch Mode: Auto-regenerate on file changes during development
- Bundle Generation: Create a single bundled
.d.tsfile for simpler distribution - JSDoc Extraction: Include JSDoc comments in generated files
- Type Checking: Validate generated types compile correctly
- npm Package: Create npm package structure with proper
typesfield in package.json