Skip to content

Conversation

@leonace924
Copy link

@leonace924 leonace924 commented Jan 5, 2026

Add strictNoSuspense TypeScript option for safer useTranslation usage

Fixes #1896

Description

When using useSuspense: false, forgetting to check ready before accessing t causes i18n keys to render on the page. This PR adds an optional TypeScript setting that enforces checking ready before using t.

Usage

// Enable in your i18next.d.ts
declare module 'i18next' {
  interface CustomTypeOptions {
    strictNoSuspense: true;
  }
}
const result = useTranslation('ns', { useSuspense: false });

result.t('key');  // TypeScript error: t is possibly undefined

if (result.ready) {
  result.t('key');  // OK
}

result.ready && result.t('key');  // Also OK

When useSuspense: true (default) or when strictNoSuspense is not enabled, behavior remains unchanged.

Changes

  • Added UseTranslationResponseNotReady, UseTranslationResponseReady, and UseTranslationResponseStrict types to index.d.ts
  • Added overloads to useTranslation that return discriminated union when useSuspense: false and strictNoSuspense: true
  • Added test suite in test/typescript/strict-no-suspense/

Checklist

  • only relevant code is changed (make a diff before you submit the PR)
  • run tests npm run test
  • tests are included
  • commit message and code follows the Developer's Certification of Origin

Checklist (for documentation change)

  • only relevant documentation part is changed (make a diff before you submit the PR)
  • motivation/reason is provided
  • commit message and code follows the Developer's Certification of Origin

Contribution by Gittensor, see my contribution statistics at https://gittensor.io/miners/details?githubId=42954461

@coveralls
Copy link

Coverage Status

coverage: 97.682%. remained the same
when pulling ea038a4 on leonace924:fix/no-suspense
into 272929c on i18next:master.

@adrai
Copy link
Member

adrai commented Jan 5, 2026

if this lands, we need to update the docs, either here or here or both.

@leonace924
Copy link
Author

if this lands, we need to update the docs, either here or here or both.

@adrai do i need to update the docs as well?

@adrai
Copy link
Member

adrai commented Jan 5, 2026

if this lands, we need to update the docs, either here or here or both.

@adrai do i need to update the docs as well?

if this PR lands, yes - that would be nice

@leonace924
Copy link
Author

leonace924 commented Jan 5, 2026

if this lands, we need to update the docs, either here or here or both.

@adrai do i need to update the docs as well?

if this PR lands, yes - that would be nice

@adrai i will put the PR for docs once this is landed.
When would you review and this can land?

@adrai
Copy link
Member

adrai commented Jan 5, 2026

When would you review and this can land?

depends on @marcalexiei or @ahrjarrett

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

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

3 participants