Vue defineProps allows for the definition of complex types for properties

Within my code, I am exploring the use of complex prop types in certain instances. Below is an example of what I have in mind:

enum Country {
  [...]
}

interface IPerson {
  firstname: string;
  lastname: string;

}

interface IAddress {
  street: string;
  houseNo: string;
  city: string;
  postalCode: string;
  country: number;
}

interface ITestComponentProps {
  person: IPerson;
  addresses: Array<IAddress>;
}

My goal is to utilize this structure in TestComponent.vue like so:

[...]
const props = defineProps<ITestComponentProps>();
[...]

If I were to incorrectly provide the props in a different structure, such as:

[...]
<TestComponent :props="'foo'" />
[...]

The expectation would be to receive some form of error message indicating the incorrect prop structure. However, according to Vue documentation:

Complex types and type imports from external files are not supported at present. There may be plans to introduce support for type imports in the future.


Is anyone aware of a workaround to achieve the desired behavior or when support for complex types will be implemented?

Answer №1

When using Vue version < 3.3, the syntax below is supported:

const props = defineProps<ITestComponentProps>();

This syntax only allows:

  • A type literal
  • A reference to an interface or a type literal in the same file

It is recommended to use the alternative syntax shown below:

import {PropType} from 'vue'

const props=defineProps({
       person:{
          type:Object as PropType<Person>
       }
  })

Benefits of using this syntax include:

  • Compatibility with Options API and composition API, regardless of script setup
  • Slightly more typing involved
  • Runtime warnings for incorrect basic type assignments (e.g., string instead of number)
  • Support for imported props objects

Credit to Linus borg for sharing insights in this comment

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

Exploring the capabilities of VueJs in detecting events triggered by parent components

When users click on a specific image in the Collection(parent component), my goal is to display that image in a Modal(child component). Below is the code snippet: routes.js import Home from './components/Home'; import About from './compone ...

What is the best way to update a Vue variable when the corresponding API variable has

I am looking to integrate VUE with Pano2VR. Pano2VR provides an API () that enables me to retrieve a variable value from Pano2VR and use it in Vue. I have written code that successfully retrieves the variable value from Pano2VR and assigns it to Vue. Whe ...

Utilizing a dynamically created Stripe checkout button

Currently, I am attempting to integrate a checkout button from the Stripe Dashboard into my VueJS Project. I have a feeling that I might not be approaching this in the correct manner, so if you have any advice, I would greatly appreciate it. In order to ...

Customizing global CSS for a specific page in Nuxt: A step-by-step guide

I am currently working on a project that involves multiple layouts. To style these layouts, I have added CSS globally in the nuxt.config.js file as shown below: css: [ '@/assets/plugins/fontawesome-free/css/all.min.css', '@/assets/ ...

Is there a way for me to receive the response from the this.$store.dispatch method in vue.js 2?

Here is the structure of my component : <script> export default{ props:['search','category','shop'], ... methods: { getVueItems: function(page) { this.$store.disp ...

Is there a way to include an object as a parameter in $router.push() method?

Is there a way to iterate through an array of objects and have the ability to click on an item to display a new page with data from another component? This is my router.js setup: { path: '/artists', component: () => import('../v ...

Retrieve the data of the current component from the slot template

This specific vue component showcases a CardGroup template with a headerRight slot. The content of the slot includes a message displaying the total number of items, accessed through the this.total data property. <template> <CardGroup> ...

Angular - Automatically filling in an empty input field upon dropdown selection

My goal is to create a DropdownBox that will automatically fill input fields based on the selected value. For example, selecting "Arnold" from the dropdown will populate another textbox with "Laptop". How can I accomplish this? { name:'Arnold', i ...

Files for the Express API and Sequelize are nowhere to be found

After collaborating with a Freelance developer for more than 10 months on a project, the developer suddenly disappeared without warning. Although he sent me a file containing the work he had completed, I realized that the backend API was missing. Project ...

Building a custom Vuetify extension to enhance core features

I am currently working on developing a plugin-based component library to ensure consistency across various product lines using vuetify. However, when I try to install the library and add the components, I encounter multiple errors related to the dark theme ...

What is the key to mastering any concept in Angular2 for optimal results?

When creating a Pipe to filter data, I often use the datatype any, allowing for flexibility with types. However, I have some questions regarding this approach: Is it considered a best practice? Does it impact performance when handling large amounts of da ...

Tips on transferring information to the graphical user interface

Here is my code snippet: signup.post('/signup', urlendcodedParser, async(req: Request, res: Response) => { const username = req.body.username; const password = req.body.password; const age = req.body.age; const email = req ...

What is the best way to implement Angular translation for multiple values in a typescript file, while also incorporating parameters?

this.snackBar.open( `Only files of size less than ${this.fileSizeAllowed}KB are allowed`, this.translate.instant('USER_REG.close'), { panelClass: 'errorSnackbar', ...

"Production environment encounters issues with react helper imports, whereas development environment has no trouble with

I have a JavaScript file named "globalHelper.js" which looks like this: exports.myMethod = (data) => { // method implementation here } exports.myOtherMethod = () => { ... } and so forth... When I want to use my Helper in other files, I import it ...

Tips on updating x-label formatting (to display months in words) utilizing morris.js with Angular 5

Snapshot of my Request. I'm looking to alter the xLabel (2018-01 - to - Jan) and I am utilizing morris.js. ...

Vue component not reflecting updated value from axios response

I am struggling to update the temp object after making an axios call. Any assistance would be greatly appreciated. This is the entire code snippet. I have implemented this component in a Vue application. Vue and components are new to me, so any guidance ...

Tips for enlarging the font size of a number as the number increases

Utilizing the react-countup library to animate counting up to a specific value. When a user clicks a button, the generated value is 9.57, and through react-counter, it visually increments from 1.00 to 9.57 over time. Here's the code snippet: const { ...

Can you please explain the meaning of this error generated by npm-check-updates?

After attempting to start a new project with Vue 3, Vuetify 3, Vue-router, and Pinia, I navigated to my c:\Vue directory where I normally store my source code. From there, I ran the command npm create vuetify. Here's what happened: Vuetify.js - M ...

Ways to verify if a function has completed execution and proceed to invoke another function

I am seeking to verify if a user has chosen an item from the ngFor form and then redirect them to another page upon submitting the form with the updated value. HTML: <mat-select placeholder="Treatment" [(ngModel)]="model.TreatmentA" name="TreatmentA" ...

Strategies for effectively choosing this specific entity from the repository

Is it possible to choose the right entity when crafting a repository method using typeorm? I'm facing an issue where I need to select the password property specifically from the Admin entity, however, the "this" keyword selects the Repository instead ...