Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Export the backend options type #105

Merged
merged 2 commits into from Dec 10, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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;
}