Component is indeed the correct type, as you've attempted, but you need to import it before use:

<script setup lang="ts">
import type { Component } from 'vue'
⋮
interface Props {
  header?: Component
  body?: Component
  footer?: Component
}

const fragments: Props = {
  header: FragProfile,
  body: FragSubject,
  footer: TypeSection
}
</script>

demo

Answer from tony19 on Stack Overflow
🌐
GitHub
github.com › Kamar-Meddah › vue-generate-component-typescript
GitHub - Kamar-Meddah/vue-generate-component-typescript: Vue js component generator
<template> <div class="home"> <h1>home Component</h1> </div> </template> <script lang="ts"> import Vue from 'vue'; import Component from 'vue-class-component'; @Component({}) export default class HomeComponent extends Vue { mounted (){ console.log('hello from app'); } } </script> <style scoped lang="scss"> .home { } </style>
Starred by 7 users
Forked by 10 users
Languages   JavaScript 84.5% | TypeScript 8.5% | Vue 7.0% | JavaScript 84.5% | TypeScript 8.5% | Vue 7.0%
🌐
npm
npmjs.com › package › vue-generate-component-typescript
vue-generate-component-typescript - npm
March 24, 2018 - Vue js component generator. Latest version: 1.3.6, last published: 8 years ago. Start using vue-generate-component-typescript in your project by running `npm i vue-generate-component-typescript`. There are no other projects in the npm registry using vue-generate-component-typescript.
      » npm install vue-generate-component-typescript
    
Published   Mar 24, 2018
Version   1.3.6
Author   Kamar Meddah
Discussions

vue.js - How to get a component type so I can use it in a typescript interface - Vue 3 - Stack Overflow
I'm just trying to setup an array ... dynamic component switching later. But I'm stuck at getting Typescript to work with it and no matter what way I ask the question online all the previous answers are all about completely different things. I've provided a screenshot of what I want to do. I want to be able to write a typescript interface and give it some type which corresponds to any vue component ... More on stackoverflow.com
🌐 stackoverflow.com
vue.js - Generate d.ts file for Vue components written in Typescript - Stack Overflow
I'm new to Vue and I would like to learn how to create components and publish the package to NPM. So, my idea is to create Vue (typescript) + Vuetify reusable components and install them from NPM i... More on stackoverflow.com
🌐 stackoverflow.com
Working with slots in Vue 3 + TypeScript.
FYI, I was able to get this working like so: More on reddit.com
🌐 r/vuejs
5
9
March 22, 2021
Why does importing components gives an error?
It sounds like a tsconfig problem. How is your project resolving the '@' on the imports? Is it declared that @ points to the root directory? "compilerOptions": { "baseUrl": ".", "paths": { "@/": ["src/"] }, } More on reddit.com
🌐 r/vuejs
6
1
March 20, 2024
🌐
DigitalOcean
digitalocean.com › community › tutorials › vuejs-using-typescript-with-vue
How To Use TypeScript with Vue Single File Components | DigitalOcean
March 17, 2021 - In this tutorial, you will use @vue/cli to generate a new Vue.js 2.0 application with TypeScript and build a Single-File Component (SFC).
🌐
Medium
toastui.medium.com › developing-vue-components-with-typescript-18357ae7f297
Developing Vue Components with TypeScript | by TOAST UI | Medium
June 10, 2019 - If you are using Visual Studio ... be integrated onto Vue projects to develop components by using the extend in the type declaration defined in @type directory....
🌐
GitHub
github.com › ktsn › vuetype
GitHub - ktsn/vuetype: Generate TypeScript declaration files for .vue files
This would useful if you would like to unit test your components in TypeScript. ... <template> <div>{{ message }}</div> </template> <script lang="ts"> import Vue from 'vue' import Component from 'vue-class-component' @Component export default class MyComp extends Vue { message = 'Hello' } </script> ... You can enable watch mode by adding --watch (shorthand -w) flag. In the watch mode, vuetype watches update of .vue files and generates declaration files when the .vue files are updated.
Starred by 123 users
Forked by 26 users
Languages   TypeScript 95.8% | JavaScript 4.2% | TypeScript 95.8% | JavaScript 4.2%
🌐
GitHub
github.com › Akryum › vue-typegen
GitHub - Akryum/vue-typegen: Generate types for TypeScript Vue components libraries
/* eslint-disable */ declare module '*.vue' { const component: any export default component } ... { "name": "vue-mention", "description": "Mention popper for input and textarea", "version": "1.1.0", "license": "MIT", "main": "dist/vue-mention.umd.js", "module": "dist/vue-mention.es.js", "types": "dist/index.d.ts", "exports": { ".": { "import": "./dist/vue-mention.es.js", "require": "./dist/vue-mention.umd.js" } }, "scripts": { "dev": "vite build --watch", "build": "vite build && tsc -d --emitDeclarationOnly && vue-typegen gen -s src -o dist", "prepublishOnly": "npm run build" }, "peerDependencies": { "vue": "^3.2" }, "devDependencies": { "@vitejs/plugin-vue": "^2.0.1", "typescript": "^4.5.4", "vite": "^2.7.10", "vue": "^3.2.26", "vue-typegen": "^0.1.1" } }
Author   Akryum
Top answer
1 of 2
32

Component is indeed the correct type, as you've attempted, but you need to import it before use:

<script setup lang="ts">
import type { Component } from 'vue'
⋮
interface Props {
  header?: Component
  body?: Component
  footer?: Component
}

const fragments: Props = {
  header: FragProfile,
  body: FragSubject,
  footer: TypeSection
}
</script>

demo

2 of 2
0

Here is a detailed example of dynamic type-safe component loading as it is tricky to get right.

You can use generic Component<PropType> type to define components with similar parameters.

So for instance, to dynamically load all components of certain type you can use this loader function.

type FilterCompName = "EasyFilter"  | "NameFilter"
type Proptype = { filter: string }
type FilteredComponent = Component<Proptype>

function loader<FilteredComponent>(comp: FilterCompName ): FilteredComponent  {
  return defineAsyncComponent(() => import(`./comps/${comp}.vue`))
}

Then you will have mapping array

type FilterNames= "easyFilter"  | "nameFilter"
const comps: { [id in FilterNames]: FilteredComponent} = {
  "easyFilter": loader("EasyFilter"),
  "nameFilter": loader("NameFilter")
} as const

Then just define ref for name and computed for the dynamically loaded async component

const filterString= ref<string>("test")
const name = ref<FilterNames>("easyFilter")
const ViewComp: ComputedRef<FilteredComponent> = computed(() => comps[name .value])

Then in template section you get dynamic component with correctly typed propertis

<template>
   <component  :is="ViewComp" :filter="filterString" />
</template>

Here is a working example

Find elsewhere
🌐
LogRocket
blog.logrocket.com › home › how to use vue 3 with typescript
How to use Vue 3 with TypeScript - LogRocket Blog
June 4, 2024 - For each item below, I’ll show ... the two. Let’s get started! defineComponent provides decorators from the Vue library, which we can use to define Vue components with full TypeScript support....
🌐
Vue.js
vuejs.org › guide › typescript › overview
Using Vue with TypeScript | Vue.js
If you are currently using Vue 3 + TypeScript via Vue CLI, we strongly recommend migrating over to Vite. We are also working on CLI options to enable transpile-only TS support, so that you can switch to vue-tsc for type checking. To let TypeScript properly infer types inside component options, we need to define components with defineComponent():
🌐
DEV Community
dev.to › asayerio_techblog › how-to-use-typescript-to-create-vue-apps-with-vue-class-components-3hp7
How to use TypeScript to Create Vue Apps with Vue Class Components - DEV Community
February 8, 2021 - We can use Vue class components library to add components into our app. We can also define TypeScript types for each part of a component easily with class components.
🌐
GitHub
github.com › microsoft › TypeScript-Vue-Starter
GitHub - microsoft/TypeScript-Vue-Starter: A starter template for TypeScript and Vue with a detailed README describing how to use the two together.
typescript-vue-tutorial/ ├─ dist/ └─ src/ └─ components/ TypeScript files will start out in your src folder, run through the TypeScript compiler, then webpack, and end up in a bundle.js file in dist. Any components that we write will go in the src/components folder. ... Webpack will eventually generate ...
Starred by 4.4K users
Forked by 581 users
Languages   JavaScript 40.8% | Vue 36.1% | TypeScript 20.7% | HTML 2.4% | JavaScript 40.8% | Vue 36.1% | TypeScript 20.7% | HTML 2.4%
🌐
Snyk
snyk.io › advisor › javascript packages › vue-generate-component-typescript
vue-generate-component-typescript - npm package | Snyk
An important project maintenance signal to consider for vue-generate-component-typescript is that it hasn't seen any new versions released to npm in the past 12 months, and could be considered as a discontinued project, or that which receives low attention from its maintainers.
🌐
Medium
medium.com › @ademyalcin27 › crafting-type-safe-components-in-vue-js-with-typescript-3724276c6155
Crafting Type-Safe Components in Vue.js with TypeScript | by Ademyalcin | Medium
March 6, 2024 - The goal is to create a dropdown that is flexible yet type-safe, ensuring that the data structure of the options is strictly adhered to. The component is defined with <script setup lang="ts">, indicating that we are using TypeScript in a setup ...
🌐
GitHub
github.com › frogbob › vue-ts-component-generator
GitHub - frogbob/vue-ts-component-generator: Generate vue typescript components.
Generate vue typescript components. Contribute to frogbob/vue-ts-component-generator development by creating an account on GitHub.
Starred by 5 users
Forked by 2 users
Languages   JavaScript 100.0% | JavaScript 100.0%
🌐
Vue.js
v2.vuejs.org › v2 › guide › typescript.html
TypeScript Support — Vue.js
March 8, 2023 - Fortunately, there’s a TypeScript feature to augment existing types called module augmentation. For example, to declare an instance property $myProperty with type string: After including the above code as a declaration file (like my-property.d.ts) in your project, you can use $myProperty on a Vue instance. You can also declare additional global properties and component options:
🌐
Medium
medium.com › js-dojo › make-the-most-of-your-vue-components-with-typescript-34a7c4d42135
Make the most of your Vue components with Typescript | by Adrien Miquel | Vue.js Developers | Medium
October 25, 2020 - Adding the lang="ts" attribute to the script tag so that Typescript compiler can recognized it and “scan” it. ... <script lang="ts"> import Vue from 'vue';export default Vue.extend({ // ... You component (name, props, data, computed, ...) ...
🌐
Medium
medium.com › @blaster203 › how-to-create-a-component-library-with-vue-3-vitejs-typescript-8eb41f799045
How to create a component library with Vue 3 + ViteJS + TypeScript? | by Israel De Castro A. | Medium
December 29, 2023 - How to create a component library with Vue 3 + ViteJS + TypeScript? We’re using ViteJS to create the component library. With the option, build.lib we will create the library. If you want to learn …