i18next / i18next-gitbook

165 stars 172 forks source link

import i18next from 'i18next' does not work with typescript #101

Closed scharf closed 5 years ago

scharf commented 5 years ago

It seems that the docu is out of sync with the i18next module. TypeSctipt complains about the import as suggested in the docu:

import i18next from 'i18next' 

The way it works is:

import * as i18next from 'i18next';
jamuhl commented 5 years ago

https://github.com/i18next/i18next/blob/master/test/typescript/nonEsModuleInterop/exposed.test.ts https://github.com/i18next/i18next/blob/master/test/typescript/init.test.ts

both work depending on ts config -> check above tests

scharf commented 5 years ago

Thanks to a hint by @rosskevin I found a solution:

Adding the option "esModuleInterop": true to tsconfig.json the original (suggested) import works again:

import i18next from 'i18next' 

The point is, that something changed in i18ext that broke this import.

I had to add "esModuleInterop": true, otherwise I need a terrible hack to import i18next-xhr-backend

What confused me was that updating i18next form version 15.1.1 to 17.0.18 broke my code without me changing anything. That happened before, when I switched from 11.9.1 to 15.0.6 I had to change the import form

import * as i18next from 'i18next';

to

import i18next from 'i18next';

I have lots of dependencies but the i18n family of plugins now caused a few times headaches for me and therefore I reported the error (those are the only plugin that broke the imports after an update).

As I said, I did not know about the esModuleInterop option which seems to solve some problems.

I have to admit, I am still a bit confused how imports in typescript/javascript work and I have an an old unanswered question at stackoverflow: "When to use which import style for of CommonJs (node) modules in Typescript?"

jamuhl commented 5 years ago

Fun fact for me...there is no issue (never ever) for users that use plain javascript es6 - but for typescript...there is a endless story of chaos (at least looks like this for me)...glad @rosskevin has the patience to go through this...so all kudos to him

scharf commented 5 years ago

I agree, that javascript is more forgiving and using typescript is painful at times....