uni-app icon indicating copy to clipboard operation
uni-app copied to clipboard

defineProps和defineEmits不支持导入类型

Open xiaohe0601 opened this issue 10 months ago • 0 comments

Vue 3.3+ 已经支持defineProps和defineEmits使用导入类型,但是在最新版本的uni-app中使用时报错

https://cn.vuejs.org/api/sfc-script-setup.html#type-only-props-emit-declarations

在 3.2 及以下版本中,defineProps() 的泛型类型参数只能使用类型字面量或者本地接口的引用。 这个限制已经在 3.3 版本中解决。最新版本的 Vue 支持在类型参数的位置引用导入的和有限的复杂类型。

<script lang="ts" setup>
import type { Props, Emits } from "./types.ts";

defineProps<Props>();

defineEmits<Emits>();
</script>
// types.ts

export interface Props {
  name?: string;
}

export interface Emits {
  (e: "change", id: number): void;
}

报错信息

[vite] [plugin:vite:vue] [@vue/compiler-sfc] No fs option provided to `compileScript` in non-Node environment. File system access is required for resolving imported types.

12 |  import type { Props, Emits } from "./types.ts";
13 |
14 |  defineProps<Props>();
   |              ^^^^^

版本信息

  • uniapp:3.0.0-4000820240401001
  • vue:3.3.11

xiaohe0601 avatar Apr 03 '24 03:04 xiaohe0601