casper-ecosystem / casper-js-sdk

TypeScript Casper Client SDK
Apache License 2.0
71 stars 60 forks source link

Library cannot be used with modern moduleResolution TypeScript settings #386

Closed akwodkiewicz closed 5 months ago

akwodkiewicz commented 5 months ago

Description

It is not possible to import any type exported internally from the lib directory when your TS project is set to any of the modern values of moduleResolution: "Node16", "NodeNext" or "Bundler"

Explanation

The reason for that is filename clash between ./dist/lib.js and ./dist/lib/ directory -- modern resolution algorithms resolve the './lib' in

export * from './lib'

in the ./dist/index.d.ts file to the ./dist/lib.js , not ./dist/lib/index.d.ts, because that's how the Node resolution algorithm works (lib.js has a higher precedence than lib/index.js).

The quickest way to resolve the issue is to change the lib.js filename to something like lib.web.js, so that there is no confusion between the file name and the directory name.

Reproduction

Prerequisites

Steps to reproduce

  1. Try to import things like
import { CasperClient, CLPublicKey, DeployUtil } from 'casper-js-sdk';

Expected behavior

The import is correct.

Actual behavior

TypeScript error 2305:

Module '"casper-js-sdk"' has no exported member 'DeployUtil'

Screenshots

Screenshot 2024-02-05 at 11 15 30 Screenshot 2024-02-05 at 11 15 35

Additional context

The bug does not have any runtime implications, but it's not possible to import the library correctly in projects relying on current TypeScript defaults.

Defect Type

akwodkiewicz commented 5 months ago

I'll try to address that in a PR in a moment

akwodkiewicz commented 5 months ago

A workaround for Node devs is to remove the dist/lib.js file from the package after installation -- Node is going to import dist/lib.node.js, as defined in package.json#node, and TypeScript will start resolving import from ./lib to dist/lib/index.d.ts.

hoffmannjan commented 5 months ago

Fixed in release-2.15.4.