Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Element Plus prop values not typechecked #2176

Closed
Ragura opened this issue Dec 7, 2022 · 5 comments
Closed

Element Plus prop values not typechecked #2176

Ragura opened this issue Dec 7, 2022 · 5 comments
Labels
bug Something isn't working

Comments

@Ragura
Copy link

Ragura commented Dec 7, 2022

This may be a problem beyond the usage of Element Plus, but I will describe what happens for me:

When working inside a Vue template and using imported Element Plus components, there is no typechecking for values passed to a prop. It's strange, because when hovering over the component itself, it will correctly list the props and its accepted types.

For example: the <el-image> component has a prop called fit which can take the following values: 'fill' | 'contain' | 'cover' | 'none' | 'scale-down'. The prop 'fit' correctly shows as available with intellisense (though I have to select it from the list, it won't autocomplete while typing inside the component tag). However, the prop itself is typed as string and has no mention of the correct list of strings as defined by the component.

Screenshot 2022-12-07 at 14 35 02

image

Is there anything wrong with the type recognition, or did I set something up the wrong way?

@Sight-wcg
Copy link
Contributor

I used the Show Virtual Files to look at the generated code, SFCWithInstall causes type inference error.

element-plus: export type SFCWithInstall = T & Plugin

// vue/runtime-core.d.ts
declare type Plugin_2 = (PluginInstallFunction & {
    install?: PluginInstallFunction;
}) | {
    install: PluginInstallFunction;
};
export { Plugin_2 as Plugin }

declare type PluginInstallFunction = (app: App, ...options: any[]) => any;

// VLS_type.ts
export type ComponentProps<T> =
	Record<string, unknown> &
	(
		T extends (...args: any) => any ? (T extends (...args: any) => { props: infer Props } ? Props : {})
		: T extends new (...args: any) => any ? (T extends new (...args: any) => { $props: infer Props } ? Props : {})
		: T // IntrinsicElement
	);

image

After removing SFCWithInstall, automatic completion can work normally.
image

@johnsoncodehk
Copy link
Member

@Sight-wcg Could you try does reverse T extends new (...args: any) => any, T extends (...args: any) => any fix the problem?

export type ComponentProps<T> =
	Record<string, unknown> &
	(
		T extends new (...args: any) => any ? (T extends new (...args: any) => { $props: infer Props } ? Props : {})
		: T extends (...args: any) => any ? (T extends (...args: any) => { props: infer Props } ? Props : {})
		: T // IntrinsicElement
	);

@Sight-wcg
Copy link
Contributor

Sight-wcg commented Dec 8, 2022

@Sight-wcg Could you try does reverse T extends new (...args: any) => any, T extends (...args: any) => any fix the problem?

export type ComponentProps<T> =
	Record<string, unknown> &
	(
		T extends new (...args: any) => any ? (T extends new (...args: any) => { $props: infer Props } ? Props : {})
		: T extends (...args: any) => any ? (T extends (...args: any) => { props: infer Props } ? Props : {})
		: T // IntrinsicElement
	);

Can't fix the problem.
The type of 'VLS_1' is 'T', but type of props 'type' is string.

image
image


edit: Yeah this seems to be working perfectly. My test method is wrong, should exit the takeover mode for testing @johnsoncodehk

image

Sight-wcg added a commit to Sight-wcg/volar that referenced this issue Dec 8, 2022
Sight-wcg added a commit to Sight-wcg/volar that referenced this issue Dec 8, 2022
@johnsoncodehk
Copy link
Member

FIxed by #2180

@johnsoncodehk johnsoncodehk added the bug Something isn't working label Dec 8, 2022
@Ragura
Copy link
Author

Ragura commented Dec 8, 2022

Thank you for the quick resolution!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

3 participants