Warning: Cannot find parent tsconfig.json

左心房为你撑大大i 提交于 2019-12-09 14:14:22

问题


I would like to fix the warning:

Warning: Cannot find parent tsconfig.json

in the TypeScript Errors tab in IntelliJ IDEA 2016.3. My TypeScript code lives in the src directory and my TypeScript output is going to lib as expected without the src folder being added to lib.

I consume the lib folder in other projects and it seems to work as expected. So this doesn't seem to be a big problem, but I occasionally have a problem with TSLint where it sometimes does not seem to pick up a .tsx file is JSX and lints incorrectly and seems to occasionally treat it as a normal .ts file. Eventually it seems to figure it out. I am wondering if that is related as my TSLint settings are configured to use tsconfig.json.

I have also previously had .js transpiled files turn up next to the .ts files in the src folder, but not since I modified my tsconfig.json recently.

Files as follows:

tsconfig.json
src/index.ts
lib/index.js
lib/index.d.ts

I've upgraded to TypeScript 2.1.4, but was seeing it with 2.0.10.

My tsconfig.json file:

{
  "compilerOptions": {
    "target": "es5",
    "module": "commonjs",
    "moduleResolution": "node",
    "jsx": "react",
    "allowJs": false,
    "isolatedModules": false,
    "experimentalDecorators": true,
    "emitDecoratorMetadata": true,
    "declaration": true,
    "noImplicitAny": false,
    "noImplicitUseStrict": true,
    "noEmitHelpers": false,
    "removeComments": true,
    "noLib": false,
    "sourceMap": true,
    "inlineSources": true,
    "preserveConstEnums": true,
    "allowSyntheticDefaultImports": true,
    "suppressImplicitAnyIndexErrors": true,
    "rootDir": "./src",
    "outDir": "./lib"
  },
  "include": [
    "./src/**/*"
  ],
  "compileOnSave": true,
  "atom": {
    "rewriteTsconfig": false
  }
}

回答1:


problem is specific to TypeScript version being used (2.1.x); it is fixed in 2016.3. 2 EAP

Note: this answer refers to the issue specific to IDE (WebStorm, PHPStorm, IDEA) version 2016.3: it didn't work well with TypeScript 2.1.x, showing false warnings. If you see similar message (Cannot find parent tsconfig.json) in other IDE versions, it is likely a problem with your configuration: such messages are displayed if currently edited .ts file is not included in any tsconfig.json.




回答2:


Try setting up include section in file tsconfig.json like in below image.
(Note that my project's root folder is frontend and my include section is frontend/**/*).

Of course you don't need to rename your project's root folder to frontend.
Only the naming should match.

It should take effect immediately after you save the tsconfig.json and open a .ts or .tsx file. If it doesn't try restarting the WebStorm/IDEA.

And don't forget to clear the error console before testing this solution. It might cache the previous messages.

P/S: I'm using WebStorm 2016.3.1.


If you were using webpack together with ts-loader the above solution would cause your builds to fail. If that was the case consider this approach instead.




回答3:


None of the above solutions worked for me, but I did stumble onto a tsconfig change that did. It seems the IDE is using different rules that tsc.

This did NOT work:

"include": ["./typings", "./src", "./test"],

This did work:

"include": ["./typings/**/*", "./src/**/*", "./test/**/*"],



回答4:


Try to set the "version number" to your tsconfig.json file.

{
    "version": "2.1.4",
    "compilerOptions": {
        "target": "es5",
        "module": "commonjs",
        "moduleResolution": "node",
        "jsx": "react",
        "allowJs": false,
        "isolatedModules": false,
        "experimentalDecorators": true,
        "emitDecoratorMetadata": true,
        "declaration": true,
        "noImplicitAny": false,
        "noImplicitUseStrict": true,
        "noEmitHelpers": false,
        "removeComments": true,
        "noLib": false,
        "sourceMap": true,
        "inlineSources": true,
        "preserveConstEnums": true,
        "allowSyntheticDefaultImports": true,
        "suppressImplicitAnyIndexErrors": true,
        "rootDir": "./src",
        "outDir": "./lib"
    },
    "include": [
        "./src/**/*"
    ],
    "compileOnSave": true,
    "atom": {
        "rewriteTsconfig": false
    }
}


来源:https://stackoverflow.com/questions/41072254/warning-cannot-find-parent-tsconfig-json

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!