vuejs / language-tools

⚡ High-performance Vue language tooling based-on Volar.js
https://marketplace.visualstudio.com/items?itemName=Vue.volar
MIT License
5.87k stars 403 forks source link

Prop comments not present in declaration file when prop has defaults #2398

Open jasonlewis opened 1 year ago

jasonlewis commented 1 year ago

I've seen a couple issues for this (#1434, #1261, #703) which all indicate the issue is resolved yet I'm still seeing this problem when using vue-tsc to generate the types for a component library.

I ran npm init vue@latest to test this and created the following MyButton.vue component.

import type { PropType } from 'vue'

export type Variant = 'primary' | 'secondary' | 'success' | 'danger'

const props = defineProps({
  /**
   * The variant of the button.
   */
  variant: {
    type: String as PropType<Variant>,
    default: 'primary',
  },
})

When running vue-tsc --declaration --emitDeclarationOnly I get the following declaration file for the component:

import type { PropType } from 'vue';
export declare type Variant = 'primary' | 'secondary' | 'success' | 'danger';
declare const _default: __VLS_WithTemplateSlots<import("vue").DefineComponent<{
    /**
     * The variant of the button.
     */
    variant: {
        type: PropType<Variant>;
        default: string;
    };
}, {}, unknown, {}, {}, import("vue").ComponentOptionsMixin, import("vue").ComponentOptionsMixin, {}, string, import("vue").VNodeProps & import("vue").AllowedComponentProps & import("vue").ComponentCustomProps, Readonly<import("vue").ExtractPropTypes<{
    /**
     * The variant of the button.
     */
    variant: {
        type: PropType<Variant>;
        default: string;
    };
}>>, {
    variant: Variant;
}>, {
    default: (_: {}) => any;
}>;
export default _default;
declare type __VLS_WithTemplateSlots<T, S> = T & {
    new (): {
        $slots: S;
    };
};

For whatever reason when I use this component in a project I don't see the JSDoc and Ctrl + Click takes me through to the line that has variant: Variant;.

image

I've also tried declaring props with withDefaults(defineProps<{}>()) but it yields the same result. As soon as the default is removed and the prop is marked as required the JSDoc comes through correctly.

Versions Vue: 3.2.47 Vite: 4.1.1 vue-tsc: 1.1.2

If there's any other information you'd need please let me know. I'm hoping this is just an oversight on my behalf. Cheers.

johnsoncodehk commented 1 year ago

I think defineComponent have been change so the solutions of the past don't work.

螢幕截圖 2023-02-20 06 14 00

I will see how to fix to types.

cabal95 commented 9 months ago

We are also running into this issue when trying to build an npm package of our component types (d.ts files). Did you ever make headway on finding a way to fix this? I'm happy to do some digging around on this, but not sure where to start looking. As in, I don't even know what if this is a core vue issue or a language-tools issue.

fabio-emmedata commented 4 months ago

Same issue here. Is there any workaround to make it work?