A TypeScript type generation module for Tessen localization. This module analyzes Tessen instances and generates TypeScript declarations for type-safe localization usage.
- 🔍 Smart Parameter Detection: Automatically detects parameters in localization strings
- 🏷️ Named Parameters: Supports both indexed (
{0}
,{1}
) and named ({0:user}
,{1:role}
) parameters - 🔗 Multi-Client Support: Generate types for multiple Tessen instances
- 📁 Nested Structures: Handles nested localization objects
- 💾 File Output: Write generated types directly to TypeScript declaration files
npm install @tessen/type-gen
# or
pnpm add @tessen/type-gen
# or
yarn add @tessen/type-gen
import { generateLocalizationTypes, createTypeGenerationConfig } from '@tessen/type-gen';
import { Tessen } from 'tessen';
// Create your Tessen instance
const tessen = new Tessen({
id: 'my-bot',
clients: [{
id: 'main-client',
token: 'your-bot-token',
options: { intents: [] }
}]
});
// Generate types
const config = createTypeGenerationConfig(tessen);
const types = generateLocalizationTypes(config);
console.log(types);
import { generateLocalizationTypes, createTypeGenerationConfig } from '@tessen/type-gen';
const config = createTypeGenerationConfig([tessen1, tessen2, tessen3]);
const types = generateLocalizationTypes(config);
import { writeLocalizationTypes } from '@tessen/type-gen';
// Option 1: Use default output path (process.cwd()/localization.d.ts)
const config = createTypeGenerationConfig(tessen);
await writeLocalizationTypes(config);
// Option 2: Specify custom output path in config
const configWithPath = createTypeGenerationConfig(tessen, './types/localization.d.ts');
await writeLocalizationTypes(configWithPath);
The module automatically detects and converts localization parameters:
Input: "{0} is now a {1}"
Output: (_0: string, _1: string) => string
Input: "{0:user} is now a {1:role}"
Output: (user: string, role: string) => string
Input: "Hello, world!"
Output: () => string
For a Tessen instance with localization data, the module generates:
declare global {
namespace Tessen {
interface Localization {
hello: () => string;
welcome: (username: string) => string;
userJoined: (user: string, server: string) => string;
nested: {
commands: {
help: () => string;
ping: (target: string) => string;
};
messages: {
error: (errorMessage: string) => string;
success: () => string;
};
};
}
}
}
When multiple Tessen instances are provided:
declare global { // Client 1
namespace Tessen {
interface Localization {
hello: () => string;
world: (exampleParam: string) => string;
}
}
}
declare global { // Client 2
namespace Tessen {
interface Localization {
secondClientHello: () => string;
}
}
}
Generates TypeScript declaration strings from Tessen instances.
Parameters:
config
: Configuration object containing Tessen instances
Returns: Generated TypeScript declaration string
Creates a configuration object for type generation.
Parameters:
tessens
: Single Tessen instance or array of instancesoutputPath
: Optional custom output path (defaults toprocess.cwd()/localization.d.ts
)
Returns: Configuration object
Writes generated types to a file using the output path from the configuration.
Parameters:
config
: Configuration object (containing tessens and optional outputPath)
interface TypeGenerationConfig {
tessens: Tessen[];
outputPath?: string;
}
interface ExtractedParameter {
name: string;
type: string;
}
This module is part of the Tessen ecosystem and is currently in development. It is not yet ready for production use.
LGPL-3.0-or-later
- Kıraç Armağan Önal
- Erdem Göksel