Skip to content

[Question] Is it possible to access vue component props or exported types? #675

@upupming

Description

@upupming

Thanks for your work! Volar works very nicely with TS! This is just a question.

I have a component like this:

<script setup lang="ts">
import { ref } from 'vue'
import { useCounterStore } from '@/stores/counter'

interface TemplateIntroductionProps {
  msg?: string
}

defineProps<TemplateIntroductionProps>()

const count = ref(0)
const counter = useCounterStore()
</script>

<template>
  <div>
    <h1
      v-if="msg"
      class="font-semibold text-xl text-gray-600"
    >
      {{ msg }}
    </h1>
  </div>
</template>

Since I can get the inferred TemplateIntroductionProps type in the parent template, but I found it is not possible to use this prop type in the script section.

image

Also, I see a discussion saying that it is not possible to get the exported type in a vue file. But I wonder, since volar is able to infer the prop type, maybe it can make the exported type from vue files work as expected.


Motivation:

My motivation is to pass the prop type to the Meta type of storybook, so I can get type suggestions from storybook, like this:

import TemplateIntroduction from './TemplateIntroduction.vue'

import { Meta } from '@storybook/vue3'

export default {
  title: 'Components/TemplateIntroduction',
  component: TemplateIntroduction,
  argTypes: {

  }
} as Meta<{msg: string}>
                   |--> this should be the prop type, will help suggestionn for the `argTypes` field above

update:

I found when using export default without script setup, actually I can export interface and variables:

<script lang="ts">
import { ref } from 'vue'

export default {
  setup () {
    const count = ref(1)
    const inc = () => {
      count.value++
    }

    return {
      count,
      inc
    }
  }
}

export const a = 1

export interface DemoProps {
  msg: string
}
</script>

<template>
  <button @click="inc">
    {{ count }}
  </button>
</template>
import Demo, { DemoProps, a } from '@/components/Demo.vue'
console.log(Demo, a)

So could the script setup export type and interface, too? I found the rfc it says when need to export, we should change setup to default export. There is also a ESLint rule says we should not export anything in script setup, I think there is no any good way to do this.

Metadata

Metadata

Assignees

No one assigned

    Labels

    questionFurther information is requested

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions