Getting Started With Vue CLI 3 + TypeScript

Getting Started With Vue CLI 3 + TypeScript

In this tutorial, we’ll be exploring Vue CLI 3, along with the TypeScript, in order to quickly scaffold a project.

Creating a project

Note that the following steps can also be achieved on the Vue GUI — to open it type Vue ui into your command line.

Start off by creating a Vue CLI project as normal:

# Install Vue CLI if it isn't already installed
npm install --global @vue/cli

# Create a new project
vue create my-project-name

When prompted, choose ‘Manually select features’ and then select TypeScript. Select any other features that you would like.

Further options

After this, you’ll be asked to select additional options.

Use class-style component syntax? Vue TypeScript components can be written in either of two ways:

Answer Yes to install the libraries needed to write class-style components.

Pick a linter / formatter config: 

Choose TSLint for appropriate TypeScript syntax checking and analysis.

Wait for the project to be created, and then type npm run serve to see it live.

Project structure

Your folder structure will look similar to a typical Vue CLI project. Some files are specific to TypeScript projects:

Project:
|   babel.config.js
|   package-lock.json
|   package.json
|   postcss.config.js
|   README.md
|   tsconfig.json
|   tslint.json
|   .browserslistrc
|   .gitignore
|   
+---node_modules 
|
+---public
|   |   favicon.ico
|   |   index.html
|   |   manifest.json
|   |   robots.txt
|   |   
|   \---img
|       \---icons
|               
\---src
    |   App.vue
    |   main.ts
    |   registerServiceWorker.ts
    |   router.ts
    |   shims-tsx.d.ts
    |   shims-vue.d.ts
    |   store.ts
    |   
    +---assets
    |       logo.png
    |       
    +---components
    |       HelloWorld.vue
    |       
    \---views
            About.vue
            Home.vue

tsconfig.jsonDenotes a TypeScript project. Contains compiler options and specifies the location of root files.

tslint.jsonOptions and rules for Typescript linting, for TSlint.

main.ts The equivalent of main.js; the entry file for the project.

shims-tsx.d.ts Allows the use of .tsx files. Read more about this here.

shims-vue.d.ts Allows .vue single file components to be imported and used.

Writing components

We can now write TypeScript in .vue single file components, within the script tags.

<script lang="ts"></script>

Components are written using either Vue.extend() or the @Component decorator. Choose the way that is best suited to your needs.

Basic usage, with Vue.extend()

Similar to declaring a normal Vue component. Vue.extend() is used so that the code inside can be subject to type inference.

<template>
  <div>
    <h1>{{ msg }}</h1>
  </div>
</template>

<script lang="ts">
import Vue from 'vue';
export default Vue.extend({
  name: 'HelloWorld',
  props: {
    msg: String,
  },
});
</script>

<style scoped lang="scss">
</style>

Class-style, with @Component decorator

Decorators, such as @Prop, @Watch, @Emit, are used to define the component. 

This is achieved using a combination of two libraries, vue-class-component and vue-property-decorator.

<template>
  <div>
    <h1>{{ msg }}</h1>
  </div>
</template>

<script lang="ts">
import { Component, Prop, Vue } from 'vue-property-decorator';
@Component
export default class HelloWorld extends Vue {
  @Prop() private msg!: string;
}
</script>

<style scoped lang="scss">
</style>

IDE support for TypeScript

TypeScript support and features are available with these IDEs:

vue-js typescript javascript web-development

Bootstrap 5 Complete Course with Examples

Bootstrap 5 Tutorial - Bootstrap 5 Crash Course for Beginners

Nest.JS Tutorial for Beginners

Hello Vue 3: A First Look at Vue 3 and the Composition API

Building a simple Applications with Vue 3

Deno Crash Course: Explore Deno and Create a full REST API with Deno

How to Build a Real-time Chat App with Deno and WebSockets

Convert HTML to Markdown Online

HTML entity encoder decoder Online

Top Vue.js Developers in USA

Vue.js is an extensively popular JavaScript framework with which you can create powerful as well as interactive interfaces. Vue.js is the best framework when it comes to building a single web and mobile apps.

Hire Web Developer

Looking for an attractive & user-friendly web developer? HourlyDeveloper.io, a leading web, and mobile app development company, offers web developers for hire through flexible engagement models. You can **[Hire Web...

Hire Node.JS Developers | Skenix Infotech

We are providing robust Node.JS Development Services with expert Node.js Developers. Get affordable Node.JS Web Development services from Skenix Infotech.