vuejs / composition-api

Composition API plugin for Vue 2

Home Page:https://composition-api.vuejs.org/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Bad derivation of type by `ExtractPropTypes`

mbarouski opened this issue · comments

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
  }
});

Stale issue message