Level Up your VueJS project with Typescript (Part 2): Translating Components to Typescript
Tags: tutorial, frontenddev, vue, frontend
Published: 2019-03-30 22:26:01

---
title: Level Up your VueJS project with Typescript (Part 2): Translating Components to Typescript
published: true
tags: tutorial,front-end-developmen,vuejs,frontend
canonical_url: https://medium.com/@fernalvarez/level-up-your-vuejs-project-with-typescript-part-2-translating-components-to-typescript-3719092c7b5f
---

![](https://cdn-images-1.medium.com/max/1024/1*NO1PE-ic3ILCVHqeVHn1SQ.png)

#### If you missed part 1, go to this post:

[Level Up your VueJS project with Typescript (Part 1): Installing Typescript](https://dev.to/lordferquad/level-up-your-vuejs-project-with-typescript-part-1-installing-typescript-3f7l)

For the completed version of this part, use this branch of the project repo:

[jefer590/upgrade-vuejs-ts-series](https://github.com/jefer590/upgrade-vuejs-ts-series/tree/03/translate-components-to-ts)

In part 1, we prepared our project to use Typescript and was only installation + configuration. For many of you that part could be considered the “boring” part and I understand it! To compensate that, in this part we will translate almost all our VueJS Files (Home View+Components) to use Typescript! So let’s begin.

### Translating the “Hello.vue” Component — Single File Component

For this component, we are going to replace only the \<script\> part with TS, the HTML template doesn’t need any modifications (one advantage of using SFC 🙌).

When we open this file we noticed that the file is very simple:

- The component data is only a variable called text with a string
- There’s only one computed property called textUpper that uses text

Try to remember this because we are going to delete all the script part of that component and we will rewrite it using TS starting with:

{% gist https://gist.github.com/jefer590/185a307ea61964dcf441f8b8398732a2 %}

You will noticed that we added a lang="ts". This means that we are telling to Vue Loader the script inside this SFC will be compiled/transpiled using Typescript.

After that, we will use a library that I absolutely love for VueJS TS development called vue-property-decorator that is already included in the project

[kaorun343/vue-property-decorator](https://github.com/kaorun343/vue-property-decorator)

And using that library, **we will start to use this pattern for every component that we will make with TS**

{% gist https://gist.github.com/jefer590/8ff40f9e24c760018743cbaa00661f4e %}

Now you may ask, _“what does this even mean?”_ let me explain it quickly:

@Component is using a feature in [TS called Decorators](https://www.typescriptlang.org/docs/handbook/decorators.html) and in this context will pass some metadata of the component to the class. The metadata is basically all the properties that are available in the VueJS Component like data , methods , components , mixins , etc…

In export default class Hello Extends Vue we are doing almost the same as the traditional way that is exporting the VueJS component object but we have TWO main differences:

- The class name will be also the name of the component
- Since this is Typescript, we need the VueJS object to be inherited to have the types available and also to tell to VueJS that this is a component object.

To declare the textdata or any kind of data in your VueJS component, create simple a class property and initialize it with the “Hello” string:

```
private text: string = 'string'
```

this line is pretty much the same as:

{% gist https://gist.github.com/jefer590/0b86298d267cbef2c73bb8d873278272 %}

and to create the textUpper computed method, [we need to create a “getter”](https://www.typescriptlang.org/docs/handbook/classes.html#accessors)inside our component class

{% gist https://gist.github.com/jefer590/58e53795f9814716471bf01a51e1db36 %}

which, in the traditional way, translates to:

{% gist https://gist.github.com/jefer590/81c76965672cfe05a0d5cb0b288871f4 %}

After that, our “Hello.vue” Component should look like this:

{% gist https://gist.github.com/jefer590/e588a7e5a263f1585e5a844362071b9f %}

If you run the project with these changes you will notice that everything is working as expected.

### Translating the “World.jsx” Component — TSX Component

For this file, the first thing we need to do is rename it to use the .tsx file extension. If you are running the project, the hot reloading will fail because the compiler is not accepting how this file is done. Let’s translate this!

First, copy all the content of the file and put it somewhere else because we are going to totally rewrite the World.tsx file. After you clean the file, we will declare our Component class in the same way as the Hello.vue file:

{% gist https://gist.github.com/jefer590/f06e4a300235caeba905a0fe6d0b4faf %}

Let’s do the same as the Hello.vue with the data and computed property!

{% gist https://gist.github.com/jefer590/f959c1e66fb13dfe7bb78cb443dad81b %}

Since this is a TSX file, a render function will be needed. To type that function, the parameter of h needs to use the Vue.CreateElement Interface and the render function needs to return Vue.VNode which translates to the “HTML” returned in that function:

{% gist https://gist.github.com/jefer590/e26ae25b811bc934125486d643437499 %}

This is a good amount of code to check if everything is working. After trying, it will appear an error like this:

{% gist https://gist.github.com/jefer590/3571467b29cd9c6777288f18205bd47c %}

To fix this, in the Home.vue view, change the import of World.jsx to use TSX instead. After that, the project should run fine with the only problem that our text is not updating. Since this is TSX/JSX we need to attach the events manually. To fix that, we need to add a method to the on-input event of our input and create a class method that changes the value of text to use the passed text.

{% gist https://gist.github.com/jefer590/f8b6c1753a66f3ea96cc43a22496c73c %}

After this change, the text mutation should work fine!

### Translating the “Home.vue” View

The only thing left to update for now is the Home.vue file. Go to the file, copy the component imports and delete the script part. We will follow next the same pattern to create a class component but adding the component imports:

{% gist https://gist.github.com/jefer590/bc950f61f720c34912cc48816db19a54 %}

And to attach the Hello and World components, we will use the @Component decorator by passing an object with the components key as if it were a traditional VueJS object:

{% gist https://gist.github.com/jefer590/eaac432936d0df2021914db82dacdebd %}

After save, the project should start to work properly again.

#### That all for part 2 friends! Stay tuned for part three where we are going to translate the Vuex module to use Typescript and also how to use it inside a Typescript Class Component!

### 🙌 Thanks for reading! 🙌