简体   繁体   English

当 js 文件中不存在模块时,为什么 VS Code 不会抛出“找不到模块 'typescript'.ts(2307)”?

[英]Why does VS Code NOT throw "Cannot find module 'typescript'. ts(2307)" when the module is NOT there in js files?

I am using VScode with several react apps.我正在将 VScode 与几个反应应用程序一起使用。 For one of my projects, in the .js files I do not get the "Cannot find module 'typescript'. ts(2307)" alert on import something from './an/incorrect/path' lines, where I would expect that ts2307 alert.对于我的一个项目,在 .js 文件中,我在import something from './an/incorrect/path'没有收到“找不到模块'typescript'.ts(2307)”警报,我希望ts2307 警报。

However I do get that alert in .ts files that import from incorrect paths.但是,我确实在从错误路径导入的 .ts 文件中收到该警报。

How do I get those alerts to appear as well in .js files?如何让这些警报也出现在 .js 文件中?

I've checked that both of these settings are set to true in VScode settings.json.我检查了这两个设置在 VScode settings.json 中都设置为 true。 You want to change these two settings in VS Code's settings.json:您想在 VS Code 的 settings.json 中更改这两个设置:

"typescript.validate.enable": true,
"javascript.validate.enable": true,

This is my tsconfig file.这是我的 tsconfig 文件。

{
    "compilerOptions": {
        "target": "ES5",
        "module": "commonjs",
        "lib": ["es2017", "dom"],
        "allowJs": true,
        "jsx": "react",
        "noImplicitAny": false,
        "noFallthroughCasesInSwitch": true,
        "moduleResolution": "node",
        "allowSyntheticDefaultImports": true,
        "esModuleInterop": true,
        "skipLibCheck": true,
        "outDir": "lib"
    },
    "include": ["./**/*"]
}

I just found the answer, right after I considered that typescript checking might be entirely disabled for .js files.我刚刚找到了答案,就在我认为可能对 .js 文件完全禁用打字稿检查之后。 Then I found I had to add "checkJs": true to the compilerOptions in tsconfig.json.然后我发现我必须在 tsconfig.json 中的 compilerOptions 中添加"checkJs": true

ts2307 are now correctly shown when trying to import from incorrect file paths.尝试从不正确的文件路径导入时,现在可以正确显示 ts2307。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 找不到模块 'react'.ts(2307) - Cannot find module 'react'.ts(2307) 打字稿智能感知和 TS2307 (TS) 的 Visual Studio 设置找不到模块杂项 - visual studio settings for typescript intellisense and TS2307 (TS) Cannot find module Miscellaneous Typescript React / NestJS 应用程序失败 @Heroku 构建 - “错误 TS2307:找不到模块” - Typescript React / NestJS app fails @Heroku build - “error TS2307: Cannot find module” 错误TS2307:找不到模块'反应' - error TS2307: Cannot find module 'react' tslint错误:找不到模块'redux / todo / actions'.ts(2307) - tslint error: Cannot find module 'redux/todo/actions'.ts(2307) 将 PNG/SVG 导入 React - TS2307:找不到模块 - Importing PNGs/SVGs into React - TS2307: Cannot find module 导入空类时出错,错误TS2307:找不到模块“菜单” - Error importing empty class, error TS2307: Cannot find module 'menu' TS2307:找不到模块 '_rc-trigger@5.2.0@rc-trigger' 或其对应的类型声明 - TS2307: Cannot find module '_rc-trigger@5.2.0@rc-trigger' or its corresponding type declarations 找不到模块 '../../images/Logo.png'.ts(2307) 红色错误仍在显示 - Cannot find module '../../images/Logo.png'.ts(2307) Red Error is Still showing 找不到模块 './components/Table/Table' 或其对应的类型声明。 ts(2307) - Cannot find module './components/Table/Table' or its corresponding type declarations. ts(2307)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM