From: shilangyu Date: Mon, 1 Mar 2021 20:33:57 +0000 (+0100) Subject: Improve type safety X-Git-Url: http://these/git/%24%7BgetStaticDir%28%29%7D/styles/%7Brss%7D?a=commitdiff_plain;h=66cc9416e4b8072414bb24c26f75ab859537db25;p=lemmy-ui.git Improve type safety --- diff --git a/generate_translations.js b/generate_translations.js index 313e800..594f574 100644 --- a/generate_translations.js +++ b/generate_translations.js @@ -32,17 +32,17 @@ const baseLanguage = "en"; fs.readFile(`${translationDir}${baseLanguage}.json`, "utf8", (_, fileStr) => { const keys = Object.keys(JSON.parse(fileStr)); - const data = `import * as i18n from "i18next"; - -type I18nKeys = -${keys.map(key => ` | "${key}"`).join("\n")}; + const data = `import { i18n } from "i18next"; declare module "i18next" { - export interface TFunction { + export type I18nKeys = +${keys.map(key => ` | "${key}"`).join("\n")}; + + export interface TFunctionTyped { // basic usage < TResult extends TFunctionResult = string, - TInterpolationMap extends object = StringMap + TInterpolationMap extends Record = StringMap >( key: I18nKeys | I18nKeys[], options?: TOptions | string @@ -50,13 +50,17 @@ declare module "i18next" { // overloaded usage < TResult extends TFunctionResult = string, - TInterpolationMap extends object = StringMap + TInterpolationMap extends Record = StringMap >( key: I18nKeys | I18nKeys[], defaultValue?: string, options?: TOptions | string ): TResult; } + + export interface i18nTyped extends i18n { + t: TFunctionTyped; + } } `; diff --git a/src/shared/i18next.ts b/src/shared/i18next.ts index 9928903..3d3d5a1 100644 --- a/src/shared/i18next.ts +++ b/src/shared/i18next.ts @@ -1,4 +1,4 @@ -import i18next from "i18next"; +import i18next, { i18nTyped } from "i18next"; import { getLanguage } from "./utils"; import { en } from "./translations/en"; import { el } from "./translations/el"; @@ -82,4 +82,6 @@ i18next.init({ interpolation: { format }, }); -export { i18next as i18n, resources }; +export const i18n = i18next as i18nTyped; + +export { resources };