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

Declaring a different defaultNS than "translation" allows unexpected cases on t function. [Typescript] #1302

Closed
ImADrafter opened this issue Apr 19, 2021 · 6 comments · Fixed by #1328
Assignees

Comments

@ImADrafter
Copy link
Contributor

🐛 Bug Report

Declaring a different defaultNS than "translation" allows unexpected cases on t function.

Related issues: #1301

I think the issue might be related to this line since 'translation' is hardcored.

To Reproduce

codesandbox;

// i18nConfig

i18n.use(initReactI18next).init({
  defaultNS: "ns1",
  resources: {
  sk: {
    ns1: {
      a: "translated A"
    },
    ns2: {
      b: "translated B"
    },
    ns3: {
      c: "translated C"
    }
  }
} 
});
// App.tsx
  const { t } = useTranslation();
  ...
  <h4>{t("c")}</h4> {/* It should not compile */}

Expected behavior

When a specifing a different defaultNS than "translation", TS compilation should not allow cases that are not supported by JS implementation.

Your Environment

  • i18next version: i.e. 20.2.1
  • react-i18next version: 11.8.13
@pedrodurek
Copy link
Member

Hey @ImADrafter, It's not possible to automatically infer the defaultNS from t function based on the options passed to the init function. The workaround available right now is this:

const { t } = useTranslation<'ns1'>();

Since people have been requesting the ability to override the defaultNS (without using the workaround above), I'm going to open a PR this week to accommodate that using type augmentation (the same technique we use to infer keys and return types for t function). For now, let's keep this issue opened.

@stale
Copy link

stale bot commented Apr 27, 2021

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

@stale stale bot added the stale label Apr 27, 2021
@pedrodurek pedrodurek removed the stale label Apr 28, 2021
@stale
Copy link

stale bot commented May 6, 2021

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

@stale stale bot added the stale label May 6, 2021
@pedrodurek pedrodurek removed the stale label May 9, 2021
@stale
Copy link

stale bot commented May 16, 2021

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

@stale stale bot added the stale label May 16, 2021
@pedrodurek pedrodurek removed the stale label May 21, 2021
@stale
Copy link

stale bot commented May 28, 2021

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

@stale stale bot added the stale label May 28, 2021
@adrai adrai removed the stale label May 28, 2021
@stale
Copy link

stale bot commented Jun 4, 2021

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

@stale stale bot added the stale label Jun 4, 2021
@stale stale bot closed this as completed Jun 11, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants