Is the Vue.js 2.x composition API completely compatible with TypeScript?

After migrating our vue.js 2.x project to typescript, I came across Evan You's comment in this issue:

https://github.com/vuejs/rfcs/pull/17#issuecomment-494242121

The Class API proposal has been dropped, so I switched to the regular option-based version of vue.js, which presented me with a lot of type problems.

I am wondering if vue.js 2.x composition api is fully compatible with typescript and should I use it to solve all these issues? What would be the best practice in this situation?

Answer №1

I'm not entirely clear on what it means for something to be "fully compatible with TypeScript." However, I can confirm that TypeScript works seamlessly with the Vue composition API, enhancing both code quality and developer experience.

If you're interested, here's a sample snippet showcasing the composition plugin in action with Vue 2:

import { computed, createComponent, reactive } from "@vue/composition-api";

export default createComponent({
  name: "Hello",

  template: `<p>{{ state.message }}</p>`,

  props: {
    name: {
      type: String,
      required: true
    }
  },

  setup(props, context) {
    const state = reactive({
      message: computed(() => `Hello, ${props.name}!`)
    });
    return {
      state
    };
  }
});

All of the above code is fully typed. The composition API elements (such as createComponent, reactive, computed) come with accurate type support. It's worth noting that thanks to the composition API, usage of this becomes unnecessary.

Answer №2

Absolutely, Vue.js seamlessly integrates with TypeScript thanks to the built-in TypeScript tooling support provided by Vue CLI. Simply install it using npm or yarn, as explained here. This means you won't need any extra tools to start using TS with Vue. The links also provide insight into recommended setup and fundamental usage.

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

Retrieve the string value from a deeply nested object

Describe this object: public serviceTable: Services[] = [ { 1: { service: 'Service 1', description: 'Lorem Ipsum', serviceIsMandatory: true, serviceIsDisabled: true, price: 100, hourlyRate:100, hours: 5} }, { 2: { service: 'Ser ...

The Post method in Next.js API does not display any data on the specified endpoint

Whenever I attempt to send a POST request from my frontend within a Next.js project (using TypeScript), the JSON data I expect at my API endpoint does not appear. Instead, all I see is an empty hyperlink that leads nowhere. The code for my API is placed i ...

How to apply an icon image using the background property in CSS in Vue 3

Need help setting background image from the public folder |_public | |_images | |_icon.png |_src |_components |_TheHeader.vue I'm having difficulty trying to set an image as a background in TheHeader.vue using CSS, but the image is located in ...

Utilizing PrimeNG's p-dataView feature without repetitive FieldSets

Currently, I am utilizing p-dataView and I'm interested in implementing p-fieldset based on the application type. My goal is to prevent the fieldset from being duplicated when multiple instances occur. The scenario below illustrates one such case; how ...

What is the best way to dynamically incorporate Before After CSS code in Vue?

I am facing a unique challenge in my current project. I need to dynamically apply colors from data inside a v-for loop, specifically for the :after CSS pseudo-element. While normal CSS properties are easily applicable, I am struggling with applying styles ...

Find out if a dynamically imported component has finished loading in Nextjs

Here is a simplified version of my current situation import React, { useState } from 'react'; import dynamic from 'next/dynamic'; const DynamicImportedComponent = dynamic(() => import('Foo/baz'), { ssr: false, loading ...

What is the process of uploading a file from a Vue.js app to the local public disk in Laravel?

Hello there, I am looking to upload a file from a Vue.js app to the local public directory of Laravel and only store the unique name in MySQL upon form submission. However, I am unsure how to achieve this using JavaScript. // Template Tag <input type= ...

Leverage the properties from a slot in Vue to use as local component data

Hello, I am currently using a layout that contains a slot where I render my pages. The layout fetches some data when rendered and passes it to the slot as props. While I can easily access the props within the template, I'm unable to use it as local da ...

What is the best way to fetch the chosen item in an Angular select dropdown

I am working on a dropdown that populates with items of type ObjectA. item.component.html: <label>items list: <select formControlName="itemsCtl" (change)="onChange()"> <option *ngFor="let item of itemList" [value]="item">{{i ...

"Learn how to expand and collapse individual row details with the help of Bootstrap-vue

Currently, I am utilizing the bootstrap-vue table feature in a project, but have encountered a challenge without clear documentation. Is there a way for me to toggle the details of one row at a time? If you'd like to see a complete example of a tabl ...

TypeORM is unable to locate the default connection within a class

I have encountered an issue while trying to incorporate TypeORM within a class. It seems to be unable to locate the default connection despite awaiting the connection. I have double-checked the configuration and tested it with .then(), which did work succe ...

The submit button in the Vue component fails to trigger the submission

I created a unique Vue component to serve as a substitute for traditional submit buttons. It allows me to pass in a handler function that the component executes after disabling itself and marking its status as loaded. In case of errors, it can recover and ...

React - Component continuously updates during form input

I am facing an issue where as I type in the login form modal, the entire component is re-rendering and causing me to only be able to type 1 or 2 letters before it resets. This is a problem that I have not encountered before. I attempted to factor out the ...

Function calls are currently not enabled. Please consider substituting the function or lambda with a pointer to an exported function

Incorporating APP_INITIALIZER into my app has been a bit of a challenge. In the app.module.ts file, I have configured it with all the necessary imports like so: @NgModule({ ... providers: [ ..., ContextService, { provide: APP_INITIALIZER, useFactory: ...

When a newer request is made, I would like to cancel my previous one

Currently, I am working with a text box that triggers an HTTP call for each input character. However, my challenge lies in the fact that I need to be able to cancel the previous request if the user enters the next character before receiving a response. ...

What is the best way to ensure that each service call to my controller is completed before proceeding to the next one within a loop in Angular?

Calling an Angular service can be done like this: this.webService.add(id) .subscribe(result => { // perform required actions }, error => { // handle errors }); // Service Definition add(id: number): Observable < any > { retu ...

Stop committing changes in Git when there are any TypeScript errors found

While working on my project in TypeScript using Visual Code, I encountered a situation where I was able to commit and push my changes to the server through Git (Azure) even though there was an error in my code causing a build failure. It made me wonder i ...

Issues encountered when trying to upload images to Firestore Storage

I am attempting to upload an image and store its URL in a Firestore document. To achieve this, I have the following code snippet: This function uses the device camera to capture the photo. selectImage(): Promise<any> { return new Promise(resolv ...

Error in syntax: The tailwind import statement contains an unrecognized word and will not function correctly

After configuring Tailwind CSS with Next.js, I made changes to the tailwind.config.js file. However, after making these changes, the compilation process failed and resulted in the following error: Error - ./src/assets/styles/global.css:3:1 Syntax error: Un ...

Encountering an error in Angular2 and TypeScript: TS2322 error message stating that the type 'Response' cannot be assigned to type 'UserStatus'

Currently, I am facing some challenges while working with Angular2 and TypeScript. Transitioning from AngularJS to Angular2 has proven to be a bit tricky for me. To better understand this new framework, I decided to create an experimental app with the foll ...