Skip to content

Instantly share code, notes, and snippets.

@SandroMaglione
Last active June 24, 2024 13:28
Show Gist options
  • Save SandroMaglione/35532b42df93f71b8a3af016f2f2207c to your computer and use it in GitHub Desktop.
Save SandroMaglione/35532b42df93f71b8a3af016f2f2207c to your computer and use it in GitHub Desktop.
i18n (Internationalization and localization) in Typescript using `satisfies` only
type Message = string | ((args: any) => string);
type Translation = { en: Message; it: Message };
export type I18n = Record<string, Translation>;
export const t = {
helloWorld: {
en: "Hello World",
it: "Ciao mondo",
},
helloParam: {
en: (param: string) => `Hello ${param}` as const,
it: (param: number) => `Ciao ${param}` as const,
},
} as const satisfies I18n;
const helloWorld = t.helloWorld["it"];
const helloParamEn = t.helloParam["en"]("world");
const helloParamIt = t.helloParam["it"](10);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment