wheatjs / vite-plugin-vue-type-imports

Import types in Vue SFC for defineProps
223 stars 16 forks source link

vite-plugin-vue-type-imports

Enables you to import types and use them in your defineProps and defineEmits. Supports both Vue 2 and Vue 3.

NPM version

⚠️ This Plugin is still in Development and there may be bugs. Use at your own risk.

Install

# Install Plugin
npm i -D vite-plugin-vue-type-imports
// vite.config.ts

import { defineConfig } from 'vite'
import Vue from '@vitejs/plugin-vue'
import VueTypeImports from 'vite-plugin-vue-type-imports'

export default defineConfig({
  plugins: [
    Vue(), 
    VueTypeImports(),
  ],
})

Nuxt

// nuxt.config.ts

export default {
  buildModules: [
    'vite-plugin-vue-type-imports/nuxt',
  ]
}

Usage

// types.ts

export interface User {
  username: string
  password: string
  avatar?: string
}
<script setup lang="ts">
import type { User } from '~/types'

defineProps<User>()
</script>

<template>...</template>

Known limitations

Notes

Caveats

Illegal code:

export type Bar = Foo
export interface Foo {
  foo: Bar
}

Alternatively, you can reference the types themselves in their own definitions

Acceptable code:

export type Bar = string

export interface Foo {
  foo: Foo
  bar: Foo | Bar
}

These types may cause conflicts:

type Foo_1 = string
type Bar_2 = number

License

MIT License © 2021-PRESENT Jacob Clevenger