Skip to content

Commit

Permalink
Export the backend options type (#105)
Browse files Browse the repository at this point in the history
* Export the backend options type

Also, stop overriding the CustomPluginOptions in the i18next module
since that conflicts with other backends.

* Update README.md with TypeScript usage
  • Loading branch information
2Pacalypse- committed Dec 10, 2022
1 parent bede968 commit 0085ebb
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 12 deletions.
19 changes: 19 additions & 0 deletions README.md
Expand Up @@ -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<HttpBackendOptions>({
backend: {
// http backend options
},

// other i18next options
})
```

---

<h3 align="center">Gold Sponsors</h3>
Expand Down
18 changes: 6 additions & 12 deletions index.d.ts
Expand Up @@ -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:
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -95,11 +95,11 @@ interface RequestResponse {
}

export default class I18NextHttpBackend
implements BackendModule<BackendOptions>
implements BackendModule<HttpBackendOptions>
{
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[],
Expand All @@ -120,11 +120,5 @@ export default class I18NextHttpBackend
): void;
type: "backend";
services: any;
options: BackendOptions;
}

declare module "i18next" {
interface CustomPluginOptions {
backend?: BackendOptions;
}
options: HttpBackendOptions;
}

0 comments on commit 0085ebb

Please sign in to comment.