You cannot employ typed arguments in combination with Typescript within the VueJS framework

I'm struggling to develop a typescript vue component with some methods. Here is the script snippet.

<script lang="ts">
import Vue from 'vue';
export default Vue.extend({
    methods: {
    check(value: number) {
      console.log(value)
    },
  },
});
</script>

However, I keep encountering this error message:

Module parse failed: Unexpected token (22:15)
You may need an appropriate loader to handle this file type.
|   },
|   methods: {
>     check(value: number) {
|       console.log(value);
|       this.value = value;

The issue seems to be related to the typed :number argument in the method. When I remove the typing, the error disappears.

Why is this happening? How can I fix this error?

Below is my package.json configuration:

{
  "name": "rating-form",
  "version": "0.1.0",
  "private": true,
  // Other dependencies and scripts
}

And here is my tsconfig.json setup:

{
  "compilerOptions": {
    "target": "es5",
    "module": "esnext",
    // Other compiler options
  },
  // Include and exclude paths
}

Answer №1

If you're working with a vue cli build project, there's a chance that the configuration is not properly set up to utilize typescript.

To fix this, include typescript and @vue/cli-plugin-typescript in your project:

npm install --save-dev typescript @vue/cli-plugin-typescript

Don't forget to add a tsconfig.json file at the root of your project as well.

Here's an example of what the tsconfig.json file might look like:

{
  "compilerOptions": {
    "target": "esnext",
    "module": "esnext",
    "strict": true,
    "jsx": "preserve",
    "importHelpers": true,
    "moduleResolution": "node",
    "experimentalDecorators": true,
    "esModuleInterop": true,
    "allowSyntheticDefaultImports": true,
    "sourceMap": true,
    "baseUrl": ".",
    "lib": [
      "esnext",
      "dom",
      "dom.iterable",
      "scripthost"
    ]
  },
  "exclude": [
    "node_modules"
  ]
}

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

Struggling with TypeScript and JsObservable? Let us assist you!

Having previous experience with JSRender, JSViews, and JSObservables, I recently embarked on a new project using TypeScript. Unfortunately, I am struggling to understand how to properly utilize TypeScript in my project, especially when it comes to referenc ...

Make multiple axios.get requests to retrieve and show data from the specified endpoint repeatedly

I am currently working on a backend built in Flask that provides a REST API for various tasks, one of which involves requesting deployment. Once the request is made, logs are stored in the database. To address this, I have created another REST API endpoint ...

Utilizing the power of Vue with Onsen UI components to manage events

I'm currently working on implementing the onsen actionsheet and I am trying to activate an event once the actionsheet is hidden. https://i.stack.imgur.com/sR06W.png I have attempted the following: new Vue({ el: '#app', template: & ...

Methods in Ionic to call an external JavaScript file from TypeScript

Having a JSON list stored in a JavaScript file, my objective is to filter it using TypeScript before sending the filtered results to the HTML homepage. However, I encountered an issue within the HTML file. It's worth mentioning that when running the ...

Running a function in a different vue file from an imported file - A step-by-step guide

Learning the ropes of vue.js, I am working with two separate vue files: First file: import second from second.vue; export default{ component: {second}, data() {return{ data: 'data', }} methods: { function f1{ console.log(t ...

What is the best way to showcase nested array JSON data in an HTML Table?

https://i.stack.imgur.com/OHL0A.png I attempted to access the following link http://jsfiddle.net/jlspake/v2L1ny8r/7/ but without any success. This is my TypeScript code: var viewModel = function(data){ var self = this; self.orders = ko.observableArr ...

Set the component variable to hold the output of an asynchronous method within a service

As I work on developing an application, I aim to keep my component code concise and devoid of unnecessary clutter. To achieve this, I plan to offload complex logic into a service which will then be injected into the component. Suppose my component includes ...

Tips for testing FormGroupDirective within a component

I am facing difficulties in testing a component with FormGroupDirective in the viewProviders section. I am unable to create a mock of the parent and set an empty formGroup. The component code is as follows: @Component({ (...) viewProviders: [ ...

Triggering events in vue-router

I have two components called MyItem.vue and ChangeItem.vue. I'm attempting to emit an event in the ChangeItem.vue component and catch it in the MyItem.vue component, but for some reason, it's not working as expected. MyItem.vue <template> ...

Executing methods sequentially in the ngOnInit lifecycle hook consecutively

Working with Angular15 has presented me with a challenge. In my app.component.ts file, I have two methods: “ngOnInit()”, as shown below. public ngOnInit(): void { this.getToken(); this.UserLoggedIn(); } I am looking to run the above two functions in ...

Sending parameters with a linked Get request leads to a 404 error page

Exploring how to interact with an external API using a TS/Express server. The user will choose a product, triggering a GET request to the server, which then queries the external API for pricing data. This is a project for fun and learning purposes, so ple ...

What is the reason behind this strange alert coming from Vue / Vuetify / Vite?

Currently, I am setting up an array of Vuetify 'chips' that have the ability to drag data from one chip to another: <v-container id="endgrid" style="max-width: 300px; position: relative;"> <v-row v-for="(row,r) ...

I am debating whether to retrieve individual items in vuex using getters, actions, and SQL queries

Apologies if this question seems a bit unusual, but I'm struggling to articulate it clearly. I am currently developing a basic fullstack application using Vue, Vuex, Express, and Postgresql. As I retrieve data from my database and display it on the ...

Protect your Laravel and Vue SPA from CSRF attacks by securely storing the JWT token in an HttpOnly cookie

Frontend uses Vue.js for the single page application (SPA). The backend is built on Laravel framework. The frontend and backend are decoupled, communicating with each other through xhr requests. Initially, I stored the JWT token in local storage for aut ...

Testing the Child Component's EventEmitter Functionality

In my child component, there is an event emitter that sends a boolean value when the style changes in the parent component: export class ExampleComponent implements OnInit, OnChanges { @Output() statusEvent = new EventEmitter<boolean>(); getS ...

Do not attempt to log after tests have finished. Could it be that you overlooked waiting for an asynchronous task in your test?

Currently, I am utilizing jest in conjunction with the Vue framework to create unit tests. My test example is successfully passing, however, I am encountering an issue with logging the request. How can I resolve this error? Is there a mistake in my usage o ...

Can you explain the concept of static in Typescript?

Exploring the distinctions between the static and instance sides of classes is addressed in the Typescript documentation on this page. The static and instance sides of classes: understanding the difference In object-oriented programming languages like ...

Upon updating my application from Angular 14 to 16, I encountered an overwhelming number of errors within the npm packages I had incorporated

After upgrading my angular application from v14 to v16, I encountered numerous peer dependencies issues, which led me to use the --force flag for the upgrade process. However, upon compiling, I am now faced with a multitude of errors as depicted in the scr ...

Encountered an issue while trying to run the production script. Received an error when attempting to

Encountering a problem when running the command below npm run production Upon executing the command, the following error is displayed: ERROR Failed to compile with 5 errors error in ./resources/assets/sass/app.scss Module build failed: ModuleBuildErro ...

Inter-component communication in Angular

I am working with two components: CategoryComponent and CategoryProductComponent, as well as a service called CartegoryService. The CategoryComponent displays a table of categories fetched from the CategoryService. Each row in the table has a button that r ...