vuejs / composition-api

Composition API plugin for Vue 2
https://composition-api.vuejs.org/
MIT License
4.19k stars 342 forks source link

Bad derivation of type by `ExtractPropTypes` #965

Closed mbarouski closed 2 years ago

mbarouski commented 2 years ago

Issue description

Let's say we have interface:

interface InterfaceWithAllOptionalKeys {
  a?: number;
  b?: string;
}

then we use that interface to describe some property prop of some component:

defineComponent({
  props: {
    prop: {
      type: Object as PropType<InterfaceWithAllOptionalKeys>,
      required: true
    }
  },
  // ...
});

and as a result, the type of props.prop inside setup(props) function (for instance) is:

image

But if I change interface to:

interface InterfaceWithAllOptionalKeys {
  a: number | undefined;
  b?: string;
}

then the type of props.prop is correct and is:

image

What's expected

To have type of props.prop equal InterfaceWithAllOptionalKeys for both interfaces.

How to reproduce

Code to reproduce the issue:

interface InterfaceWithAllOptionalKeys {
  a?: number; // use `a: number | undefined;` to compare type of props.prop
  b?: number;
}

defineComponent({
  props: {
    prop: {
      type: Object as PropType<InterfaceWithAllOptionalKeys>,
      required: true
    }
  },
  setup(props) {
    console.log(props.prop); // check type of props.prop in your IDE
  }
});
github-actions[bot] commented 2 years ago

Stale issue message