International Types

0.8.1 · active · verified Sun Apr 19

international-types is a focused TypeScript package that provides essential utility types for constructing robust and type-safe internationalization (i18n) solutions. It is designed to enhance developer experience by offering strong type inference and autocompletion for translation keys, message scopes, and interpolation parameters within i18n implementations. The current stable version is 0.8.1. While this package does not provide any runtime i18n functionality itself, it serves as the foundational typing layer for higher-level i18n libraries, such as `next-international`. Its core differentiators are compile-time validation, preventing common i18n-related errors like missing keys or incorrect parameter types, and promoting maintainability in localized applications by ensuring all translation requirements are met at build time. Its release cycle is often synchronized with its primary consumer, `next-international`.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to set up type-safe translation keys, scopes, and parameters using `international-types`. It shows how to define a locale type, create a `t` function with robust type inference, and ensures that translation keys exist and all required interpolation parameters are provided at compile time, reducing runtime errors.

import type {
  LocaleKeys,
  BaseLocale,
  Scopes,
  ScopedValue,
  CreateParams,
  ParamsObject
} from 'international-types';

type AppLocale = {
  param: 'This is a {value}';
  'hello.people': 'Hello {name}! You are {age} years old.';
  'scope.nested.greeting': 'A nested greeting.';
};

function createI18nTypedT<Locale extends BaseLocale, Scope extends Scopes<Locale> | undefined>(scope?: Scope) {
  return function t<Key extends LocaleKeys<Locale, Scope>, Value extends ScopedValue<Locale, Scope, Key>>(
    key: Key,
    ...params: CreateParams<ParamsObject<Value>, Locale, Scope, Key, Value>
  ) {
    // In a real i18n library, this function would handle the actual translation lookup and interpolation.
    // For this example, we just log the key and params to demonstrate type safety.
    console.log(`Translating key: ${String(key)} with params: ${JSON.stringify(params[0]) || '{}'}`);

    // Example: Dummy return for a type-safe signature
    return `Translated: ${String(key)}`
  };
}

const t = createI18nTypedT<AppLocale, undefined>();

t('param', {
  value: 'exampleValue' // 'value' is required and type-checked here
});

t('hello.people', {
  name: 'John Doe',
  age: 30 // 'name' and 'age' are required and type-checked
});

const scopedT = createI18nTypedT<AppLocale, 'scope.nested'>('scope.nested');

scopedT('greeting'); // Autocompletes to 'greeting' under 'scope.nested'

// Example of intentionally incorrect usage (will cause TypeScript errors)
// t('non.existent.key');
// t('param', { wrongParam: 'value' });
// t('hello.people', { name: 'Alice' }); // Missing 'age' parameter

view raw JSON →