question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

ts-node gives type errors, presumably doesn't load .d.ts file, when allowJs && checkJs

See original GitHub issue

Expected Behavior

Code compiles.

Actual Behavior

Gives type error, saying variable implicitly has an any type.

Steps to reproduce the problem

  1. Create a library with type declarations:
    // foo.js
    export function foo({ foo }) {
       console.log(`Inside foo({foo:${foo}})`);
    }
    // foo.d.ts
    export declare function foo({ foo }: {
       foo: number;
    }): void;
    // package.json
    {
      "name": "foo",
      "version": "1.0.0",
      "main": "foo.js",
      "types": "foo.d.ts",
      "type": "module",
      "license": "MIT"
    }
    
  2. Create a TS project, set allowJs, checkJs to true.
  3. import the library and call foo({foo:100})
  4. Run tsc, it compiles without any error.
  5. Run node --loader ts-node/esm main.ts

Output:

TSError: ⨯ Unable to compile TypeScript:
../myapp-foo/foo.js:1:23 - error TS7031: Binding element 'foo' implicitly has an 'any' type.

1 export function foo({ foo }) {

Minimal reproduction

https://github.com/itsfarseen/ts-node-repro

Specifications

ts-node v10.1.0
node v16.5.0
compiler v4.3.5

tsconfig.json: https://github.com/itsfarseen/ts-node-repro/blob/main/myapp-bar/tsconfig.json

Operating system and version: Arch Linux

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:8 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
cspotcodecommented, Aug 9, 2021

Whenever you require a file and ts-node needs to compile it, the file is manually added to the “files” array as needed to ensure TypeScript looks at it and emits compiled code.

I suspect this is happening: tsc will not look at the .js file because it has instead resolved to the .d.ts file. ts-node, on the other hand, has seen a require() which resolved to the js file, so it has forced the .js file to be included in your “files” array.

tsc --listFiles will show the files pulled into compilation. tsc –traceResolutions will explain why they are pulled into compilation.

*Above, “files” array refers to the array in tsconfig.json

On Mon, Aug 9, 2021, 1:50 AM Blake Embrey @.***> wrote:

I’m not really an expert on TypeScript when it comes to the JS features, but based on this https://www.typescriptlang.org/docs/handbook/intro-to-js-ts.html I believe it’s basically doing type-checking on your JavaScript (almost) as if it’s TypeScript.

— You are receiving this because you commented. Reply to this email directly, view it on GitHub https://github.com/TypeStrong/ts-node/issues/1421#issuecomment-894966816, or unsubscribe https://github.com/notifications/unsubscribe-auth/AAC35OA3MJBHWY273PE5AL3T35UD7ANCNFSM5BXNYTRQ .

1reaction
blakeembreycommented, Aug 9, 2021

Update: I was using the scope option wrong (trying to give it a directory instead of a boolean). This works for me:

TS_NODE_SCOPE=true node --loader ts-node/esm main.ts

You can also add this to your tsconfig.json too:

"ts-node": {
  "scope": true
}
Read more comments on GitHub >

github_iconTop Results From Across the Web

ts-node ignores d.ts files while tsc successfully compiles the ...
ts files I created (while tsc has no problem with it). Project structure is: / conf/ dist/ src/ types/ package.json tsconfig.json.
Read more >
8.0.3 breaks project with: Unable to require .d.ts file #797
Hey, today I upgraded the ts-node package to the newest version 8.0.3 and it now throws an exception: TypeError: Unable to require `.d.ts` ......
Read more >
TypeScript Node - ts-node - npm
Start using ts-node in your project by running `npm i ts-node`. ... ts-node. TypeScript icon, indicating that this package has built-in type ......
Read more >
*.d.ts vs *.ts - this vs that - in the front-end development?
d.ts is the type definition files that allow to use existing JavaScript code in TypeScript. For example, we have a simple JavaScript function...
Read more >
Options | ts-node
json , or cwd if no tsconfig.json is loaded. Environment: TS_NODE_SCOPE_DIR. moduleTypes​. Override the module type of certain files, ignoring the package.json ...
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