项目
在 GitHub 查看
wheatjs/vite-plugin-vue-type-imports
关于
Import types in Vue SFC for defineProps
网站与应用开发实测星标增长
- 24小时
- 0
- 7天
- 0
- 15天
- 0
- 1个月
- 正在收集历史
各周期内的星标净变化,周期相互重叠,并非每日新增。缺失的测量保持空白。星标代表关注度,不代表软件质量。
README.md
原文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.
⚠️ 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
- Namespace imports like
import * as Foo from 'foo'are not supported. - These types are not supported.
- The plugin currently only scans the content of
<script setup>. Types defined in<script>will be ignored.
Notes
Enumtypes will be converted to Union Types (e.g.type [name] = number | string) , since Vue can't handle them right now.- The plugin may be slow because it needs to read files and traverse the AST (using @babel/parser).
Caveats
- Do not reference the types themselves implicitly, it will cause infinite loop. Vue will also get wrong type definition even if you disable this plugin.
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
}
- Ending the type name with something like
_1and_2is not recommended, because it may conflict with the plugin's transformation result
These types may cause conflicts:
type Foo_1 = string
type Bar_2 = number
License
MIT License © 2021-PRESENT Jacob Clevenger
探索相关项目
加载中…