Dealing with Error TS2769 in Visual Studio Code when passing props to a custom component in Vue 2 with Typescript

I've encountered an issue with a Vue JS component that involves passing a custom prop. I am utilizing the Vue Options API without utilizing the class component syntax.

Whenever I pass ANY prop to my custom component the-header, I receive an error stating that typescript cannot recognize the overload on my component.

This error occurs regardless of which prop I try to pass, so it seems like there is something incorrect in my implementation.

Below is a minimal example, and I would greatly appreciate any assistance!

// App.vue
<template>
  <v-app>
    <the-header
    :first_name='user.first_name'
    />
  </v-app>
</template>
<script lang="ts">
import Vue from 'vue'
import TheHeader from './components/UI/TheHeader.vue'
export default Vue.extend({
  components: {
    TheHeader,
  },
  computed: {
    user (): User {
      return this.$store.getters['user/getCurrentUser']
    },
  },
})
</script>
// TheHeader.vue
<template>
  <nav>
    <v-app-bar
    app
    class="header">
  </nav>
</template>
<script lang="ts">
import Vue from 'vue'
export default Vue.extend({
  name: 'TheHeader',
  props: {
    first_name: {
      type: String as Prop<string>,
      default: 'User'
    },
  },
})
</script>

My tsconfig.json

{
  "compilerOptions": {
    "target": "esnext",
    "module": "esnext",
    "strict": true,
    "jsx": "preserve",
    "importHelpers": true,
    "moduleResolution": "node",
    "allowJs": true,
    "skipLibCheck": true,
    "esModuleInterop": true,
    "allowSyntheticDefaultImports": true,
    "sourceMap": true,
    "baseUrl": ".",
    "types": [
      "webpack-env",
      "jest",
      "node",
      "vuetify"
    ],
    "paths": {
      "@/*": [
        "src/*"
      ]
    },
    "lib": [
      "esnext",
      "dom",
      "dom.iterable",
      "scripthost"
    ]
  },
  "include": [
    "src/**/*.ts",
    "src/**/*.tsx",
    "src/**/*.vue",
    "tests/unit/*.ts",
    "tests/unit/*.tsx", 
    "tests/unit/setup/shims-vue.d.ts"
  ],
  "exclude": [
    "node_modules",
    "src/**/*.js",
    "src/**/*.jsx",
    "tests/**/*.js",
    "tests/**/*.jsx",
    "tests/e2e/*.ts",
    "tests/e2e/*.tsx"  
  ]
}

Error Message:

No overload matches this call.
  Overload 1 of 3, '(options?: ThisTypedComponentOptionsWithArrayProps<{ links: { route: {
 name: string; }; text: string; icon: string; }[]; } & { logout(): void; } & { user: User; 
registeredSince: string; } & { id: number; value: boolean; } & Vue, object, object, object,
 never> | undefined): CombinedVueInstance<...>', gave the following error.

  Overload 2 of 3, '(options?: ThisTypedComponentOptionsWithRecordProps<{ links: { route: {
 name: string; }; text: string; icon: string; }[]; } & { logout(): void; } & { user: User; 
registeredSince: string; } & { id: number; value: boolean; } & Vue, object, object, object,
 object> | undefined): CombinedVueInstance<...>', gave the following error.

  Overload 3 of 3, '(options?: ComponentOptions<{ links: { route: { name: string; }; text: 
string; icon: string; }[]; } & { logout(): void; } & { user: User; registeredSince: string;
 } & { id: number; value: boolean; } & Vue, DefaultData<{ ...; } & { ...; } & { ...; } & { 
...; } & Vue>, DefaultMethods<...>, DefaultComputed, PropsDefinition<...>, DefaultProps> |
 undefined): CombinedVueInstance<...>', gave the following error. (ts2769)

Answer №1

If you want to specify a prop type, you can utilize the PropType helper that is imported from the 'vue' library:

import Vue,{PropType} from 'vue'
export default Vue.extend({
  name: 'TheHeader',
  props: {
    first_name: {
      type: String as PropType<string>,
      default: 'User'
    },
  },
})

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

The onNodeContextMenuSelect function does not seem to be functioning properly within the p-tree

<p-tree [value]="files" selectionMode="single" (onNodeContextMenuSelect)="showContect($event)" > </p-tree> Whenever I right click, the event doesn't seem to be triggering. Instead, the default browser c ...

Exploring the contents of this JSON array

I'm trying to fetch data from this link: <script type="text/javascript"> $.getJSON('http://api01.notaion.com/?item&id=120001462', function(data) { }); </script> I am uncertain whether I need to use a callback=?, a ...

During operational hours, an Ajax request may cause interruptions to the website's functionality

Having an issue with a large ajax request: I am querying a PHP file that makes some cURL requests, taking 15-20 seconds to complete and then returning JSON on my webpage. It's a standard ajax request, but I found a strange bug. While the ajax query i ...

