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

Return type problem #1587

Closed
mazenkhalil opened this issue Nov 28, 2022 · 17 comments
Closed

Return type problem #1587

mazenkhalil opened this issue Nov 28, 2022 · 17 comments
Assignees

Comments

@mazenkhalil
Copy link

🐛 Bug Report

Upon updating to v12.0.0 return type issue appears as follows:
image

As you can see, explicitly defining the return type to string | undefined shows an error. But defining the type as string alone doesn't show any error.

Compare that with explicitly defining the value as below, the behavior sounds weird to me.

image

As you can see here, if the type is string or string | undefined and the value is null, the error appears (expected).

Expected behavior

string | undefined return type should work without issues as it was before updating to v12.0.0.

Your Environment

  • runtime version: node v16
  • i18next version: v12.0.0
  • os: Mac
@adrai
Copy link
Member

adrai commented Nov 28, 2022

https://www.i18next.com/overview/typescript#argument-of-type-defaulttfuncreturn-is-not-assignable-to-parameter-of-type-xyz

@mazenkhalil
Copy link
Author

Sorry my bad. I had to restart the IDE to reload the configurations.

@eiskalteschatten
Copy link

I have been trying to solve this problem all day in my project, but when I follow the instructions linked above, I get these errors:

TS2305: Module '"i18next"' has no exported member 't'.
     1 | import { createAsyncThunk, createSlice, PayloadAction } from '@reduxjs/toolkit';
   > 2 | import { t } from 'i18next';
TS2339: Property 'use' does not exist on type 'typeof import("/Users/alexseifert/other/typescript-fastify-react-boilerplate/packages/client/src/i18next")'.
      7 |
      8 | i18n
   >  9 |   .use(initReactI18next)
        |    ^^^
     10 |   .init({
     11 |     resources,
     12 |     defaultNS: 'common',

Apparently, I'm missing something since it seems to override all of i18next's typings rather than merge with them. Any ideas? For now, I'm going to have downgrade and keep i18next at a fixed version.

This is the i18next.d.ts within the project:
https://github.com/eiskalteschatten/typescript-fastify-react-boilerplate/blob/main/packages/client/src/i18next.d.ts

@millar
Copy link

millar commented Jan 18, 2023

Hey @eiskalteschatten,

I had the same issue and what worked for me was putting the override in my i18n.ts file like so:

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

declare module 'i18next' {
  interface CustomTypeOptions {
    returnNull: false;
  }
}

i18n
  .use(initReactI18next)
  .init({
    returnNull: false,
  });

export default i18n;

@eiskalteschatten
Copy link

Hey @millar,

Thanks for the example. That worked wonderfully for me!

@schaeferCode
Copy link

@millar,

Thank you for that example. Worked for me as well. The thing that bothers me now is I don't know why that worked! Can anyone explain why that putting the override in the i18n.ts file fixed the issue when adding a i18next.d.ts file with the same content didn't?

@Wordllban
Copy link

Wordllban commented Feb 21, 2023

@schaeferCode,

I'm not sure, but maybe it's because override from separate file isn't reading in proper order to apply

@OZZlE
Copy link

OZZlE commented May 4, 2023

initReactI18next

I don't have any i18n.ts file? Using next.js ...

@adrai
Copy link
Member

adrai commented May 4, 2023

@OZZlE
Copy link

OZZlE commented May 5, 2023

Why is this ticket closed, it's still an issue?

@marcus-sa
Copy link

I can't get it to work either ...

@adrai
Copy link
Member

adrai commented Jun 2, 2023

@marcus-sa
Copy link

marcus-sa commented Jun 2, 2023

did you set returnNull: false ?

i18next.com/overview/typescript#argument-of-type-defaulttfuncreturn-is-not-assignable-to-parameter-of-type-xyz

Yeah, I've done it for both react-i18next and i18next.
Both d.ts files are defined in the compilerOptions.types section in my tsconfig.json.

@adrai
Copy link
Member

adrai commented Jun 2, 2023

please provide a minimal reproducible example repository

btw: you need to define that in the i18next options and in the i18next type definitions, nit in the compiler options

@adrai
Copy link
Member

adrai commented Jun 2, 2023

image

@marcus-sa
Copy link

As I've said, that doesn't fix it.
I worked around it by downgrading i18next and react-i18next.
I'd recommend better DX for this because it seems like A LOT of people are having difficulties with your new API.

@adrai
Copy link
Member

adrai commented Jun 2, 2023

@marcus-sa like said: please provide a minimal reproducible example so that can be investigated, like here: https://github.com/i18next/react-i18next/tree/master/example/react-typescript/simple

especially:


and: https://github.com/i18next/react-i18next/blob/master/example/react-typescript/simple/src/i18n/config.ts#L17

beside that, @pedrodurek is currently working on a redesign: i18next/i18next#1911
feel free to help and contribute

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

9 participants