i18next / i18next-browser-languageDetector

language detector used in browser environment for i18next
MIT License
865 stars 88 forks source link

Types entries missing in package exports (Needed for Typescript >= 4.7 and moduleResolution="Node16"/"Nodenext") #266

Closed gunters63 closed 2 years ago

gunters63 commented 2 years ago

šŸ› Bug Report

types entries missing in package exports

To Reproduce

For projects using Typescript >= 4.7 AND Node resolution Node16/Nodenext the types fail to load with the message:

 Could not find a declaration file for module 'i18next-browser-languagedetector'

This can be reproduced with a minimal project using Typescript >= 4.7 and "moduleResolution": "Node16", in the tsconfig.json configuration file.

Expected behavior

Typescript types should load.

The cause seems to be that the types property is missing from the module exports:

  "exports": {
    "./package.json": "./package.json",
    ".": {
      "require": "./dist/cjs/i18nextBrowserLanguageDetector.js",
      "default": "./dist/esm/i18nextBrowserLanguageDetector.js"
    },
    "./cjs": {
      "default": "./dist/cjs/i18nextBrowserLanguageDetector.js"
    },
    "./esm": {
      "default": "./dist/esm/i18nextBrowserLanguageDetector.js"
    }
  },

The correct ones would be:

  "exports": {
    "./package.json": "./package.json",
    ".": {
      "require": "./dist/cjs/i18nextBrowserLanguageDetector.js",
      "default": "./dist/esm/i18nextBrowserLanguageDetector.js",
      "types": "./index.d.ts"
    },
    "./cjs": {
      "default": "./dist/cjs/i18nextBrowserLanguageDetector.js",
      "types": "./index.d.ts"
    },
    "./esm": {
      "default": "./dist/esm/i18nextBrowserLanguageDetector.js",
      "types": "./index.d.ts"
    }
  },

The reason seems to be that the Typescript compiler (starting from 4.7) uses the exports fields to find the type declaration files and there is no fall back to the package.json "types" property when they are missing.

See also: https://devblogs.microsoft.com/typescript/announcing-typescript-4-7/#esm-nodejs

adrai commented 2 years ago

Would you like to send a Pull Request to address this?

adrai commented 2 years ago

just released v6.1.7

gunters63 commented 2 years ago

That fixed the problem! Thanks :)

adrai commented 2 years ago

https://github.com/i18next/i18next-browser-languageDetector/issues/267

gunters63 commented 2 years ago

Yes, sorry. Missed that before.

The types entry should be the first one in the exports.

gunters63 commented 2 years ago

When I hacked directly in the package.json it worked, but other tools insist on the correct order I suppose :)