ts-node gives type errors, presumably doesn't load .d.ts file, when allowJs && checkJs
See original GitHub issueExpected Behavior
Code compiles.
Actual Behavior
Gives type error, saying variable implicitly has an any
type.
Steps to reproduce the problem
- 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" }
- Create a TS project, set allowJs, checkJs to true.
- import the library and call
foo({foo:100})
- Run
tsc
, it compiles without any error. - 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:
- Created 2 years ago
- Comments:8 (3 by maintainers)
Top 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 >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
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:
Update: I was using the scope option wrong (trying to give it a directory instead of a boolean). This works for me:
You can also add this to your
tsconfig.json
too: