Tips for integrating TypeScript with Vue.js and Single File Components

After extensive searching online, I have struggled to find a straightforward and up-to-date example of setting up Vue.js with TypeScript. The typical tutorials out there either are outdated or rely on specific configurations that don't apply universally. It seems like there is a lack of generic, reliable examples to work from.

Here are some of the resources I checked out:

I typically use the basic template generated by running vue-cli init webpack with default settings. Since it generates a lot of code, I won't paste it all here. But if specific parts are needed, I can provide them upon request.

The official Vue.js documentation hasn't been helpful in my context as it lacks guidance on using TypeScript with Single File Components (SFCs). The most recent attempt I made was following the last resource listed above. However, after precisely following the setup, I encountered an error when running npm run dev:

[tsl] ERROR in /Users/[REDACTED]/ts-test/src/main.ts(12,3)
      TS2345: Argument of type '{ el: string; router: any; template: string; components: { App: { name: string; }; }; }' is not assignable to parameter of type 'ComponentOptions<Vue, DefaultData<Vue>, DefaultMethods<Vue>, DefaultComputed, PropsDefinition<Rec...'.
  Object literal may only specify known properties, and 'router' does not exist in type 'ComponentOptions<Vue, DefaultData<Vue>, DefaultMethods<Vue>, DefaultComputed, PropsDefinition<Rec...'.

If anyone can offer insights into why this issue is occurring and how to resolve it, I would greatly appreciate it. Ideally, I'm seeking a clear, concise, step-by-step guide on setting up a functional Vue.js + TypeScript configuration using the webpack template.

Although I have successfully completed various client projects using Vue.js with plain JavaScript, integrating TypeScript into Vue.js has proven to be quite confusing for me.

Answer №1

Experimenting with typescript and vue did not go as smoothly as expected. Some of the vue internals are not compatible with typescript:

  1. Vuex and using
    this.$store.dispatch('some_action')
  2. Vue.use, Vue.mixin, and other global instances that mutate the core Vue

During my search, I came across some helpful boilerplates like: typescript-vue-tutorial by Daniel Rosenwasser and TypeScript-Vue-Starter by Microsoft.

I also attempted to create a vue-webpack-template using typescript on my own: https://github.com/sobolevn/wemake-vue-template

There are various tools available to enhance your typescript + vue workflow:

In the end, I opted to utilize flow. Explore this project template if you're intrigued.

Answer №2

latest update

In September 2018, it is recommended to experiment with the new vue-cli alongside the TypeScript Plugin.


I completely agree with @sobolevn's viewpoint. However, I feel that there is still a lot of information to consider before making a judgment, particularly in terms of community support for TypeScript.

The Vue ecosystem is increasingly embracing TypeScript:

  1. Support for TypeScript linting in *.vue files within VSCode. Check out this issue on vetur.

  2. Diversification within the Open Source Ecosystem.

  3. New and improved design for the vue-cli. Click here for details: click me

If you can afford to be patient, it might be beneficial to observe developments for a while. Alternatively, you can explore projects such as TypeScript-Vue-Starter and A Webpack Template Fork With Typescript Support.

Answer №3

This template appears to be the most current vue-cli option, boasting a high number of stars and compatibility with vue 2.5.

Interestingly, this specific template was not explicitly mentioned in any of the previous answers.

vue init ducksoupdev/vue-webpack-typescript my-project

Explore more here: https://github.com/ducksoupdev/vue-webpack-typescript

Answer №4

Utilizing the most recent VueJS updates allows for the development of a robust and sustainable project using Vue, Typescript, and JSX. To assist beginners, I have designed a boilerplate available at Vue-TSX Starter

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

pagination functionality incorporated into element ui tables

Issue with Element UI: when a checkbox is selected and the page is changed, the selected rows' checkboxes are removed. I need to retain the selection items while paging so that users can select items from multiple pages without losing the selections f ...

`How to retrieve query parameters using the GET method in AngularJS`

