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

feat: Support generic typed template slots for RFC 436 #1987

Merged
merged 3 commits into from Oct 11, 2022

Conversation

johnsoncodehk
Copy link
Member

@johnsoncodehk johnsoncodehk commented Oct 11, 2022

Update codegen for script setup to change export component type from DefineComponent to functional component.

For following SFC code:

<template>
  <slot name="default" :test="props.msg"></slot>
</template>

<script lang="ts" setup generic="T">
const props = defineProps<{ msg: T }>()
</script>

Output type:

<T>(__props: { msg: T }): {
	props: typeof __props,
	children: { default(_: { test: T }): any },
}

vue-component-meta tests is broken because we not yet implement type extract for functional component in vue-component-meta.

@johnsoncodehk johnsoncodehk force-pushed the functional-script-setup-component branch from 6db4beb to 5a086a4 Compare October 11, 2022 10:59
@johnsoncodehk johnsoncodehk changed the title refactor: export script setup component as functional component type feat: Support generic typed template slots for RFC 436 Oct 11, 2022
@johnsoncodehk johnsoncodehk merged commit ec5946d into master Oct 11, 2022
@johnsoncodehk johnsoncodehk deleted the functional-script-setup-component branch October 11, 2022 11:15
@StepanMynarik
Copy link

StepanMynarik commented Oct 11, 2022

Updated to 1.0.4 and the thing works PERFECTLY. Generic list components, multiselects etc. - all will be cleaner now, more safe. This is lovely.
Well done!

@StepanMynarik
Copy link

StepanMynarik commented Oct 11, 2022

I noticed one issue that stems from this I believe.

In the following code, 'typeof BButton' is highlighted with error: Type '(__VLS_props: { text: string; preset?: Preset | undefined; size?: Size | undefined; disabled?: boolean | undefined; active?: boolean | undefined; }) => Omit<Element, "props" | "children"> & Omit<...> & { ...; }' provides no match for the signature 'new (...args: any): any'.

const referenceRef = $ref<InstanceType<typeof BButton> | null>(null);

@johnsoncodehk
Copy link
Member Author

@StepanMynarik You should use ReturnType<typeof BButton> instead of InstanceType<typeof BButton> for components gen with experimentalRfc436 enabled.

@johanninos
Copy link

johanninos commented Nov 17, 2022

@StepanMynarik You should use ReturnType<typeof BButton> instead of InstanceType<typeof BButton> for components gen with experimentalRfc436 enabled.

InstanceType<typeof BButton<T>> can pass generic.
but ReturnType<typeof BButton<T>> wasn't supported untill typescript 4.7.0

but vue has issue with this ts new feature

vuejs/core#7161

how to deal with that?

@johanninos
Copy link

  const F = () => BButton <string>('');
  const btnRef = ref<ReturnType<typeof F> | null>(null);

this works, but a bit wasteful

@jaulz
Copy link

jaulz commented Feb 21, 2023

Does this actually also work with defineComponent?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants