Skip to content

Type Safety

koto generates TypeScript type definitions from your translation files, giving you autocompletion and compile-time safety for every translation key.

Why type-safe keys?

Without types, translation key errors are silent runtime bugs:

// Typo -- renders empty string or the raw key in production
t('homePage.hero.subtitel')
// ^^^^^ no error, no warning

With koto-generated types, your editor and CI catch this immediately:

// TypeScript error: Property 'subtitel' does not exist
t('homePage.hero.subtitel')
// ^^^^^ TS2339

Generating types

  1. Enable type generation in your config:

    koto.config.ts
    import { defineConfig } from 'koto'
    export default defineConfig({
    sourceLocale: 'en',
    targetLocales: ['es', 'fr', 'de'],
    files: ['src/locales/[locale].json'],
    provider: {
    name: 'openai',
    model: 'gpt-4o-mini',
    },
    types: {
    output: 'src/types/i18n.d.ts',
    framework: 'next-intl',
    },
    })
  2. Run the types commandnpx koto types

  3. Include the generated file in your tsconfig.json if needed (most setups pick it up automatically).

npx koto types
Generated type definitions → src/types/i18n.d.ts 87 keys mapped

Generated output

Given a source locale file like:

{
"common": {
"save": "Save",
"cancel": "Cancel",
"delete": "Delete"
},
"homePage": {
"hero": {
"title": "Welcome to {appName}",
"subtitle": "The best way to manage your projects"
}
},
"auth": {
"login": "Log in",
"signup": "Sign up",
"forgotPassword": "Forgot password?"
}
}

koto generates:

src/types/i18n.d.ts
// Auto-generated by koto -- do not edit manually
export interface Messages {
common: {
save: string
cancel: string
delete: string
}
homePage: {
hero: {
title: string
subtitle: string
}
}
auth: {
login: string
signup: string
forgotPassword: string
}
}

Framework integrations

Set framework: 'next-intl'. koto generates a module declaration compatible with next-intl’s useTranslations hook:

koto.config.ts
types: {
output: 'src/types/i18n.d.ts',
framework: 'next-intl',
}

Generated output:

src/types/i18n.d.ts
import 'next-intl'
type Messages = {
common: { save: string; cancel: string; delete: string }
homePage: { hero: { title: string; subtitle: string } }
auth: { login: string; signup: string; forgotPassword: string }
}
declare module 'next-intl' {
interface AppMessages extends Messages {}
}

Now useTranslations('common') gives you autocompletion for save, cancel, and delete.

Watch mode

Regenerate types automatically whenever your source locale files change:

Terminal window
npx koto types --watch
npx koto types --watch
Watching for changes... src/locales/en.json changed → regenerating types Generated type definitions → src/types/i18n.d.ts 92 keys mapped

Pairs well with your dev server for instant feedback on new or renamed keys.

Tips

  • Commit the generated type file so all team members and CI have access without running koto types first.
  • Add koto types to your build script as a pre-build step:
    {
    "scripts": {
    "prebuild": "koto types",
    "build": "next build"
    }
    }
  • Use koto types --watch alongside your dev server for a seamless workflow.