Skip to content

Latest commit

 

History

History
198 lines (138 loc) · 9.07 KB

File metadata and controls

198 lines (138 loc) · 9.07 KB

Vue Language Features

⚡ Fast Vue Language Support Extension

Plugin's page on Visual Studio Marketplace

Vue Language Features is a language support extension built for Vue, Vitepress and petite-vue. this is based on @vue/reactivity to calculate everything on-demand, to implement native TypeScript language service level performance.

[Tips]

Quick Start

Usage

Setup for Vue 2
  1. Add @vue/runtime-dom

This extension requires Vue 3 types from the @vue/runtime-dom.

Vue 3 and Vue 2.7 has built-in JSX types. For Vue version <= 2.6.14 you need to add JSX types by install @vue/runtime-dom:

// package.json
{
  "devDependencies": {
    "@vue/runtime-dom": "latest"
  }
}
  1. Remove Vue.extend

Template type-checking is not supported with Vue.extend. You can use composition-api, vue-class-component, or export default { ... } instead of export default Vue.extend.

Here is a compatibility table for different ways of writing the script blocks:

Component options type-checking in <script> Interpolation type-checking in <template> Cross-component props type-checking
export default { ... } with JS Not supported Not supported Not supported
export default { ... } with TS Not supported Supported but deprecated Supported but deprecated
export default Vue.extend({ ... }) with JS Not supported Not supported Not supported
export default Vue.extend({ ... }) with TS Limited (supports data types but not props types) Limited Not supported
export default defineComponent({ ... }) Supported Supported Supported
Class component Supported Supported with additional code (#21) Supported with additional code

Note that you can use defineComponent even for components that are using the Options API.

  1. Support for Vue 2 template

Volar preferentially supports Vue 3. Vue 3 and Vue 2 templates have some differences. You need to set the target option to support the Vue 2 templates.

// tsconfig.json
{
  "compilerOptions": {
    // ...
  },
  "vueCompilerOptions": {
    "target": 2.7,
    // "target": 2, // For Vue version <= 2.6.14
  }
}
  1. remove .d.ts files if they exist.

For projects generated by the Vue CLI, .d.ts files are included. Remove these files.

rm src/shims-tsx.d.ts src/shims-vue.d.ts
Define Global Components

PR: vuejs/core#3399

Local components, Built-in components, native HTML elements Type-Checking is available with no configuration.

For Global components, you need to define GlobalComponents interface, for example:

// components.d.ts
declare module '@vue/runtime-core' {  // Vue 3
// declare module 'vue' {   // Vue 2.7
// declare module '@vue/runtime-dom' {  // Vue <= 2.6.14
  export interface GlobalComponents {
    RouterLink: typeof import('vue-router')['RouterLink']
    RouterView: typeof import('vue-router')['RouterView']
  }
}

export {}

Notes

Vetur

You need to disable Vetur to avoid conflicts.

Recommended use css / less / scss as <style> language, because these base on vscode-css-languageservice to provide reliable language support.

If use postcss / stylus / sass, you need to install additional extension for syntax highlighting. I tried these and it works, you can also choose others.

Volar does not include ESLint and Prettier, but the official ESLint and Prettier extensions support Vue, so you could install these yourself if needed.

If using Vetur's Customizable Scaffold Snippets, recommend use Snippet Generator convert to VSCode Snippets. There are also snippets on the VSCode Marketplace, such as Sarah Drasner's Vue VSCode Snippets, if you prefer ready-made snippets without customization.

If VSCode gives an error for class and slot like this:

This is because one of the packages installed in your project uses @types/react which breaks some parts of Volar.

Please see the following solutions:

Recursive components

Volar can't typecheck recursive components out of the box due to TS limitation. But there's a workaround, you can explicitly specify component's props like so:

Bar.vue

<template>
  <Bar :a="'wrong'" />
</template>

<script setup lang="ts">
import { defineAsyncComponent, type DefineComponent } from 'vue'

interface Props {
  a: number
}

const Bar = defineAsyncComponent<DefineComponent<Props>>(
  () => import('./Bar.vue') as any
)
defineProps<Props>()
</script>

Custom File Extensions

Syntax highlighting and intellisense can be applied to additional file extensions beyond just vue. This will need to be configured in three different places for full support.

In VS Code settings for the Volar extension add any additional extensions you need to Additional Extensions. Such as ext.

In your tsconfig.json file you will need to make sure your custom extension is included by TypeScript. If you have an include value for ./src/*.vue then you would want to add an include for ./src/*.ext as well.

"include": [
  "./src/*.ts",
  "./src/*.vue",
  "./src/*.ext",
]

Finally you need to make VS Code recognize your new extension and automatically associate it with the Vue language format. To do this you need to configure your File Associations setting to map *.ext to the language value vue. This can be done under Text Editor > Files, or with the key files.associations.

Sponsors

Credits