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

Typescript: error when using multiple fallback key and at least one key is valid #2183

Closed
lpproulx opened this issue May 10, 2024 · 2 comments

Comments

@lpproulx
Copy link

lpproulx commented May 10, 2024

🐛 Bug Report

Maybe there is something i'm doing wrong or i don't understand correctly but i was expecting typescript to not give an error when there is at least one valid key that it can fallback to.

To Reproduce

import i18n from 'i18next';
import { initReactI18next } from 'react-i18next';

export const defaultNS = 'common';
export const resources = {
  en: {
    common: {
      error: {
        unspecific: 'Unknown error',
        existingError: 'Something went wrong',
      },
    },
  },
} as const;

i18n.use(initReactI18next).init({
  defaultNS,
  resources,
  lng: 'en',
});

const existingError = 'existingError';
const nonExistingError = 'otherError';

i18n.t([`common:error.${existingError}`, 'common:error.unspecific']);
// Typescript error here
i18n.t([`common:error.${nonExistingError}`, 'common:error.unspecific']);
import 'react-i18next';
import { defaultNS, resources } from '../locales';

declare module 'i18next' {
  interface CustomTypeOptions {
    defaultNS: typeof defaultNS;
    resources: (typeof resources)['en'];
  }
}
Argument of type '[("common:error.unspecific" | "common:error.otherError")[]]' is not assignable to parameter of type '[key: string | string[], options: TOptionsBase & $Dictionary & { defaultValue: string; }] | [key: string | string[], defaultValue: string, options?: (TOptionsBase & $Dictionary) | undefined] | [key: ...]'.
  Type '[("common:error.unspecific" | "common:error.otherError")[]]' is not assignable to type '[key: TemplateStringsArray | "error.existingError" | "error.unspecific" | "common:error.existingError" | "common:error.unspecific" | (TemplateStringsArray | "error.existingError" | "error.unspecific" | "common:error.existingError" | "common:error.unspecific")[], options?: (TOptionsBase & $Dictionary) | undefined]'.
    Type at position 0 in source is not compatible with type at position 0 in target.
      Type '("common:error.unspecific" | "common:error.otherError")[]' is not assignable to type 'TemplateStringsArray | "error.existingError" | "error.unspecific" | "common:error.existingError" | "common:error.unspecific" | (TemplateStringsArray | "error.existingError" | "error.unspecific" | "common:error.existingError" | "common:error.unspecific")[]'.
        Type '("common:error.unspecific" | "common:error.otherError")[]' is not assignable to type '(TemplateStringsArray | "error.existingError" | "error.unspecific" | "common:error.existingError" | "common:error.unspecific")[]'.
          Type '"common:error.unspecific" | "common:error.otherError"' is not assignable to type 'TemplateStringsArray | "error.existingError" | "error.unspecific" | "common:error.existingError" | "common:error.unspecific"'.
            Type '"common:error.otherError"' is not assignable to type 'TemplateStringsArray | "error.existingError" | "error.unspecific" | "common:error.existingError" | "common:error.unspecific"'.ts(2345)

Expected behavior

There is no typescript error when there is at least 1 valid key

Your Environment

  • runtime version: node 20.11.1
  • i18next version: 23.11.2
  • os: Windows
  • typescript: 5.4.5
@marcalexiei
Copy link
Member

This is the expected behaviour: all keys are type checked. If you need to provide a fallback use defaultValue option: https://www.i18next.com/translation-function/essentials#overview-options

@lpproulx
Copy link
Author

lpproulx commented May 13, 2024

Thanks. I tried this way early on but i was using the key as a defaultValue and it was not working, but you made me realize that i can do

i18n.t([`common:error.${nonExistingError}`, 'anyOtherPossiblyWrongKey'], { defaultValue: i18n.t( 'common:error.unspecific') });
or this way in my case
i18n.t(`common:error.${nonExistingError}`, { defaultValue: i18n.t( 'common:error.unspecific') });

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

No branches or pull requests

3 participants