I have created a REST API in WSO2 ESB. Below is the request service for the POST method: createUser: function(aUser) { var myCreateUserRequest = { "User": { "UserName": aUser.Username, ...

Encountering issues trying to display state value retrieved from an AJAX call within componentDidMount in React

I recently implemented an AJAX call in my React application using Axios, but I am a bit confused about how to handle the response data. Here is the code snippet that I used: componentDidMount() { axios.get('https://jsonplaceholder.typicode.com/us ...

What is the best way to incorporate padding into an Angular mat tooltip?

I've been attempting to enhance the appearance of the tooltip dialog window by adding padding. While adjusting the width and background color was successful, I'm encountering difficulties when it comes to styling the padding: https://i.sstatic.ne ...

Combining Multiple Arrays into One | Node.js

Can someone explain how to merge an array of arrays to me? For instance, I have the following array: [ [ {brand: 'fiat', model: 'palio'} ], [ {brand: 'nissan', model: 'march'} ] ] I want to transform this array int ...

Activate the SHIFT key

In the input field for user's firstname, I have restricted all keys to a-z and 0-9 only. This restriction is working well, but I also want to allow the SHIFT key so that users can capitalize letters if needed. Despite trying various solutions, I have ...

Using Angular NgUpgrade to inject an AngularJS service into an Angular service results in an error message stating: Unhandled Promise rejection: Cannot read property 'get' of undefined; Zone:

I have noticed several similar issues on this platform, but none of the solutions seem to work for me. My understanding is that because our Ng2App is bootstrapped first, it does not have a reference to $injector yet. Consequently, when I attempt to use it ...

After a period of activity, requests to Node.js with Express may become unresponsive

My server is running smoothly but after some time, it stops responding to requests. When I try to load the page, it gives me a "couldn't establish connection" error message. Here is my app.js: var express = require('express'); var path = r ...

Struggling to transfer information between different components?

I recently implemented a delete button functionality in my project to remove elements when clicked, but I'm facing an issue where the input decorator is not properly receiving data for deletion. When I console log, it shows that the array is empty whi ...

Is there a way to dynamically replace a section of a link with the current URL using JavaScript or jQuery?

I have a link that appears on multiple pages, and I want to dynamically change part of the link based on the current URL* of the page being visited. (*current URL refers to the web address shown in the browser's address bar) How can I use JavaScript ...

Having trouble accessing a React component class from a different component class

I just started learning reactjs and javascript. For a simple project, I'm working on creating a login and registration form. The issue I'm facing is that when a user enters their email and password and clicks 'register', instead of movi ...

What is the most graceful method to define a class attribute just once?

Is there a clean and efficient way to set a value only once within a TypeScript class? In other words, is there a way to make a value read-only after it has been assigned? For instance: class FooExample { public fixedValue: string; public setFixe ...

Employing JavaScript for fading divs in and out sequentially

Hey there! I'm currently struggling with implementing transitions for my tool tips. Any assistance would be greatly appreciated! I am looking to have my "fader" divs fade in and out on click of a button, with each transition lasting 5 seconds. It&apo ...

Unusual Behavior in AngularJS: Using ng-include Triggers Full Page Reloading

An issue arises where a simple ng-include command causes the entire website to be printed recursively in a specific area of the page, ultimately crashing the browser. Even changing the path doesn't resolve the problem, suggesting that it's not ev ...

In JavaScript, escape is comparable to android decode

I used a JavaScript method to encode a string: var result = escape('Вася') The resultant string is: "%u0412%u0430%u0441%u044F" Now I need to decode this string in Java. How can I achieve this? The following attempt did not work: URLDecod ...

Understanding how to retrieve the FileType from a Document Object Model using JavaScript

Looking at this DOM structure, we have an image with the following details: <img id="this-is-the-image" src="http://192.168.1.100/Image_tmp/2016-06/d4eb8d"> The task at hand is to click a button, execute a JavaScript function, and download the ima ...

Creating custom shaders in three.js that can receive shadows involves a few key steps. Let

For more information, you can visit the original post here. The task at hand seems to be quite complex due to the lack of documentation and examples on this specific topic on the three.js website. Currently, I am able to render the correct image and, with ...

Revamp the code by implementing promises

Upon calling the code snippet below, the two Webtrends calls are getting cancelled instead of returning an HTTP 200 status. This happens because the form submission triggers the cancellation. To resolve this issue, I introduced a 2-second delay before subm ...

What is the best way to access a template in asyncData function?

Is there a way to access this.$el within asyncData? I have translations stored in a database and I need to retrieve a list of translations used on the current page. Once I have this list, I will send a request to the server to receive them. After receivi ...

Struggling to modify a string within my React component when the state is updated

Having a string representing my file name passed to the react-csv CSVLink<> component, I initially define it as "my-data.csv". When trying to update it with data from an axios request, I realize I may not fully understand how these react components w ...