Skip to content

When not using suspense, provoke a typescript error if t is accessed without first checking ready (optional setting) #1896

@sydonia86

Description

@sydonia86

🚀 Feature Proposal

Add a new boolean type option that, when enabled and suspense is disabled, makes the UseTranslationResponse type evaluate to something like { t: TFunction; ready: true; } | { ready?: false; }.

That would provoke a type error if t is accessed without first checking ready.

Motivation

Disabling suspense can result in snappier loading behavior, but if you forget to check ready, you tend to get i18n keys visibly rendered on your site.
Unfortunately, to make use of this you would have to avoid destructuring the useTranslation return value and use i18n.ready and i18n.t, which is why it should be opt-in behavior.

I'm also not sure if and how it would affect the non-hook stuff.

Example

declare module 'i18next' {
  interface CustomTypeOptions {
    strictNoSuspense: true,
  }
}
const Component = () => {
  const i18n = useTranslation(['translation'], {useSuspense: false});
  i18n.t($ => $.foo); // error!
  i18n.ready && i18n.t($ => $.foo); // ok
};

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions