如果未包含在tspl.json中,则Typescript文件会出错

nimxete2  于 6个月前  发布在  TypeScript
关注(0)|答案(1)|浏览(71)

我的项目中有以下文件结构:

- root
  - build
     - index.js
     - other files
  - src
     - index.ts
     - other files
  - postinstall.ts
  - tsconfig.json

字符串
我想compilie只src文件夹内的文件到build目录,不想编译postinstall.ts文件,因为它只在我的项目中安装一个新的软件包后使用。
如果我不添加postinstall.tsinclude节在tsconfig.json我可以看到错误,当我打开文件.这里有一些错误我克:

Cannot find module 'fs' or its corresponding type declarations.ts(2307)
Cannot find module 'path' or its corresponding type declarations.ts(2307)
Cannot find name '__dirname'.ts(2304)


如果我在include部分添加它,那么postinstall.ts上的所有错误都消失了,但现在我在tsconfig.json中有错误'rootDir' is expected to contain all source files.
以下是我的tsconfig.json设置:

{
  "compilerOptions": {
    "target": "esnext",
    "skipLibCheck": true,
    "strict": true,
    "forceConsistentCasingInFileNames": true,
    "esModuleInterop": true,
    "module": "commonjs",
    "resolveJsonModule": true,
    "isolatedModules": true,
    "rootDir": "./src",
    "removeComments": true,
    "typeRoots": [
      "./node_modules/@types",
      "./src/types"
    ],
    "noUnusedLocals": true,
    "noImplicitReturns": true,
    "sourceMap": true,
    "baseUrl": "./src",
    "paths": {
      "@/*": [
        "./*"
      ]
    },
    "outDir": "build",
    "experimentalDecorators": true,
    "strictPropertyInitialization": false
  },
  "tsc-alias": {
    "resolveFullPaths": true,
    "verbose": false
  },
  "compileOnSave": true,
  "include": [
    "src"
  ],
  "exclude": [
    "node_modules"
  ]
}


我该怎么解决这个问题?谢谢

fdbelqdn

fdbelqdn1#

您可以为postinstall.ts定义另一个TS Config文件,您可以将其称为tsconfig.node.json(就像Vitevite.config.ts所做的那样):

{
  "compilerOptions": {
    "composite": true,
    "skipLibCheck": true,
    "module": "ESNext",
    "moduleResolution": "bundler",
    "allowSyntheticDefaultImports": true
  },
  "include": ["postinstall.ts"]
}

字符串
然后,您可以通过以下命令将其与Typescript CLI一起使用:

tsc --project tsconfig.node.json

相关问题