Skip to content

jsx onClick is not assignable to type of .vue (custom component) #555

Open
@icuxika

Description

@icuxika

VButton

<script setup lang="ts">
import { computed } from "vue";

interface Props {
	theme?: string;
	size?: string;
	level?: string;
	disabled?: boolean;
	loading?: boolean;
}

const props = withDefaults(defineProps<Props>(), {
	theme: "button",
	size: "normal",
	level: "normal",
	disabled: false,
	loading: false,
});

const classes = computed(() => {
	return {
		[`v3-theme-${props.theme}`]: props.theme,
		[`v3-size-${props.size}`]: props.size,
		[`v3-level-${props.level}`]: props.level,
	};
});
</script>
<template>
	<button class="v3-button" :class="classes" :disabled="props.disabled">
		<span v-if="props.loading" class="v3-loading-indicator"></span>
		<span v-else class="v3-loading-indicator-empty"></span>
		<slot />
	</button>
</template>

use VButton

export const DemoX = defineComponent({
	props: {
		component: {
			type: Object as PropType<Component>,
			required: true,
		},
	},
	setup(props, { attrs, slots, emit, expose }) {
		// 渲染源码
		const html = computed(() => {
			return Prism.highlight(
				(props.component as ComponentType).__sourceCode,
				Prism.languages.html,
				"html"
			);
		});
		// 源码是否可见
		const codeVisible = ref(false);
		const toggleCodeVisible = () =>
			(codeVisible.value = !codeVisible.value);
		return () => (
			<div class={style.container}>
				<h2>{(props.component as ComponentType).__sourceCodeTitle}</h2>
				<div class={style.component}>{h(props.component)}</div>
				<div class={style.action}>
						<VButton onClick={withModifiers(toggleCodeVisible, ["self"])}>
							{codeVisible.value ? "隐藏源代码" : "查看源代码"}
						</VButton>
				</div>
				{codeVisible.value ? (
					<div class={style.code}>
						<pre class={"language-html"} innerHTML={html.value} />
					</div>
				) : (
					<div />
				)}
			</div>
		);
	},
});

Question

It works normally when I use yarn dev, but it will report error when I use vue-tsc --noEmit && vite build

error TS2322: Type '{ onClick: (event: Event, ...args: unknown[]) => any; }' is not assignable to type 'IntrinsicAttributes & Partial<{ loading: boolean; disabled: boolean; size: string; theme: string; level: string; }> & Omit<Readonly<ExtractPropTypes<{ theme: { type: PropType<string>; } & { ...; }; size: { ...; } & { ...; }; level: { ...; } & { ...; }; disabled: { ...; } & { ...; }; loading: { ...; } & { ...; }; }>>...'.
  Property 'onClick' does not exist on type 'IntrinsicAttributes & Partial<{ loading: boolean; disabled: boolean; size: string; theme: string; level: string; }> & Omit<Readonly<ExtractPropTypes<{ theme: { type: PropType<string>; } & { ...; }; size: { ...; } & { ...; }; level: { ...; } & { ...; }; disabled: { ...; } & { ...; }; loading: { ...; } & { ...; }; }>>...'.

56       <VButton onClick={withModifiers(toggleCodeVisible, ["self"])}>
                  ~~~~~~~
error Command failed with exit code 2.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions