为什么 Vue.js3 `npm run build` 会给出一堆错误: TS2304 “Cannot find name ‘foo’’” & TS2532“对象可能是‘未定义’”?

发布于 2025-01-12 00:04:55 字数 3817 浏览 0 评论 0原文

我使用 TypeScript 完成了一个 Vue.js3 项目。我用 Vue 做的最后一个项目是 Vue.js2,我有一个更简单的设置,没有 vue-tsc。

看来,如果我运行默认的 npm run build 命令,就会调用 vue-tsc --noEmit && vite build,我收到 53 个错误(各种文件中都有多个错误)。

这些错误在开发编译期间永远不会出现,并且在 VS 代码中看不到任何突出显示的问题。在尝试构建之后,我只看到大量错误(可能与我的 Typescript 设置有关?)。

我可以通过从命令中删除 vue-tsc --noEmit 并仅使用:vite build 来解决这个问题。但是,想知道我是否遗漏了任何内容或给我的项目带来任何潜在问题?

代码片段如下:

Vue 应用程序中的 build 配置 package.json

  "scripts": {
    "dev": "vite",
    "build": "vue-tsc --noEmit && vite build",
    "preview": "vite preview --port 5050",
    "test:unit": "vitest --environment jsdom",
    "test:e2e": "start-server-and-test preview http://127.0.0.1:5050/ 'cypress open'",
    "test:e2e:ci": "start-server-and-test preview http://127.0.0.1:5050/ 'cypress run'",
    "typecheck": "vue-tsc --noEmit -p tsconfig.vitest.json --composite false",
    "lint": "eslint . --ext .vue,.js,.jsx,.cjs,.mjs,.ts,.tsx,.cts,.mts --fix --ignore-path .gitignore"
  },

例如,

当我使用 vue-tsc 构建时看到的错误(显示 2)

44         v-model="user.password"
                    ~~~~

src/components/Register.vue:44:18 - error TS2304: Cannot find name 'user'.

44         v-model="user.firstName"
                    ~~~~

src/components/Login.vue:48:25 - error TS2532: Object is possibly 'undefined'.
...
...53 more :(

Register.vue 组件(上面错误标记的组件示例)

注意:同样,这里的 dev 编译没有错误。

<script lang="ts">
import RegisterButton from "./buttons/RegisterButton.vue";
import ValidationErrors from "./ValidationErrors.vue";
export default {
  name: "RegisterUser",
  components: {
    RegisterButton,
    ValidationErrors,
  },
  data() {
    return {
      user: {
        firstName: "",
        lastName: "",
        email: "",
        password: "",
      },
      errors: {},
    };
  },
  methods: {
    onValidationError(errors: Record<string, unknown>) {
      let err = {};
      for (const key in errors) {
        const message = errors[key]["message"];
        err[key] = message;
      }
      this.errors = err;
    },
  },
};
</script>

<template>
  <div class="container-sm mt-3">
    <h1 class="mb-4">Register a New User</h1>
    <ValidationErrors :errors="errors" />
    <!-- Registration Form -->
    <div class="mb-3">
      <input
        type="text"
        class="form-control form-control-lg"
        placeholder="First Name"
        v-model="user.firstName"
      />
    </div>
    <div class="mb-3">
      <input
        type="text"
        class="form-control form-control-lg"
        placeholder="Last Name"
        v-model="user.lastName"
      />
    </div>
    <div class="mb-3">
      <input
        type="email"
        class="form-control form-control-lg"
        placeholder="Email"
        v-model="user.email"
      />
    </div>
    <div class="mb-3">
      <input
        type="password"
        class="form-control form-control-lg"
        placeholder="Password"
        v-model="user.password"
      />
    </div>
    <RegisterButton :user="this.user" @validationError="onValidationError" />
  </div>
</template>

tsconfig.json 设置

{
  "compilerOptions": {
    "baseUrl": "./",
    "paths": {
      "@": ["src/*"],
      "store/*": ["src/store/*"]
    },
    "module": "es2022",
    "moduleResolution": "Node",
  }
}

有人知道我可能缺少什么吗?我的 tsconfig.json 中可能存在错误或缺失的内容吗?

Tl;DR

运行构建命令为 vue-tsc --noEmit && vite build 抛出一堆在开发编译期间不存在的错误。如果我仅通过 vite build 构建,事情就可以了。但是,我是 vue-tsc 的新手,想知道我的设置是否不正确,或者是否缺少某些内容。

I finished a Vue.js3 project using TypeScript. The last project I did with Vue was Vue.js2, and I had a simpler setup without vue-tsc.

It appears that if I run the default npm run build command, that calls vue-tsc --noEmit && vite build, I get 53 errors (multiple in all sorts of files).

These errors are never present during dev compile, and don't see any problems highlighted in VS code. I only see the slough of errors (likely related to my Typescript setup?) following the build attempt.

I was able to work around it, by removing the vue-tsc --noEmit from the command and only use: vite build. However, wonder if I'm missing anything or causing me any potential problems with my project?

Code snippets below:

build config in Vue app package.json

  "scripts": {
    "dev": "vite",
    "build": "vue-tsc --noEmit && vite build",
    "preview": "vite preview --port 5050",
    "test:unit": "vitest --environment jsdom",
    "test:e2e": "start-server-and-test preview http://127.0.0.1:5050/ 'cypress open'",
    "test:e2e:ci": "start-server-and-test preview http://127.0.0.1:5050/ 'cypress run'",
    "typecheck": "vue-tsc --noEmit -p tsconfig.vitest.json --composite false",
    "lint": "eslint . --ext .vue,.js,.jsx,.cjs,.mjs,.ts,.tsx,.cts,.mts --fix --ignore-path .gitignore"
  },

E.g,

Errors I see when I build with vue-tsc (2 shown)

44         v-model="user.password"
                    ~~~~

src/components/Register.vue:44:18 - error TS2304: Cannot find name 'user'.

44         v-model="user.firstName"
                    ~~~~

src/components/Login.vue:48:25 - error TS2532: Object is possibly 'undefined'.
...
...53 more :(

Register.vue component (example of component flagged in error above)

NOTE: again, no errors on dev compile here.

<script lang="ts">
import RegisterButton from "./buttons/RegisterButton.vue";
import ValidationErrors from "./ValidationErrors.vue";
export default {
  name: "RegisterUser",
  components: {
    RegisterButton,
    ValidationErrors,
  },
  data() {
    return {
      user: {
        firstName: "",
        lastName: "",
        email: "",
        password: "",
      },
      errors: {},
    };
  },
  methods: {
    onValidationError(errors: Record<string, unknown>) {
      let err = {};
      for (const key in errors) {
        const message = errors[key]["message"];
        err[key] = message;
      }
      this.errors = err;
    },
  },
};
</script>

<template>
  <div class="container-sm mt-3">
    <h1 class="mb-4">Register a New User</h1>
    <ValidationErrors :errors="errors" />
    <!-- Registration Form -->
    <div class="mb-3">
      <input
        type="text"
        class="form-control form-control-lg"
        placeholder="First Name"
        v-model="user.firstName"
      />
    </div>
    <div class="mb-3">
      <input
        type="text"
        class="form-control form-control-lg"
        placeholder="Last Name"
        v-model="user.lastName"
      />
    </div>
    <div class="mb-3">
      <input
        type="email"
        class="form-control form-control-lg"
        placeholder="Email"
        v-model="user.email"
      />
    </div>
    <div class="mb-3">
      <input
        type="password"
        class="form-control form-control-lg"
        placeholder="Password"
        v-model="user.password"
      />
    </div>
    <RegisterButton :user="this.user" @validationError="onValidationError" />
  </div>
</template>

tsconfig.json setup

{
  "compilerOptions": {
    "baseUrl": "./",
    "paths": {
      "@": ["src/*"],
      "store/*": ["src/store/*"]
    },
    "module": "es2022",
    "moduleResolution": "Node",
  }
}

Does anyone have an idea of what I might be missing? Might there be something incorrect or missing from my tsconfig.json?

Tl;DR

Running build command as vue-tsc --noEmit && vite build throws a bunch of errors that aren't present during dev compiles. If I build only via vite build, things do work. However, I'm new to vue-tsc and wondering if my setup is incorrect, or if I'm missing something.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

一念一轮回 2025-01-19 00:04:55

我也发现了同样的问题,通过将vue-tsc版本升级到0.40.13(最新版本)解决了

I found the same problem, and solved it by upgrading the version of vue-tsc to 0.40.13 (the latest version)

放手` 2025-01-19 00:04:55

从 package.json 的构建行中删除 vue-tsc --noEmit 后,它必须起作用。

"build": "vue-tsc --noEmit && vite build",

一定

"build": "vite build",

适合我的情况我也有一个配置文件,所以:

"build": "vite build --config vite-app.config.ts"

it must work after you remove vue-tsc --noEmit from package.json's build line.

"build": "vue-tsc --noEmit && vite build",

must be

"build": "vite build",

for my case I also have a config file so:

"build": "vite build --config vite-app.config.ts"
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文