Introduction To The Script Setup Syntax - TypeScript Friendly Vue 3 - Vue Mastery
Introduction To The Script Setup Syntax - TypeScript Friendly Vue 3 - Vue Mastery
While the evolution from Vue 3.1 to 3.2 was technically a minor version update, it comes with
a major improvement in TypeScript experience. This new version of Vue promotes the Script
Setup syntax, which was previously an experimental feature, and is now an officially
supported one. With it, we have a simpler way to write components with the Composition API and
a more elegant way of using TypeScript in our Vue apps.
While the above snippets are in pure JavaScript, this simplicity carries over to the
TypeScript side of Vue as well.
This tutorial will focus on the TypeScript aspects of using the new Script Setup syntax. So to get
the most out of this tutorial, make sure that you have a basic familiarity with TypeScript,
the Composition API, and the Script Setup syntax. If you need a refresher on any of these, you can
check out the official docs or the Vue Mastery courses on these subjects.
So first, we’ll go through some TypeScript fundamentals for working with the Composition API, and
then we’ll get into the new TypeScript features of the Script Setup syntax. The goal is to provide a
well-rounded tour of the Vue-TypeScript combination that features the newest recommended
practices.
Setup
If you want to follow along with the code in this tutorial, you can create a new Vue TypeScript app
using Vite. If you’re new to Vite, you can check out our course taught by its creator, Evan You.
As a newer alternative, you can also use the official scaffolding tool create-vue :
To opt-in to using the Script Setup syntax, you can simply just add the setup attribute to
the script tag:
IDE Support
For the best IDE support, you can use VSCode with the Volar plugin. As an alternative, you can
also use WebStorm, which comes with built-in support for Script Setup.
So when you put your mouse cursor on a variable, it will show you its type:
Then same when you do that in the <template> :
Now let’s begin our tour of the new Vue TypeScript experience. We’ll start with the most
fundamental of all things: creating reactive variables.