Skip to content

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

Closed

Description

@itsfarseen

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

Activity

  1. cspotcode commented on Aug 7, 2021

    @cspotcode
    Collaborator

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

  2. itsfarseen commented on Aug 8, 2021

    @itsfarseen
    Author

    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?

  3. blakeembrey commented on Aug 9, 2021

    @blakeembrey
    Member

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

  4. blakeembrey commented on Aug 9, 2021

    @blakeembrey
    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
    }
  5. itsfarseen commented on Aug 9, 2021

    @itsfarseen
    Author

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

  6. blakeembrey commented on Aug 9, 2021

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

  7. cspotcode commented on Aug 9, 2021

    @cspotcode
    Collaborator
  8. cspotcode commented on Oct 6, 2021

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions