-
-
Notifications
You must be signed in to change notification settings - Fork 8.4k
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
types(defineSlots): Support for generic keyof slots #8374
Conversation
Whoa! Thanks so much for quick action ❤️ |
@pikax Hi, I tried it and it seems not fix vuejs/language-tools#3141 now. Is there anything I miss? |
In @jd-solanki 's demo. I update vue to 3.3.4 and Change <script lang="ts" setup generic="Row extends Record<string, unknown>">
import { objectKeys } from '../utils';
import { aTableProps, aTableSlots } from './meta';
const props = defineProps(aTableProps<Row>())
const _slots = aTableSlots<Row>(
objectKeys(props.rows[0] || {}),
)
const slots = defineSlots<typeof _slots>()
for (const col of props.cols) {
const slot = slots[`header-${col.name}`];
slot?.({ col })
}
</script>
<template>
<div class="wrapper">
<div v-for="row in props.rows">
<header v-for="(col, index) in props.cols" :key="index">
<slot :name="`header-${col.name}`" :col="col"></slot>
<!-- This expression is not callable. Type '{}' has no call signatures. -->
</header>
</div>
</div>
</template> |
@xiaoxiangmoe please provide an contained example, otherwise there's too many running pieces that might get this broken. I would discourage using Here's a full working self contained example: <script lang="ts" setup generic="Row extends Record<string, unknown>">
const props = defineProps<{
rows: Row[]
cols: { name: keyof Row; label: string }[]
}>()
const slots = defineSlots<{
[K in keyof Row as `header-${K & string}`]: (props: {
col: { name: keyof Row; label: string }
}) => any[]
}>()
for (const col of props.cols) {
const slot = slots[`header-${String(col.name)}`]
slot?.({ col })
}
</script>
<template>
<div class="wrapper">
<div v-for="row in rows">
<header v-for="(col, index) in cols" :key="index">
<slot :name="`header-${String(col.name)}`" :col="col"></slot>
<!-- This expression is not callable. Type '{}' has no call signatures. -->
</header>
</div>
</div>
</template> |
@pikax I want to point out that we also have additional slot that is concated using Additionally, I want to point out that user might separate component's metas like props, slots, emits in another ts file and import it in SFC like I do in my UI lib anu. Thing to consider it when we import object from another file we need to use |
Because that's very straightforward and the adding extra doesn't reproduce the bug.
You're free to import from other files, but using Just to be clear |
Any news on merging this? |
Size ReportBundles
Usages
|
CodSpeed Performance ReportMerging #8374 will not alter performanceComparing Summary
|
related vuejs/language-tools#3141
See comment: vuejs/language-tools#3141 (comment)
Currently
defineSlots
with keyof generic has the error of// Type '{}' has no call signatures.ts(2349)
, this PR fixes that error.The issue is caused by
Readonly
, it seems it changes the type for calling the function. I think the typereadonly
safeness on typescript is less important than having generics working properly.