Fixing prop passing issues in Vue.js components to prevent errors

My Vue-cli install with TypeScript is set up to render JSX using a boilerplate. However, I am facing an issue when trying to pass a property to the HelloWorld component. Here's what my code looks like:

export default Vue.extend({
  name: 'App',
  functional: true,
  render() {
    return (
      <div id="app">
        <HelloWorld msg="Welcome to Your Vue.js + TypeScript App" />
      </div>
    );
  },
});

Even though I managed to successfully pass the msg prop by adding the context in the functional component file like this:

export default Vue.extend({
  name: 'HelloWorld',
  functional: true,
  props: {
    msg: String,
  },
  render(h, context) {
    return (
      <div class="hello">
        <h1>{ context.props.msg }</h1>
      </div>
    );
  },
});

However, despite the functionality working as expected and the string rendering in the HTML, I'm still receiving a complex error message in my terminal:

ERROR in /home/scott/convrrt-component/view-play/src/App.vue(25,21):
25:21 No overload matches this call.
  Overload 1 of 3, '(options?: ThisTypedComponentOptionsWithArrayProps<{ msg: string; } & Vue, object, object, object, never> | undefined): CombinedVueInstance<{ ...; } & Vue, object, object, object, Record<...>>', gave the following error.
    Type '{ msg: string; }' is not assignable to type 'ThisTypedComponentOptionsWithArrayProps<{ msg: string; } & Vue, object, object, object, never>'.
      Property 'msg' does not exist on type 'ThisTypedComponentOptionsWithArrayProps<{ msg: string; } & Vue, object, object, object, never>'.
  Overload 2 of 3, '(options?: ThisTypedComponentOptionsWithRecordProps<{ msg: string; } & Vue, object, object, object, object> | undefined): CombinedVueInstance<...>', gave the following error.
    Type '{ msg: string; }' is not assignable to type 'ThisTypedComponentOptionsWithRecordProps<{ msg: string; } & Vue, object, object, object, object>'.
      Property 'msg' does not exist on type 'ThisTypedComponentOptionsWithRecordProps<{ msg: string; } & Vue, object, object, object, object>'.
  Overload 3 of 3, '(options?: ComponentOptions<{ msg: string; } & Vue, DefaultData<{ msg: string; } & Vue>, DefaultMethods<{ msg: string; } & Vue>, DefaultComputed, PropsDefinition<...>, Record<...>> | undefined): CombinedVueInstance<...>', gave the following error.
    Type '{ msg: string; }' is not assignable to type 'ComponentOptions<{ msg: string; } & Vue, DefaultData<{ msg: string; } & Vue>, DefaultMethods<{ msg: string; } & Vue>, DefaultComputed, PropsDefinition<...>, Record<...>>'.
      Property 'msg' does not exist on type 'ComponentOptions<{ msg: string; } & Vue, DefaultData<{ msg: string; } & Vue>, DefaultMethods<{ msg: string; } & Vue>, DefaultComputed, PropsDefinition<...>, Record<...>>'.
    23 |         // Needed to add in .eslintrc in rules a property of:  "global-require": 0
    24 |         <img alt="Vue logo" src={require('@/assets/logo.png')} />
  > 25 |         <HelloWorld msg="Welcome to Your Vue.js + TypeScript App" />
       |                     ^
    26 |       </div>
    27 |     );
    28 |   },
Version: typescript 4.1.6

This error seems contradictory given that the functionality works, and the prop 'msg' is properly defined in the HelloWorld component.

Question: What could be causing this error message in the terminal, and how can it be resolved?

Answer №1

Update: It appears that there is a known compatibility issue with Vue2 and tsx files. It seems best to avoid using tsx files in Vue2 as current plugins are not effective in resolving this issue. However, Vue3 does not seem to have this problem.

Referring to the types file of Vue

  render?(this: undefined, createElement: CreateElement, context: RenderContext<Props>): VNode | VNode[];

Based on this information, it is recommended to avoid using this within the render function and instead utilize context.

It is suggested to specify a type and potentially a default value for your msg prop

import Vue from 'vue';

export default Vue.extend({
  name: 'HelloWorld',
  functional: true,
  props: {
    msg: {
      type: String,
      default: ''
    }
  },
  render(h, context) {
    return (
      <div class="hello">
        <h1>{context.props.msg}</h1>
      </div>
    );
  }
});

I have not encountered any errors when using TypeScript and Vue2 JSX.

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

Creating Class Names Dynamically in Angular 2 Using ngFor Index

Encountering an issue while trying to generate a dynamic class name based on the ngFor loop index in Angular 2. Due to restrictions, I had to use a specific syntax as Angular 2 does not support ngFor and ngIf together on the same element. Given this setup ...

Executing a function within JSX to dismiss a modal in NextJS

