Error when building a file that imports .d.ts file (no .ts file)

See original GitHub issue

Current Behavior

TSDX (probably Rollup behind the scenes) struggles with building a src/index.ts file that imports generated types. Here’s a repo with a reproduction: https://github.com/pcowgill/tsdx-typechain/

This line - import { Greeter } from "../typechain/Greeter"; - isn’t working.

Greeter is a .d.ts file.

I might need to configure Rollup via TSDX, there might be a simple change to the tsconfig.json that can address it, or maybe something else needs to be done.

Error: Could not resolve '../typechain/Greeter' from src/index.ts

    at error (/Users/paulcowgill/Code/tsdx-typechain/node_modules/rollup/dist/shared/node-entry.js:5400:30)

    at ModuleLoader.handleResolveId (/Users/paulcowgill/Code/tsdx-typechain/node_modules/rollup/dist/shared/node-entry.js:12410:24)

    at ModuleLoader.<anonymous> (/Users/paulcowgill/Code/tsdx-typechain/node_modules/rollup/dist/shared/node-entry.js:12298:30)

    at Generator.next (<anonymous>)

    at fulfilled (/Users/paulcowgill/Code/tsdx-typechain/node_modules/rollup/dist/shared/node-entry.js:38:28)

Expected behavior

tsdx build completes and uses the .d.ts type files I imported in src/index.ts.

Suggested solution(s)

I’m not sure if I need a rollup plugin, a different set of choices in tsconfig.json, or something else. If it’s a rollup plugin I need, then just some docs for TSDX would be the change to make. If it’s a tsconfig.json change, then maybe the default TSDX tsconfig.json could be different to accommodate this type of usage too while still supporting the existing use cases.

Additional context

I used TypeChain to generate the types since it’s an Ethereum-related project, but I don’t think you need to worry about how the .d.ts types were generated to debug this issue. The type I’m trying to import is here https://github.com/pcowgill/tsdx-typechain/blob/primary/typechain/Greeter.d.ts

Your environment

Software Version(s)
TSDX 0.13.2
TypeScript 3.9.5
Browser n/a
npm/Yarn npm 6.14.4
Node 12.18.0
Operating System macOS 10.15.5

Thanks!!

Issue Analytics

  • State:open
  • Created 3 years ago
  • Comments:16

github_iconTop GitHub Comments

2reactions
pcowgillcommented, Jun 23, 2020

@agilgur5 tsdx builds successfully when using the import type syntax rather than just import.

https://github.com/ethereum-ts/TypeChain/issues/247#issuecomment-648139358

1reaction
agilgur5commented, Jun 13, 2022

Thanks those details are helpful.

Currently in Project 1 tsdx build works fine and i’m able to publish the npm package and pull it in Project 2 and make use of MyContractFactory.ts but any functions in MyContractFactory.ts that return types declared in MyContract.d.ts get returned as type any

Gotcha, yea I’m pretty sure that’s due to #113, which is actually TS’s own behavior – so TSDX and rpt2 don’t diverge on that (as of right now). You’ll need to copy over any declaration files into your dist/ folder (in the right location) so that they exist after publish as well. Or have typechain output to the dist/ folder initially. Basically declaration files are already compiled artifacts so TS doesn’t transform them in any way when compiling, it just references them for type-checking. So you have to manually copy those if you want your consumers to be able to use them too.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Why does d.ts files do not throw an error when importing ...
It was reported on the Typescript Github page here: Import path not checked when not importing any symbols #20534 and labeled "working as ......
Read more >
Could not find declaration file for module 'X' Error | bobbyhadz
The error "Could not find declaration file for module" occurs when TypeScript cannot find the type declaration for a module. To solve the...
Read more >
Documentation - tsc CLI Options - TypeScript
Flag Type Default ‑‑allowJs boolean false ‑‑allowUmdGlobalAccess boolean false ‑‑allowUnreachableCode boolean
Read more >
Module Resolution - TypeScript
A relative import is resolved relative to the importing file and cannot resolve to an ambient module declaration. You should use relative imports...
Read more >
TypeScript errors and how to fix them
ts ' cannot be compiled under '–isolatedModules' because it is considered a global script file. Add an import, export, or an empty 'export...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found