Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
itsfarseen opened this issue Aug 7, 2021 · 8 comments

Comments

@itsfarseen
Copy link

itsfarseen commented Aug 7, 2021

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

@cspotcode
Copy link
Collaborator

Might need to adjust ts-node's ignore option to ignore myapp-foo or enable ts-node's files option.

@itsfarseen
Copy link
Author

itsfarseen commented Aug 8, 2021

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?

@blakeembrey
Copy link
Member

blakeembrey commented Aug 9, 2021

@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.

@blakeembrey
Copy link
Member

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
}

@itsfarseen
Copy link
Author

@blakeembrey could you explain what is happening under the hood when checkJs is enabled?

@blakeembrey
Copy link
Member

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
Copy link
Collaborator

cspotcode commented Aug 9, 2021 via email

@cspotcode
Copy link
Collaborator

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants