Improve type safety

This commit is contained in:
shilangyu 2021-03-01 21:33:57 +01:00
parent 19c0223b79
commit 66cc9416e4
2 changed files with 15 additions and 9 deletions

View file

@ -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<string, unknown> = StringMap
>(
key: I18nKeys | I18nKeys[],
options?: TOptions<TInterpolationMap> | string
@ -50,13 +50,17 @@ declare module "i18next" {
// overloaded usage
<
TResult extends TFunctionResult = string,
TInterpolationMap extends object = StringMap
TInterpolationMap extends Record<string, unknown> = StringMap
>(
key: I18nKeys | I18nKeys[],
defaultValue?: string,
options?: TOptions<TInterpolationMap> | string
): TResult;
}
export interface i18nTyped extends i18n {
t: TFunctionTyped;
}
}
`;

View file

@ -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 };