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

Error reported only in node_modules directory #58128

Closed
regseb opened this issue Apr 9, 2024 · 3 comments
Closed

Error reported only in node_modules directory #58128

regseb opened this issue Apr 9, 2024 · 3 comments
Labels
Working as Intended The behavior described is the intended behavior; this is not a bug

Comments

@regseb
Copy link

regseb commented Apr 9, 2024

πŸ”Ž Search Terms

TS7016, node_modules

πŸ•— Version & Regression Information

  • This is the behavior in every version I tried, and I reviewed the FAQ for entries about node_modules

⏯ Playground Link

No response

πŸ’» Code

.
β”œβ”€ package.json
β”œβ”€ index.js
└─ not_node_modules/
 Β  └─ sub/
 Β     β”œβ”€ source.js
 Β     └─ type.d.ts
  • package.json

    {
      "name": "testcase",
      "version": "1.0.0",
      "devDependencies": {
        "typescript": "5.4.4"
      }
    }
  • index.js

    /**
     * @type {import("./node_modules/sub/type.d.ts")}
     */
    const baz = true;
    
    /**
     * @type {import("./not_node_modules/sub/type.d.ts")}
     */
    const qux = true;
    
    // Use variable to remove "'xxx' is declared but its value is never read."
    console.log(baz, qux);
  • not_node_modules/

    • not_node_modules/sub/
      • not_node_modules/sub/source.js

        export default {
            BAR: 0,
        };
      • not_node_modules/sub/type.d.ts

        import type Foo from "./source.js";
        
        export type keyofFoo = (typeof Foo)[keyof typeof Foo];
        export default keyofFoo;
  1. Unzip testcase.zip
  2. cd testcase/
  3. npm install
  4. cp -r not_node_modules/sub/ node_modules/
  5. npx tsc --noEmit --checkJs --strict index.js

πŸ™ Actual behavior

node_modules/sub/type.d.ts:1:22 - error TS7016: Could not find a declaration file for module './source.js'. '/home/regseb/testcase/node_modules/sub/source.js' implicitly has an 'any' type.

1 import type Foo from "./source.js";
                       ~~~~~~~~~~~~~


Found 1 error in node_modules/sub/type.d.ts:1

πŸ™‚ Expected behavior

No error. Or 2 errors with node_modules/sub/type.d.ts and not_node_modules/sub/type.d.ts

Additional information about the issue

The two imported directories (not_node_modules/sub/type.d.ts and node_modules/sub/type.d.ts) are identical, but TypeScript only reports an error for the file in the node_modules directory.


I have this problem in my projects. TypeScript doesn't report an error in Metalint (the sub-project). But when I import it into Cast Kodi, TypeScript reports the error.

@andrewbranch andrewbranch added the Working as Intended The behavior described is the intended behavior; this is not a bug label Apr 9, 2024
@andrewbranch
Copy link
Member

TypeScript analyzes JavaScript files for type info under allowJs, but this behavior is disabled by default inside node_modules for performance reasons. You can change it via maxNodeModulesJsDepth.

@regseb
Copy link
Author

regseb commented Apr 11, 2024

Thanks @andrewbranch for the explanation. There are no more errors with the: npx tsc --noEmit --checkJs --strict index.js --maxNodeModuleJsDepth 2

But I can't ask users of my application to add the maxNodeModuleJsDepth option to their tsconfig.json file. Is there a best practice for this problem?

@typescript-bot
Copy link
Collaborator

This issue has been marked as "Working as Intended" and has seen no recent activity. It has been automatically closed for house-keeping purposes.

@typescript-bot typescript-bot closed this as not planned Won't fix, can't repro, duplicate, stale Apr 14, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Working as Intended The behavior described is the intended behavior; this is not a bug
Projects
None yet
Development

No branches or pull requests

3 participants