You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
ts-node gives type errors, presumably doesn't load .d.ts file, when allowJs && checkJs #1421
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 }) {
I read in the FAQ that I need to fiddle with --files option to make ts-node read global types.
But here there are two separate packages. myapp-bar has myapp-foo in the dependencies section of its package.json.
Does anyone know why allowJs and checkJs make a difference though?
It makes sense that when checkJs is false, typechecking the JS files are skipped.
But why doesn't it give errors when allowJs is false? Does the presence of .d.ts files make the JS file equivalent to a TS file for typechecking?
Then why are they not considered equivalent when allowJs and checkJs are both true?
@itsfarseen I think this error is unfortunately valid. The problem isn't within myapp-bar, but in myapp-foo since you enabled checkJs. Note the file that is erroring, it is not part of tsc when you run it. If you do run tsc, with the same configuration in myapp-foo, you'll see that same error.
As for solutions, I don't have a good one right now. In theory the scope option should be used to avoid this problem but I wasn't able to get it working right now.
Edit: To clarify it's not related to files or type resolution or anything else, just due to literally applying the tsconfig.json on a JavaScript file you didn't expect.
I'm not really an expert on TypeScript when it comes to the JS features, but based on this I believe it's basically doing type-checking on your JavaScript (almost) as if it's TypeScript.
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
Activity
cspotcode commentedon Aug 7, 2021
Might need to adjust
ts-node's ignore option to ignoremyapp-fooor enable ts-node'sfilesoption.itsfarseen commentedon Aug 8, 2021
I read in the FAQ that I need to fiddle with
--filesoption to make ts-node read global types.But here there are two separate packages.
myapp-barhasmyapp-fooin thedependenciessection of itspackage.json.Does anyone know why
allowJsandcheckJsmake a difference though?It makes sense that when
checkJsis false, typechecking the JS files are skipped.But why doesn't it give errors when
allowJsis false? Does the presence of.d.tsfiles make the JS file equivalent to a TS file for typechecking?Then why are they not considered equivalent when
allowJsandcheckJsare both true?blakeembrey commentedon Aug 9, 2021
@itsfarseen I think this error is unfortunately valid. The problem isn't within
myapp-bar, but inmyapp-foosince you enabledcheckJs. Note the file that is erroring, it is not part oftscwhen you run it. If you do runtsc, with the same configuration inmyapp-foo, you'll see that same error.As for solutions, I don't have a good one right now. In theory the
scopeoption should be used to avoid this problem but I wasn't able to get it working right now.Edit: To clarify it's not related to
filesor type resolution or anything else, just due to literally applying thetsconfig.jsonon a JavaScript file you didn't expect.blakeembrey commentedon 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:
You can also add this to your
tsconfig.jsontoo:itsfarseen commentedon Aug 9, 2021
@blakeembrey could you explain what is happening under the hood when
checkJsis enabled?blakeembrey commentedon Aug 9, 2021
I'm not really an expert on TypeScript when it comes to the JS features, but based on this I believe it's basically doing type-checking on your JavaScript (almost) as if it's TypeScript.
cspotcode commentedon Aug 9, 2021
cspotcode commentedon Oct 6, 2021
As far as I can tell, this has been solved so I'm going to close it. Please comment if the issue persists and if you think it's a ts-node bug.