denoland / denoland/dnt

Type checking ESM with mapped package fails to see types within package

Open
#331 0 comments 0 reactions 0 assignees View on GitHub
Dominant language
Rust
Stars
1.3k
Forks
49
PR merge metrics
No merged PRs in 30d

Description

My project uses [@hexagon/base64](https://github.com/Hexagon/base64), which cross-publishes to deno.land as [b64](https://deno.land/x/b64@1.1.27). I'm experimenting with migrating my Node library to use dnt so I can start cross-publishing as well, and so I updated my import to point to deno.land:

```
import base64 from "https://deno.land/x/b64@1.1.27/src/base64.js";
```

Which I then map to the NPM equivalent in my dnt build script:

```ts
mappings: {
'https://deno.land/x/b64@1.1.27/src/base64.js': {
name: '@hexagon/base64',
version: '^1.1.25'
}
},
```

When I run the build script, the export maps over as expected:

```ts
import base64 from "@hexagon/base64";
```

BUT dnt fails to build when it tries to type check the ESM it just output:

```
deno task build
Task build deno run -A build_npm.ts 0.0.1
[dnt] Transforming...
[dnt] Running npm install...

added 2 packages, and audited 3 packages in 849ms

found 0 vulnerabilities
[dnt] Building project...
[dnt] Type checking ESM...
src/helpers/iso/isoBase64URL.ts:1:20 - error TS7016: Could not find a declaration file for module '@hexagon/base64'. '/Users/matt/Developer/simplewebauthn/packages/server/npm/node_modules/@hexagon/base64/dist/base64.cjs' implicitly has an 'any' type.
Try `npm i --save-dev @types/hexagon__base64` if it exists or add a new declaration (.d.ts) file containing `declare module '@hexagon/base64';`

1 import base64 from "@hexagon/base64";
~~~~~~~~~~~~~~~~~

error: Uncaught (in promise) Error: Had 1 diagnostics.
throw new Error(`Had ${diagnostics.length} diagnostics.`);
^
at getProgramAndMaybeTypeCheck (https://deno.land/x/dnt@0.38.0/mod.ts:456:17)
at build (https://deno.land/x/dnt@0.38.0/mod.ts:345:17)
at eventLoopTick (ext:core/01_core.js:183:11)
at async file:///Users/matt/Developer/simplewebauthn/packages/server/build_npm.ts:7:1
```

The **@hexagon/base64** package includes its own **.d.ts** file, though, that has all the type declarations:

Screenshot 2023-08-16 at 9 59 18 AM

It doesn't seem possible to get dnt to see these types. My project currently gets TypeScript to see these types by including `"./node_modules/@types"` in **tsconfig.json**'s `typeRoots` (which, I know, doesn't really make sense since @hexagon/base64's types aren't actually in **node_modules/@types/** but it works 🤷‍♂️), but `typeRoots` isn't a valid value for dnt's `compilerOptions`:

Screenshot 2023-08-16 at 10 03 48 AM

I tried adding `/// ` above my deno.land import in the source file, but it just gets stripped out by dnt so that doesn't seem to be an option either.

Any idea how I can get ESM type checking to see this package's types?

Contributor guide

Open the contributing guide

Research direction

Start with build_npm.ts and the generated ESM for src/helpers/iso/isoBase64URL.ts, then inspect dnt 0.38.0's ESM type-checking path and the npm mapping behavior. Reproduce the build with @hexagon/base64 and verify that its bundled declaration file is discovered without relying on typeRoots; done means the build completes with the mapped package correctly typed.

Written by the indexing model from the issue text.

Assessment

Tech stack
deno, node.js, typescript
Domain
build-system, tooling
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.