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

Improvement: How to fix i18n definition in lifecycle handlers #14

Open
Flanker72 opened this issue Aug 31, 2023 · 0 comments
Open

Improvement: How to fix i18n definition in lifecycle handlers #14

Flanker72 opened this issue Aug 31, 2023 · 0 comments

Comments

@Flanker72
Copy link

Hi! Can't create PR, cause push is not allowed :(

So, please, check this code snippet (only changed lines attached):

// In types.ts
export type I18nThis = Service<{ i18n: I18NSettings }> & ServiceMethods;

// In index.ts
// ...
  async started() {
    const _this = this as unknown as I18nThis // This is the working solution without warnings

    const files = await readFilesSync(_this.settings.i18n.dirName) // Use _this instead of this

    for (const translation of files) {
      // ...
      _this.settings.i18n.polyglot.extend(object) // Use _this instead of this
    }
  }

This reassignment through the unknown allows to use your typed service schema with settings, methods, etc. inside lifecycle handlers like created, started, etc.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant