I'm using VSCode and TypeScript classes for Vue 2 component development. See: vuejs/vue-class-component.
In my current project I use plugins like vue-i18n
for translations of labels etc. These plugins extend the Vue components with their own functions like this.$t(...)
to get a translation by key, but VSCode doesn't recognize / doesn't no off these extensions (or are they mixins?) etc.
How can I learn VSCode that these extension functions exist and intellisense starts working? Can I create my own *.d.ts files? And if so, how can I hook these up so VSCode can find them for intellisense? Any example is welcome. Or link to some example Github repo where this is done?
Vue is written in TypeScript itself and provides first-class TypeScript support. All official Vue packages come with bundled type declarations that should work out-of-the-box.
Vetur provides syntax highlighting for all your Vue source code files. Without Vetur, a .vue file will be displayed in this way by VS Code: with Vetur installed: VS Code is able to recognize the type of code contained in a file from its extension.
This issue is solved now and documented in the Vue TypeScript documentation. It's called "Augmenting Types for Use with Plugins".
The following snippet is from this page for quick reference:
// For example, to declare an instance property $myProperty with type string:
// 1. Make sure to import 'vue' before declaring augmented types
import Vue from 'vue'
// 2. Specify a file with the types you want to augment
// Vue has the constructor type in types/vue.d.ts
declare module 'vue/types/vue' {
// 3. Declare augmentation for Vue
interface Vue {
$myProperty: string
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With