I am currently utilizing the Tanstack React Query library to perform a POST request from a Modal that includes a form: const addDay = (day: TDay) => { const apiURL = process.env.NEXT_PUBLIC_SERVER_URL const queryURL = apiURL + router ...

Issue with service injection within a singleton service in Angular2

Currently, I have two services in my application. ServiceA is non-singleton and provided to components through the Providers array, while ServiceB is a singleton that is listed in its module's Providers array. Both services work perfectly fine indepen ...

Having trouble showing the fa-folders icon in Vuetify?

Utilizing both Vuetify and font-awesome icons has been a successful combination for my project. However, I am facing an issue where the 'fa-folders' icon is not displaying as expected: In the .ts file: import { library } from '@fortawesome/ ...

Angular validation with input binding using if statement

I have developed a reusable component for input fields where I included a Boolean variable called "IsValid" in my typescript file to handle validation messages. Here is the code from my typescript file: export class InputControlsComponent implements OnIn ...

Can one obtain a public IP address using Typescript without relying on third-party links?

Though this question has been asked before, I am currently working on an Angular 4 application where I need to retrieve the public IP address of the user's system. I have searched on Stackoverflow for references, but most posts suggest consuming a th ...

Extracting the CSS styles utilized by individual components in a Vue project

Description In my Vue 2 project, I have integrated bootstrap and an additional UI kit. However, customizing the CSS of my components has proven to be a challenge due to the complex abstractions present in the UI kit's variables. Looking For I am ...

Using React Native components from an external package leads to errors

I created a collection of React Native components by utilizing this template to seamlessly integrate Storybook. Additionally, I incorporated nativewind, which essentially serves as a React Native adaptation of Tailwind CSS. The structure of my components i ...

An issue occurred in React 16.14.0 where the error "ReferenceError: exports is not defined" was not properly

As the creator of the next-translate library, I have encountered a perplexing issue with an experimental version involving an error specifically related to React 16.14.0. Interestingly, upgrading React to version 17 resolves the issue, but I am hesitant to ...

Scrolling to a specific position on a page when a Vue 3 component appears is a must-do for

When I click a button, my basic form component appears without using the router. I would like the scroll position of the form to automatically move down slightly (for example, on the y-axis at 40) once it is displayed. However, I am unsure of how to achi ...

Issue encountered when working with interface and Observable during the http parsing process

Visual Studio File Structure Error app(folder) -->employee-list(folder) -->employee-list.component.html -->employee-list.component.ts -->app.component.html -->app.component.ts -->app.module.ts -->employee.json ...

When using a typescript subscription to collect data from an API, the information is stored in an array. However, only one piece of data can be

I have implemented a method to fetch data from an API using Angular: ngAfterViewInit() { this.exampleDatabase = new ExampleHttpDatabase(this._httpClient); var href = '/schuhe-store/status'; if (environment.production === false) { href ...

What is the recommended Vue js lifecycle method for initializing the materialize dropdown menu?

https://i.stack.imgur.com/cjGvh.png Upon examining the materialize documentation, it is evident that implementing this on a basic HTML file is straightforward: simply paste the HTML code into the body and add the JavaScript initializer within a script tag ...

Exploring the Relationship Between Redux and ImmutableJS in Managing Nested State and Understanding the Computational Complexity of Immutable

Trying to grasp the concept of Immutability for my debut Redux (NGRX/Store) endeavor has been quite the challenge. Avoiding state mutation has been a struggle, especially while dealing with Object.assign({}) and state mutation errors. Thankfully, I stumble ...

Customize Material-UI Icons styles in React app

I'm working on a React.js app with Typescript and I need to remove the default visited Material Icons coloring from an anchor tag. Here's the stylesheet I tried: const useStyles = makeStyles((theme: Theme) => createStyles( myAnchor: ...

Utilizing Axios to make a GET request and showcasing the outcomes on HTML using Vue

Recently delving into Vue and Axios, I am in the process of fetching data from an API to display it within an HTML card. However, it appears that the data is being attempted to be displayed before being retrieved from the API, resulting in nothing renderin ...

Typescript: parameter must be included if another is also required

In the user interface, the parameter c is mandatory only if the parameter a is set to true. interface IArguments { a: boolean, b: string, c: string } The solution below seems to be effective, but how can I exclude the c parameter in the first scenar ...

Implementing setDoc with Firebase-Admin using Typescript in Firestore

I'm having issues with my code in config/firebase.ts: import { initializeApp, cert } from 'firebase-admin/app'; import { getFirestore } from 'firebase-admin/firestore' const firebaseAdminApp = initializeApp({ credential: cert( ...

When working with the Sequelize-Typescript One To Many Association and Repository, a situation may arise where the query returns only one child entity even though there are multiple

Dealing with Sequelize-Typescript, I recently encountered the one-to-many association involving "Album" and "Photos" entities. Each "Album" can have multiple "Photos". Below are the entity codes for reference: Album.ts ` @Table({ timestamps: true, de ...

Developing a Typescript module, the dependent module is searching for an import within the local directory but encounters an issue - the module cannot be found and

After creating and publishing a Typescript package, I encountered an issue where the dependent module was not being imported from the expected location. Instead of searching in node_modules, it was looking in the current folder and failing to locate the mo ...