launchdarkly / react-client-sdk

LaunchDarkly Client-side SDK for React.js
Other
86 stars 68 forks source link

Esmodule types regression from 3.0.6 -> 3.0.7 #208

Closed andrew-w-ross closed 1 year ago

andrew-w-ross commented 1 year ago

Is this a support request? No

Describe the bug

In 3.0.7 I get the following error when trying to compile under module resolution bundler.

src/hooks/useLaunchDarkly.tsx:1:40 - error TS7016: Could not find a declaration file for module 'launchdarkly-react-client-sdk'. '/Users/aross/projects/channelape/node_modules/launchdarkly-react-client-sdk/lib/esm/index.js' implicitly has an 'any' type.
  There are types at '/Users/aross/projects/channelape/node_modules/launchdarkly-react-client-sdk/lib/index.d.ts', but this result could not be resolved when respecting package.json "exports". The 'launchdarkly-react-client-sdk' library may need to update its package.json or typings.

1 import { useFlags as useLdFlags } from "launchdarkly-react-client-sdk";

In 3.0.6 your package.json only the main and type properties are defined:

"main": "lib/index.js",
  "types": "lib/index.d.ts",
  "files": [
    "lib",
    "src",
    "!**/*.test.*",
    "!**/__snapshots__"
  ],

In 3.0.7 you've change added the exports field:

"exports": {
    "require": "./lib/cjs/index.js",
    "import": "./lib/esm/index.js"
  },
  "main": "./lib/cjs/index.js",
  "types": "lib/index.d.ts",
  "files": [
    "lib",
    "src",
    "!**/*.test.*",
    "!**/__snapshots__"
  ],

As the error explains you need to add the types to exports:

  "exports": {
    "types": "./lib/index.d.ts",
    "require": "./lib/cjs/index.js",
    "import": "./lib/esm/index.js"
  },

After changing this it'll compile just fine.

To reproduce

I haven't made a simple replication project but I assume trying to import the package with moduleResolution of node16, nodenext or bundler would do the trick.

Expected behavior For the types to be found.

Logs NA

SDK version NA

Language version, developer tools typescript@5.1.6

OS/platform NA

Additional context NA

yusinto commented 1 year ago

Thank you for reporting this issue. We are investigating this and will provide an update soon. Filed internally as 209707.

mellis481 commented 1 year ago

@yusinto Interestingly, I'm not seeing that 3.0.8 fixes this. I have the following references in my code from 3.0.6 which do not resolve in 3.0.8:

import context from 'launchdarkly-react-client-sdk/lib/context';

import { Provider } from 'launchdarkly-react-client-sdk/lib/context';

Instead, I get the following error: Cannot find module 'launchdarkly-react-client-sdk/lib/context' or its corresponding type declarations.

andrew-w-ross commented 1 year ago

@yusinto Thanks that worked.

@mellis481 I don't think that was ever intended to work there is only the single export so nothing other than importing from launchdarkly-react-client-sdk will resolve. Can you not use LDProvider that's exported?

jherdman commented 6 months ago

This is still an issue with 3.0.10