Skip to content

Bug: compiler error at mount function with TypeScript 4.9.3 #1872

@pscheede

Description

@pscheede

Describe the bug

While updating to TypeScript 4.9 and Vue 3.2.45 I encountered a compiler error in my tests at the mount() function. A bit of testing showed that the error disappears if either TypeScript is downgraded to 4.8.4, Vue is downgraded to 3.2.37 or below, or VTU is downgraded to 2.0.0.

The error seems only to be present for components that define one or more mandatory props and no emits.

tests/AComponent.spec.ts:6:31 - error TS2769: No overload matches this call.
  The last overload gave the following error.
    Argument of type 'ComponentPublicInstanceConstructor<{ $: ComponentInternalInstance; $data: {}; $props: Partial<{ number: string; }> & Omit<Readonly<ExtractPropTypes<__VLS_WithDefaults<__VLS_TypePropsToRuntimeProps<Props>, { ...; }>>> & VNodeProps & AllowedComponentProps & ComponentCustomProps, "number">; ... 10 more ...; $watch(sour...' is not assignable to parameter of type 'ComponentOptionsWithObjectProps<__VLS_WithDefaults<__VLS_TypePropsToRuntimeProps<Props>, { number: undefined; }>, {}, {}, {}, {}, ComponentOptionsMixin, ... 4 more ..., { ...; }>'.
      Type 'ComponentPublicInstanceConstructor<{ $: ComponentInternalInstance; $data: {}; $props: Partial<{ number: string; }> & Omit<Readonly<ExtractPropTypes<__VLS_WithDefaults<__VLS_TypePropsToRuntimeProps<Props>, { ...; }>>> & VNodeProps & AllowedComponentProps & ComponentCustomProps, "number">; ... 10 more ...; $watch(sour...' is not assignable to type 'ComponentOptionsBase<Readonly<ExtractPropTypes<__VLS_WithDefaults<__VLS_TypePropsToRuntimeProps<Props>, { number: undefined; }>>> & { ...; }, ... 8 more ..., { ...; }>'.
        Types of property 'setup' are incompatible.
          Type '((this: void, props: Readonly<LooseRequired<Readonly<ExtractPropTypes<__VLS_WithDefaults<__VLS_TypePropsToRuntimeProps<Props>, { number: undefined; }>>>>>, ctx: SetupContext<...>) => void | ... 2 more ... | Promise<...>) | undefined' is not assignable to type '((this: void, props: Readonly<LooseRequired<Readonly<ExtractPropTypes<__VLS_WithDefaults<__VLS_TypePropsToRuntimeProps<Props>, { number: undefined; }>>> & { ...; }>>, ctx: SetupContext<...>) => void | ... 2 more ... | Promise<...>) | undefined'.
            Type '(this: void, props: Readonly<LooseRequired<Readonly<ExtractPropTypes<__VLS_WithDefaults<__VLS_TypePropsToRuntimeProps<Props>, { number: undefined; }>>>>>, ctx: SetupContext<...>) => void | ... 2 more ... | Promise<...>' is not assignable to type '(this: void, props: Readonly<LooseRequired<Readonly<ExtractPropTypes<__VLS_WithDefaults<__VLS_TypePropsToRuntimeProps<Props>, { number: undefined; }>>> & { ...; }>>, ctx: SetupContext<...>) => void | ... 2 more ... | Promise<...>'.
              Types of parameters 'ctx' and 'ctx' are incompatible.
                Type 'SetupContext<string[]>' is not assignable to type 'SetupContext<{}>'.
                  Type '{}' is missing the following properties from type 'string[]': length, pop, push, concat, and 29 more.

6       const component = mount(AComponent, {
                                ~~~~~~~~~~

To Reproduce

Here the component and the test file from the repository for a quick overview. If the two lines concerning the emits are added, the project compiles again.

AComponent.vue

<script setup lang="ts">
interface Props {
  text: string,
  number?: number,
}
withDefaults(defineProps<Props>(), {
  number: undefined,
});

// interface Emits { (e: 'eventname'):void }
// const emit = defineEmits<Emits>();
</script>

<template>
  <div class="output">{{ text }}</div>
</template>

<style scoped>
</style>

AComponent.spec.ts

import {mount} from '@vue/test-utils';
import AComponent from '@/AComponent.vue';

describe('AComponent', () => {
  it('should display text', () => {
    const component = mount(AComponent, { props: { text: 'some text' } });

    const output = component.get('.output');
    expect(output.text()).toContain('some text');
  });
});

Expected behavior

I would expect the compiler not to report any errors.

Related information:

  • @vue/test-utils version: 2.0.1+
  • Vue version: 3.2.38+
  • node version: 16.17.1
  • npm (or yarn) version: 8.15.0
  • TypeScript version: 4.9.3

Additional context

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions