Does Vetur have additional undefined types in the type inference of deconstructed props?

When reviewing the code below, Vetur concluded that x,y are of type number | undefined.

The presence of undefined is leading to numerous warnings when using x,y further in the code.

Is there a way to eliminate the undefined from the type inference?

<script lang="ts">
import { defineComponent } from 'vue'

export default defineComponent({
  props: { x: Number, y: Number },
  setup(props) {
    let { x, y } = props
  },
})
</script>

Answer №1

Each prop is considered optional (meaning it could potentially be undefined) by default, so when typing your props, they are defined as number | undefined.

If you want to make the props required, you can use the full form of prop declaration, which eliminates the | undefined from the type of the prop:

<script lang="ts">
import { defineComponent } from 'vue'

export default defineComponent({
  props: {
    x: {
      type: Number,
      required: true, //👈
    },
    y: {
      type: Number,
      required: true, //👈
    },
  },
  setup(props) {
    let { x, y } = props
  },
})
</script>

see demo

On a side note, Vetur has been deprecated and it is recommended to use Volar as the officially supported VS Code extension for Vue 3 and TypeScript.

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 'property' is not found within the type '{ my_function(): void; }'

I am just starting out with TypeScript and VueJS. Currently, I am pondering the best approach for setting the type of a JSON key that should start off as null. <script lang="ts"> import Vue from 'vue'; export default Vue. ...

The table is failing to display the values contained within the array

My users list is successfully retrieved from the API and I can see the data in my console. However, when I attempt to map it and display it as a table, it doesn't seem to work as expected. This is the component I'm working with: interface IUser { ...

Using Typescript to extract elements from one array and create a new array

I have a set of elements "inputData" , and it appears as follows : [{code:"11" , name= "test1" , state:"active" , flag:"stat"}, {code:"145" , name= "test2" , state:"inactive" , flag:"pass"}, {code1:"785" , name= "test3" , state:"active" , flag:"stat"}, .. ...

Removing a portion of an item with the power of RxJS

I possess the subsequent entity: const myObject = { items:[ { name: 'John', age: 35, children: [ { child: 'Eric', age: 10, sex: 'M' }, { ...

TypeScript: implementing function overloading in an interface by extending another interface

I'm currently developing a Capacitor plugin and I'm in the process of defining possible event listeners for it. Previously, all the possible event listeners were included in one large interface within the same file: export interface Plugin { ...

Angular2 is throwing an error: "NavigationService provider not found! (MenuComponent -> NavigationService)"

I am in the process of developing an angular (beta7) application. I aim to have a MenuComponent at the top that utilizes the NavigationService to navigate throughout different sections of my app. To ensure that the NavigationService remains a singleton, I ...

Angular 10 encountering an issue with subject instantiation

I am encountering an issue with my Angular application. Everything runs smoothly with `ng serve`, and the application builds correctly using `ng build --prod`. However, when I attempt to run the generated sources in the browser, an error occurs: TypeError: ...

Tips for implementing lazy loading for a section of a template in Angular 2

I have an Angular 2 component that has several sub-components within it. Some of these sub-components are expensive to load and may not always be necessary, especially if the user doesn't scroll far enough down the page. Although I am familiar with l ...

Is it possible to validate a template-driven form without using the model-driven approach?

Attempting to validate a template-driven form in Angular without two-way data binding has proved to be challenging. I have successfully implemented validation using [(ngModel)], but running into an error when trying to validate the form without the MODEL p ...

Utilizing HTML imports in Typescript for efficient use as TemplateStringLiteral

Recently, I started using TypeScript for the first time and I'm looking to integrate it into my webpack build. Currently, I am working with ts-loader and babel-loader to load TypeScript files while also attempting to include HTML files within the scr ...

Retrieving the authenticated User within an API controller on Laravel

I would like to retrieve the Authenticated User's information in my API controller. Can someone provide guidance on how to achieve this? Below is the snippet from my API\CompanyController public function selected_company(){ return Auth: ...

Utilizing Service Workers for Optimal Vue Application Performance and Bundling

Utilizing Vue 2 with Common.js to create an AMD Bundle requires automatic registration of the service worker at runtime. I have been exploring options such as: https://www.npmjs.com/package/worker-plugin https://www.npmjs.com/package/worker-loader The p ...

Select numerous files and conveniently delete them using the angular delete button

Background: In one of my tables, there is a column where users can either choose or upload files as input. I have implemented a feature that allows users to select multiple files at once. Issue at Hand: What I am trying to achieve is to have an 'x&ap ...

The specified element "errors" is not found in the VeeValidate template

Trying to use VeeValidate for field validation in a Vue form with Vue 2.5 and VeeValidate 2.1, the following method is used: <input class="form-control" name="contact-email" id="contact-email" type="email" v-model="contact-email" v-validate="'re ...

Utilize Angular 5 to implement URL routing by clicking a button, while also preserving the querystring parameters within the URL

I have a link that looks like this http://localhost:4200/cr/hearings?UserID=61644&AppID=15&AppGroupID=118&SelectedCaseID=13585783&SelectedRoleID=0 The router module is set up to display content based on the above URL structure { path: & ...

Employing global variables in JavaScript files with Vue3

In my Vue3 project, I have defined global variables like this: app.config.globalproperties.$locale = locale A composable function has been created to dynamically retrieve these global variables: import { getCurrentInstance ) from 'vue' export f ...

Choosing between Vue.use and Vue.component for the main Vue component

When using Vue.use, the functionality becomes global, similar to when calling Vue.component in the root Vue component like app.vue. I recently came across a sample app that utilized both methods, with multiple calls of Vue.component and Vue.use within the ...

What is the method for defining a constant data type with a class property data type in typescript?

I've been working on developing a nestjs API and have been using classes to define my entities. For instance, I have created a Customer entity as shown below: export class Customer { id: number; name: string; } Now, while working on my Custom ...

Navigation shadow in Bootstrap not showing up on nav.shadow

I've created a navigation bar component using vue.js and am styling it with bootstrap by including the framework in the main HTML file. Although I've added the desired styling to the navbar component, I'm facing an issue where adding the sh ...

Saving data from Material UI forms in TypeScript

Is there an effective method for storing values entered into the text fields on this page? const AddUserPage = () => ( <div> <PermanentDrawerLeft></PermanentDrawerLeft> <div className='main-content'> < ...