From 285406fe97229d837583d343c9a2ba060761e78f Mon Sep 17 00:00:00 2001 From: 2Pacalypse- Date: Sat, 10 Dec 2022 14:29:15 +0100 Subject: [PATCH 1/2] Export the backend options type Also, stop overriding the CustomPluginOptions in the i18next module since that conflicts with other backends. --- index.d.ts | 18 ++++++------------ 1 file changed, 6 insertions(+), 12 deletions(-) diff --git a/index.d.ts b/index.d.ts index 35cf376..e5b8657 100644 --- a/index.d.ts +++ b/index.d.ts @@ -9,7 +9,7 @@ type AddPathOption = | string | ((lng: string, namespace: string) => string) -interface BackendOptions { +export interface HttpBackendOptions { /** * path where resources get loaded from, or a function * returning a path: @@ -59,7 +59,7 @@ interface BackendOptions { * can be used to support XDomainRequest in IE 8 and 9 */ request?( - options: BackendOptions, + options: HttpBackendOptions, url: string, payload: {} | string, callback: RequestCallback @@ -95,11 +95,11 @@ interface RequestResponse { } export default class I18NextHttpBackend - implements BackendModule + implements BackendModule { static type: "backend"; - constructor(services?: any, options?: BackendOptions); - init(services?: any, options?: BackendOptions): void; + constructor(services?: any, options?: HttpBackendOptions); + init(services?: any, options?: HttpBackendOptions): void; readMulti?( languages: string[], namespaces: string[], @@ -120,11 +120,5 @@ export default class I18NextHttpBackend ): void; type: "backend"; services: any; - options: BackendOptions; -} - -declare module "i18next" { - interface CustomPluginOptions { - backend?: BackendOptions; - } + options: HttpBackendOptions; } From 9892426a8207827989bbca10b2e5b6c164f31b4f Mon Sep 17 00:00:00 2001 From: 2Pacalypse- Date: Sat, 10 Dec 2022 16:07:30 +0100 Subject: [PATCH 2/2] Update README.md with TypeScript usage --- README.md | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/README.md b/README.md index 40f91c9..5d818e7 100644 --- a/README.md +++ b/README.md @@ -196,6 +196,25 @@ const HttpApi = new HttpApi(); HttpApi.init(null, options); ``` +## TypeScript + +To properly type the backend options, you can import the `HttpBackendOptions` interface and use it as a generic type parameter to the i18next's `init` method, e.g.: + +```ts +import i18n from 'i18next' +import HttpBackend, { HttpBackendOptions } from 'i18next-http-backend' + +i18n + .use(HttpBackend) + .init({ + backend: { + // http backend options + }, + + // other i18next options + }) +``` + ---

Gold Sponsors