Skip to content

Support for props extraction on .vue #94

@pikax

Description

@pikax

When you want to infer props from a base component, volar currently can't infer the props types (vuedx currently works)

This code works in typescript

const TestBase = defineComponent({
  props: {
    show: Boolean,
    title: String,
  },
});

type TestBaseType = typeof TestBase extends DefineComponent<
  infer Props,
  any,
  any,
  any,
  any,
  any,
  any,
  any
>
  ? Props
  : {};

const Child = defineComponent({
  props: {
    // props contains the props options
    ...(TestBase.props as TestBaseType),

    extra: String,
  },
  setup(props) {
    let i: boolean = props.show; // boolean
  },
});

But as soon as you separate into SFC types, the type gets lost:

// test.vue
<script lang="ts">
import { defineComponent } from 'vue';

export default defineComponent({
  props: {
    show: Boolean,
    title: String,
  },
});
</script>

// Child.vue
<script lang="ts">
import { defineComponent } from 'vue';
import TestBase from './test.vue'

type TestBaseType = typeof TestBase extends DefineComponent<
  infer Props,
  any,
  any,
  any,
  any,
  any,
  any,
  any
>
  ? Props
  : {};

export default defineComponent({
  props: {
    // props contains the props options
    ...(TestBase.props as TestBaseType),

    extra: String,
  },
  setup(props) {
    // does not work beacuse TestBaseType will be `{}`
    let i: boolean = props.show; // boolean
  },
});
</script>

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions