vite-plugin-dts icon indicating copy to clipboard operation
vite-plugin-dts copied to clipboard

Optional component props in the Options Api are described in .d.ts not correct

Open ivanmem opened this issue 1 year ago • 6 comments

Describe the bug

The plugin does not take into account that "error" is an optional parameter.

Component.vue:

props: {
 error: String as PropType<string>,
},

Like now:

Component.vue.d.ts:

 error: PropType<string>;

It should be:

Component.vue.d.ts:

 error?: PropType<string>;

Reproduction

I'll create it later if anyone needs it.

Steps to reproduce

No response

System Info

"vue": "3.3.2",
"vite": "4.3.5",
"vite-plugin-dts": "2.3.0",

Validations

ivanmem avatar May 17 '23 14:05 ivanmem

Vue props default optional if without required.

qmhc avatar Jun 27 '23 09:06 qmhc

@qmhc even if you specify required: false - the plugin still does not take this into account.

ivanmem avatar Jun 27 '23 11:06 ivanmem

It's unnecessary, Vue (Volar) will infer the definition of props, and will know which porp is required.

image

image

image

image

qmhc avatar Jun 27 '23 11:06 qmhc

@qmhc webstorm Volar appeared only in beta version.

ivanmem avatar Jun 27 '23 11:06 ivanmem

Currently plugin using Volar as drive (in v3), I think we should wait Volar update. Because Volar finally will slove this solution and we shouldn't work repeatedly.

qmhc avatar Jun 27 '23 11:06 qmhc

If it helps, I see the same issue with optional React prop types

My .jsx file has the following defined:

const MessageBox = ({
    center,
    children,
    icon,
    onDismiss,
    size,
    tone,
    ...rest
}) => { ... };

MessageBox.defaultProps = {
    center: false,
    children: null,
    icon: null,
    onDismiss: null,
    size: null,
    tone: null,
};

MessageBox.propTypes = {
    /** Center the content horizontally */
    center: PropTypes.bool,

    /** React elements for display inside MessageBox */
    children: PropTypes.node,

    /** A single ui-icon component */
    icon: PropTypes.element,

    /** Your callback function for the MessageBox close button */
    onDismiss: PropTypes.func,

    /** Controls spacing, text and icon size */
    size: PropTypes.oneOf(['large', 'tiny']),

    /** Controls the contextual tone */
    tone: PropTypes.oneOf([
        'neutral',
        'info',
        'success',
        'critical',
        'promote',
    ]),
};

The generated definition file is this (optional props are not listed as optional):

export default MessageBox;
declare function MessageBox({ center, children, icon, onDismiss, size, tone, ...rest }: {
    [x: string]: any;
    center: any;
    children: any;
    icon: any;
    onDismiss: any;
    size: any;
    tone: any;
}): import("react/jsx-runtime").JSX.Element | null;
declare namespace MessageBox {
    namespace defaultProps {
        const center: boolean;
        const children: null;
        const icon: null;
        const onDismiss: null;
        const size: null;
        const tone: null;
    }
    namespace propTypes {
        const center_1: PropTypes.Requireable<boolean>;
        export { center_1 as center };
        const children_1: PropTypes.Requireable<PropTypes.ReactNodeLike>;
        export { children_1 as children };
        const icon_1: PropTypes.Requireable<PropTypes.ReactElementLike>;
        export { icon_1 as icon };
        const onDismiss_1: PropTypes.Requireable<(...args: any[]) => any>;
        export { onDismiss_1 as onDismiss };
        const size_1: PropTypes.Requireable<string>;
        export { size_1 as size };
        const tone_1: PropTypes.Requireable<string>;
        export { tone_1 as tone };
    }
}
import PropTypes from 'prop-types';

StevenTew avatar Oct 10 '23 10:10 StevenTew