【问题标题】:How do I fix TSC errors when using vite?使用 vite 时如何修复 TSC 错误?
【发布时间】:2022-12-13 08:19:10
【问题描述】:

我在 Typescript React 项目中使用 Vite。我的项目中也有开玩笑的测试。

当我运行vite build时,它似乎正在编译和捆绑我的测试文件。我有一些存根测试,我计划立即进行一些编译错误。

当我运行 yarn build 时的示例 tsc && vite build

> yarn build
yarn run v1.22.17
warning package.json: No license field
$ tsc && vite build
src/core/utils/numberUtils.spec.ts:1:1 - error TS1208: 'numberUtils.spec.ts' cannot be compiled under '--isolatedModules' because it is considered a global script file. Add an import, export, or an empty 'export {}' statement to make it a module.

1 describe('numberUtils - ', () => {
  ~~~~~~~~


Found 1 error.

error Command failed with exit code 2.
info Visit https://yarnpkg.com/en/docs/cli/run for documentation about this command.

再仔细看一下,当我运行tsc 时,实际上是 Typescript 抛出了错误,尽管我仍然不确定 Vite 是否捆绑了它。

使用vite时如何修复tsc错误?

【问题讨论】:

  • 多看一点,实际上是 Typescript 在我运行 tsc 时抛出错误,尽管我仍然不确定 Vite 是否捆绑了它。
  • 您找到问题的答案了吗?

标签: vite tsc


【解决方案1】:

如您所见,错误来自tsc,而不是vite build。 Vite 并没有试图捆绑你的测试文件,它是试图对它们进行类型检查的打字稿。由于 tsc 命令因错误而失败,vite build 甚至从未运行过。

Typescript 让你忽略匹配模式的文件:How to exclude files ending in '.spec.ts' in tsconfig.json

tsconfig.json文件

"exclude": [
    "**/*.spec.ts"
]

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2022-11-13
    • 2019-12-10
    • 1970-01-01
    • 2021-05-29
    • 1970-01-01
    • 2023-03-19
    • 2021-12-31
    • 2021-11-17
    相关资源
    最近更新 更多