Skip to content

Commit

Permalink
Export the backend options type (#34)
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- authored Dec 10, 2022
1 parent 207db34 commit 894786d
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 11 deletions.
19 changes: 19 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,25 @@ const Backend = new Backend();
Backend.init(null, options);
```

## TypeScript

To properly type the backend options, you can import the `FsBackendOptions` interface and use it as a generic type parameter to the i18next's `init` method, e.g.:

```ts
import i18n from 'i18next'
import FsBackend, { FsBackendOptions } from 'i18next-fs-backend'

i18n
.use(FsBackend)
.init<FsBackendOptions>({
backend: {
// fs backend options
},

// other i18next options
})
```

# If set i18next initImmediate option to false it will load the files synchronously

```js
Expand Down
16 changes: 5 additions & 11 deletions index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ type AddPathOption =
| string
| ((language: string, namespace: string) => string)

interface BackendOptions {
export interface FsBackendOptions {
/**
* path where resources get loaded from, or a function
* returning a path:
Expand All @@ -31,11 +31,11 @@ interface BackendOptions {
}

export default class I18NexFsBackend
implements BackendModule<BackendOptions>
implements BackendModule<FsBackendOptions>
{
static type: "backend";
constructor(services?: any, options?: BackendOptions);
init(services?: any, options?: BackendOptions): void;
constructor(services?: any, options?: FsBackendOptions);
init(services?: any, options?: FsBackendOptions): void;
read(language: string, namespace: string, callback: ReadCallback): void;
create?(
languages: string[],
Expand All @@ -45,11 +45,5 @@ export default class I18NexFsBackend
): void;
type: "backend";
services: any;
options: BackendOptions;
}

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

0 comments on commit 894786d

Please sign in to comment.