我在try 建立一个新项目时遇到了麻烦.我用的是Vue3, typewriter , cypress .而且这个问题可能存在于打字配置中的某个地方.

下面是MWE.如果能帮上一点忙,我会很感激的.


我有这个Vue3组件在src/component/MinimalWorkingExample.vue:

<script setup lang="ts">
interface Props {
  headerText: string;
}

const props = defineProps<Props>();
</script>

<template>
  <div>
    <slot></slot>
  </div>
</template>

<style lang="scss" scoped></style>

我要在src/component/MinimalWorkingExample.cy.ts分钟内对那个部件进行 cypress 测试:

import MinimalWorkingExample from "./MinimalWorkingExample.vue";

describe("<MinimalWorkingExample />", () => {
  it("renders", () => {
    cy.mount(MinimalWorkingExample, {
      props: { headerText: "Testing" },
      slots: { default: "Testing text ..." },
    });
  });
});

我的IDE是红色下划线,cy.mount(MinimalWorkingExample, ...中的MinimalWorkingExample表示No overload matches this call.当我拨打vue-tsc --noEmit时,它抱怨同样的事情,并提供了更多细节:

src/component/MinimalWorkingExample.cy.ts:5:14 - error TS2769: No overload matches this call.
  The last overload gave the following error.
    Argument of type '__VLS_WithTemplateSlots<DefineComponent<__VLS_TypePropsToRuntimeProps<Props>, {}, unknown, {}, {}, ComponentOptionsMixin, ComponentOptionsMixin, ... 5 more ..., {}>, { ...; }>' is not assignable to parameter of type 'ComponentOptionsWithObjectProps<Readonly<ComponentPropsOptions>, {}, {}, {}, {}, ComponentOptionsMixin, ComponentOp
tionsMixin, string[], string>'.
      Type '__VLS_WithTemplateSlots<DefineComponent<__VLS_TypePropsToRuntimeProps<Props>, {}, unknown, {}, {}, ComponentOptionsMixin, ComponentOptionsMixin, ... 5 more ..., {}>, { ...; }>' is not assignable to type 'ComponentOptionsBase<{ [x: number]: unknown; [x: `on${Capitalize<string>}`]: ((...args: any[]) => any) | undefined; readonly length?: numb
er | Prop<unknown, unknown> | null | undefined; readonly concat?: Prop<unknown, unknown> | { (...items: ConcatArray<...>[]): string[]; (...items: (string | ConcatArray<...>)[]): s...'.
        Types of property 'setup' are incompatible.
          Type '((this: void, props: LooseRequired<Readonly<ExtractPropTypes<__VLS_TypePropsToRuntimeProps<Props>>> & {}>, ctx: { ...; }) => void | ... 2 more ... | Promise<...>) | undefined' is not assignable to type '((this: void, props: LooseRequired<{ [x: number]: unknown; [x: `on${Capitalize<string>}`]: ((...args: any[]) => any) | undefined; reado
nly length?: number | Prop<unknown, unknown> | null | undefined; readonly concat?: Prop<unknown, unknown> | { ...; } | null | undefined; ... 27 more ...; readonly toLocaleString: (()...'.
            Type '(this: void, props: LooseRequired<Readonly<ExtractPropTypes<__VLS_TypePropsToRuntimeProps<Props>>> & {}>, ctx: { ...; }) => void | ... 2 more ... | Promise<...>' is not assignable to type '(this: void, props: LooseRequired<{ [x: number]: unknown; [x: `on${Capitalize<string>}`]: ((...args: any[]) => any) | undefined; readonly length?: 
number | Prop<unknown, unknown> | null | undefined; readonly concat?: Prop<unknown, unknown> | { ...; } | null | undefined; ... 27 more ...; readonly toLocaleString: (() ...'.
              Types of parameters 'props' and 'props' are incompatible.
                Property 'headerText' is missing in type 'LooseRequired<{ [x: number]: unknown; [x: `on${Capitalize<string>}`]: ((...args: any[]) => any) | undefined; readonly length?: number | Prop<unknown, unknown> | null | undefined; readonly concat?: Prop<unknown, unknown> | { ...; } | null | undefined; ... 27 more ...; readonly toLocaleString: (()
 => string) & string;...' but required in type 'LooseRequired<Readonly<ExtractPropTypes<__VLS_TypePropsToRuntimeProps<Props>>> & {}>'.

5     cy.mount(MinimalWorkingExample, {
               ~~~~~~~~~~~~~~~~~~~~~

  src/component/MinimalWorkingExample.vue:3:3
    3   headerText: string;
        ~~~~~~~~~~
    'headerText' is declared here.
  node_modules/cypress/vue/dist/index.d.ts:1377:18
    1377 declare function mount<PropsOptions extends Readonly<ComponentPropsOptions>, RawBindings, D extends {}, C extends ComputedOptions = {}, M extends Record<string, Function> = {}, E extends EmitsOptions = Record<string, any>, Mixin extends ComponentOptionsMixin = ComponentOptionsMixin, Extends extends ComponentOptionsMixin = ComponentOptionsMixin
, EE extends string = string>(componentOptions: ComponentOptionsWithObjectProps<PropsOptions, RawBindings, D, C, M, E, Mixin, Extends, EE>, options?: MountingOptions<ExtractPropTypes<PropsOptions> & PublicProps, D>): Cypress.Chainable<{
                          ~~~~~
    The last overload is declared here.


Found 1 error in src/component/MinimalWorkingExample.cy.ts:5

现在是奇怪的事情:

当我用<slot></slot>注释掉该行时,我的IDE停止解压MinimalWorkingExample,vue-tsc --noEmit成功完成,没有输出.

它也很成功,当我保留了<slot></slot>英镑,而摆脱了headerText: string;英镑的房产.

cy.mount(MinimalWorkingExample as any, ...中使用as any也可以解决这个问题.但我想要一份漂亮的Typescript .


More details:

以下是我的tsconfig.json条:

{
  "compilerOptions": {
    "target": "esnext",
    "module": "esnext",
    "lib": [
      "esnext",
      "dom",
      "dom.iterable"
    ],
    "allowJs": false,
    "jsx": "preserve",
    "skipLibCheck": true,
    "esModuleInterop": false,
    "allowSyntheticDefaultImports": true,
    "strict": true,
    "forceConsistentCasingInFileNames": true,
    "moduleResolution": "node",
    "resolveJsonModule": true,
    "noEmit": true,
    "types": [
      "node",
      "vite/client",
      "cypress",
      "./src/shims-vue.d.ts"
    ]
  },
  "include": [
    "src",
    "test/cypress"
  ]
}

推荐答案

使用以下cypress.d.ts可能已修复:

import { ComponentMountingOptions, Vue } from "vue";

declare global {
  namespace Cypress {
    interface Chainable {
      mount<Component extends Vue>(
        component: Component,
        options?: ComponentMountingOptions<Component>
      ): Chainable<any>;
    }
  }
}

(仍然不知道我在做什么.)

Typescript相关问答推荐

如何使用另一个类型的属性中的类型

如何使用Generics保留构造函数的类型信息?

如何获取数组所有可能的索引作为数字联合类型?

具有映射返回类型的通用设置实用程序

在TypeScrip中分配回调时,参数的分配方向与回调本身相反.为什么会这样呢?

如何在另一个参数类型中获取一个参数字段的类型?

通过按键数组拾取对象的关键点

TypeScrip:来自先前参数的参数中的计算(computed)属性名称

`fetcher.load`是否等同于Get Submit?

笛卡尔产品类型

如何从输入中删除值0

如何在React组件中指定forwardRef是可选的?

有没有办法防止类型交集绕过联合类型限制?

复选框Angular 在Ngfor中的其他块中重复

S,为什么我的T扩展未定义的条件在属性的上下文中不起作用?

如何键入并类型的函数&S各属性

是否可以定义引用另一行中的类型参数的类型?

内联类型断言的工作原理类似于TypeScrip中的断言函数?

在对象数组中设置值

是否可以使用元组中对象的键来映射类型