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: 'I18n' can only be imported by using a default import #89

Closed
akuul opened this issue Feb 11, 2024 · 11 comments · Fixed by #90
Closed

Typescript: 'I18n' can only be imported by using a default import #89

akuul opened this issue Feb 11, 2024 · 11 comments · Fixed by #90

Comments

@akuul
Copy link

akuul commented Feb 11, 2024

Description

Importing asimport { I18n } from 'i18n-js'; throws TS error 'I18n' can only be imported by using a default import . Importing as default import import I18n from 'i18n-js'; throws on new I18n()
This expression is not constructable. Type 'typeof I18n' has no construct signatures.

How to reproduce

Use I18n as described in docs

What do you expect

Not to throws Typescript error

Software:

  • Ruby version: 2.7.6
  • React Native: 0.73.4
  • I18n: 4.4.0
@TheWirv
Copy link
Contributor

TheWirv commented Feb 11, 2024

I have a similar problem:
image
I think, #79 fucked up the imports...

@fnando
Copy link
Owner

fnando commented Feb 11, 2024

What does your tsconfig.json file look like? I have a project running with 4.4.0 and it's working with import { I18n as I18nJS } from "i18n-js";.

$ yarn why i18n-js
└─ app@workspace:.
   └─ i18n-js@npm:4.4.0 (via npm:^4.4.0)

$ cat app/assets/scripts/config/i18n.ts
import { I18n as I18nJS } from "i18n-js";
import translations from "config/translations.json";

export const i18n = new I18nJS(translations);
i18n.defaultLocale = "en";

$ tsc --noEmit

$ echo $?
0

Here's mine:

{
  "include": ["./app/assets/scripts"],
  "compilerOptions": {
    "rootDir": ".",
    "baseUrl": "./app/assets/scripts",
    "lib": ["DOM", "es2020"],
    "target": "es2015",
    "moduleResolution": "node",
    "isolatedModules": true,
    "allowSyntheticDefaultImports": true,
    "esModuleInterop": true,
    "resolveJsonModule": true,
  },
}

@TheWirv
Copy link
Contributor

TheWirv commented Feb 11, 2024

I made a PR to fix the issue

@fnando
Copy link
Owner

fnando commented Feb 11, 2024

@TheWirv based on your PR, looks like you don't have esModuleInterop/allowSyntheticDefaultImports set to true. Is that correct?

@TheWirv
Copy link
Contributor

TheWirv commented Feb 11, 2024

No, my tsconfig.json is as follow:

{
  "extends": "@react-native/typescript-config/tsconfig.json",
  "compilerOptions": {
    "types": ["react-native"],
    "experimentalDecorators": true,
    "baseUrl": ".",
    "paths": {
      "@green-notes/*": ["./src/*"]
    }
  }
}

And below is a screenshot excerpt from the RN config:
image

@fnando
Copy link
Owner

fnando commented Feb 11, 2024

OK, it makes to define the whole path then.

@TheWirv
Copy link
Contributor

TheWirv commented Feb 11, 2024

Also, the json bit is needed anyway. Otherwise, we wouldn't be able to access the base translations at all. :/

@fnando
Copy link
Owner

fnando commented Feb 11, 2024

@TheWirv just made a change on your pr. Can you please double check that everything still works?

@TheWirv
Copy link
Contributor

TheWirv commented Feb 11, 2024

Yep, works perfectly fine still 👌🏼

@fnando
Copy link
Owner

fnando commented Feb 11, 2024

Just released v4.4.1 with the fix for this issue. Thank you all for the fast turnaround. :)

@TheWirv
Copy link
Contributor

TheWirv commented Feb 11, 2024

Thank you too for the fast review and merge! :)

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

Successfully merging a pull request may close this issue.

3 participants