Error `TS2322` using Vue's dynamically named Slots
See original GitHub issueWhat happens and why it is wrong
When assembling a project with dynamic slots, the following error appears:
[!] (plugin rpt2) Error: C:/Users/p.marusov/Desktop/element-ui/src/components/ElTable/ElTable.vue?vue&type=script&setup=true&lang.ts(227,7): semantic error TS2322: Type 'Function' is not assignable to type 'Slot'.
Type 'Function' provides no match for the signature '(...args: any[]): VNode<RendererNode, RendererElement, { [key: string]: any; }>[]'.
The problem arises specifically because of this code. In its absence, the assembly is successful:
<template v-for="(_, slot) in $slots" :key="_" #[slot]="scope">
<slot :name="slot" v-bind="scope || {}" />
</template>
rollup.config.js
:
rollup.config.js
export default [
{
input: "src/index.ts",
output: [
{
format: "esm",
file: "dist/index.mjs",
exports: "named",
},
{
format: "cjs",
file: "dist/index.js",
exports: "named",
},
],
external: ["vue", "quasar"],
plugins: [
del({ targets: "dist" }),
peerDepsExternal(),
nodeResolve(),
vue(),
typescript2({
clean: true,
typescript: ttypescript,
tsconfig: "tsconfig.rollup.json",
}),
babel({ babelHelpers: "runtime" }),
commonjs(),
postcss(),
image(),
terser(),
],
},
];
tsconfig.json
:
tsconfig.json
{
"compilerOptions": {
"target": "esnext",
"module": "esnext",
"declaration": true,
"removeComments": true,
"moduleResolution": "node",
"isolatedModules": true,
"noImplicitAny": false,
"sourceMap": true,
"baseUrl": ".",
"paths": {
"@/*": ["src/*"]
},
"plugins": [
{
"transform": "@zerollup/ts-transform-paths",
"exclude": [
"*"
]
}
]
},
"files": [
"src/index.ts",
"src/shims-vue.d.ts"
],
"exclude": ["node_modules"]
}
I tried to set the type of slots, but all to no avail.
<template v-for="(_, slot) in ($slots as {})" :key="_" #[slot]="scope">
<slot :name="slot" v-bind="scope || {}" />
</template>
or
<template v-for="(_, slot) in ($slots as Slots)" :key="_" #[slot]="scope">
<slot :name="slot" v-bind="scope || {}" />
</template>
<script lang="ts">
import { Slots } from "vue";
</script>
I don’t understand how this can be fixed.
Issue Analytics
- State:
- Created a year ago
- Comments:7
Top Results From Across the Web
vue Dynamic Slot Names Error Templates should only be ...
I am using Vuejs(2.6.11) Dynamic Slot Names Eerror: Templates should only be responsible for mapping the state to the UI.
Read more >semantic error ts2322 | The AI Search Engine You Control
Semantic error TS2322 Type 'GridMaterial' is not assignable to type 'Material'. ... When assembling a project with dynamic slots, the following error ...
Read more >Slots - Vue.js
In a parent component using <BaseLayout> , we need a way to pass multiple slot content fragments, each targeting a different slot outlet....
Read more >Vue Components — Dynamic Slot Names and Shorthands
Since Vue.js 2.6.0, we can pass in dynamic slot names with v-slot . For example, we can use it as follows: In the...
Read more >Vue.js Components — Slots and Dynamic Components
The text Error that we put between the error-box tags replace the slot tag, so we get the word 'Error' in red displayed....
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
the issue I have is somewhat related. but it occurs when actually using a conditional template. I forked @agilgur5 repo here.
in essence, by doing:
This is being transpiled to:
Where I get the following error:
Not really sure the issue is on rpt2 or on vue side, as it seems the transpiled/generated code doesn’t match the interface of
createSlots
in this case.For the record, I can get away with it by declaring something like this:
Which is fine for my case as I never call the
createSlots
function directly, so the loss of proper type checking on it is okay.vue-tsc
did not give results. It doesn’t output anything about dynamic slots. Disabling verificationcheck: false
is the only solution so far, but not the one that I would like. Thank you for the answer, I will continue to understand