How do I access the previous and current values in a v-for loop in Vue.js in order to compare them?

I am working on a structural component that involves looping through a list and performing certain actions based on the items: .... <template v-for="(item, INDEX) in someList"> <template v-if="thisIsArrayList(item)"> ...

What is the best way to incorporate the correct SCSS file?

I have a specific scenario using Vue where I need to utilize different stylesheets based on configuration settings. In this case, I have a config file with the value type: "a", which means I must include and use the "a-setup.scss" style ...

JavaScript module encounters an uncaught error: Attempting to assign a value to a constant variable

In another module, I defined a variable in the following manner: // module1.js let directory; export { directory }; Now, I am trying to access it in a separate module like so: // module2.js import { directory } from '../js/module1.js'; directo ...

Error: Unable to call topFrame.window.changeSelectedBarStyle because it is not a valid function. What could be causing this

This function is called changeSelectedBarStyle: function changeSelectedBarStyle(tdId){ $("#menuTable td").each(function(index){ if(this.id == tdId){ $(this).removeClass("menuPanel"); $(this).addClass("menuPanelSelected" ...

Utilizing Stored Variables and Random Numbers in Selenium IDE

Can you explain how Selenium IDE handles stored variables (stored text) and random numbers? I've been trying to combine the two without much success. For example: <td>type<td> <td>css=input.some-text</td> <td>javascript ...

Here's a unique rewrite: "Learn how to manipulate the CSS class of a textarea element (specifically in NicEdit) by utilizing the addClass

I am currently validating a textarea using the NicEdit plugin. var getContent = nicEditors.findEditor("SubSliderDescription").getContent(); bValid = bValid && checkBlankTextArea(getContent, "SubSliderDescription") function checkBlankTextArea(o, ...

Why does replacing <input> with <TextField> in Material-UI & React cause the form submission to fail?

Recently, I encountered an issue with my CRUD Todo app's form for adding tasks. Initially built with a basic HTML input and button setup, I decided to enhance the design using Material-UI components. After introducing <TextField> in place of th ...

"Implementing a loop to dynamically add elements in TypeScript

During the loop session, I am able to retrieve data but once outside the loop, I am unable to log it. fetchDetails(){ this.retrieveData().subscribe(data => { console.log(data); this.data = data; for (var k of this.data){ // conso ...

Building an interactive array with checkboxes and mapping in React JS: A step-by-step guide

I am currently working on mapping a list of formations and would like to implement a dynamic array that updates when a checkbox is selected in the onChange event. The goal is to keep my organization updated with the formations they are able to dispense. ...

I have encountered limitations with useFormik where it does not accept null values for initialValues, even while utilizing a validationSchema

I am currently utilizing the useFormik hook to handle my form. The userId field is a select, so by default its value is set to null. However, my validationSchema requires this field to be populated before submission. const formik = useFormik<ApiCredit ...

What are some effective ways to utilize the data gathered from a subscribe() method in a different

handleKeyUp(event: any): void { this.technologiesService.retrieveData(event.target.value) .subscribe(data => { this.myResults = data; }); } The result of data is: I want to assign data as a property for later use. I've attempted ...

Retrieving the user's Windows username with JavaScript

Is it possible to retrieve the Windows user name solely in Internet Explorer using the code below? function GetUserName() { var wshell = new ActiveXObject("WScript.Shell"); alert(wshell.ExpandEnvironmentStrings("%USERNAME%")); } What methods ...

Submitting JSON data using JavaScript

My goal is to send a username and password to my backend server. However, I am encountering an issue where the data does not successfully reach the backend when using the following code: function register() { var text = '{"username":"admin1","pass ...

What is the best method for encrypting a URL that contains AngularJS data?

Here is the URL that needs to be encrypted: <a class="btn btn-success btn-sm btn-block" href="@Url.Action("myAction", "myController")?Id={{repeat.Id}}&HistoryId={{repeat.HistoryId}}" ng-cloak>View History</a> I am seeking guidance on enc ...

How to change a string from utf-8 to iso-8859-1 using Javascript

Although it may seem unpleasant, it is essential. I am facing an issue with a HTML form on my website that uses utf-8 charset but is sent to a server operating with iso-8859-1 charset. The problem arises when the server fails to interpret characters commo ...

Is there a way to adjust the offset in vue-scrollto specifically for mobile devices to ensure proper alignment?

I've been using the amazing vue-scrollto in combination with bootstrap and everything works perfectly on desktop, but there's an issue on mobile. The heading is not positioned correctly at the top of the screen. I tried adding padding to the head ...

Enhance the Facebook Login popup with personalized settings

When a user clicks, a popup is triggered: FB.login(function () { // My CallBack }, { scope: 'email,user_birthday,user_location,user_hometown' }); This generates a Facebook login link with multiple parameters. I desire to inc ...