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

Problem with Extending Jest Matchers in VS Code TypeScript

I've developed unique Jest matchers to enhance expect for handling AxiosResponse objects. Although I've followed the standard method for expanding Jest's matcher types, my custom matchers are not being recognized by TypeScript. The error di ...

Incorporate the Get Your Guide Widget into an Angular2 component

Currently, I am attempting to embed a "Get Your Guide" Widget within an Angular2 application. Although the script in index.html is being requested successfully, I am facing difficulties adding it to the HTML of the component. <script async defer src=" ...

Intercept Axios Responses - Retrieving API Responses for HTTP Statuses that are not in the 200 range

I've set up a custom Axios instance with interceptors for handling responses. As per the Axios documentation, the success interceptor is triggered for 2xx statuses while the error interceptor handles any other status codes. My goal is to show an error ...

I am looking to integrate VueJs to add inputs to an HTML form

Hey there, I'm looking to dynamically add input fields to an HTML form. Here's the HTML code snippet: <div class="form-group"> <a @click="addInput" clas ...

Having trouble managing TypeScript in conjunction with React and Redux

As a newcomer to TypeScript, I find myself struggling to grasp the concepts and know where to start or stop. While there are abundant resources available online, I have not been able to effectively utilize them in my project. I am hopeful for some guidance ...

Blocking negative values when a button is clicked in Vue.js using v-on:click

How can I prevent the counter from going below 0 when clicked in this Vue component? Do I need to create a separate method to block it? Thank you for your assistance. <button v-on:click="counter.document -= 1">-</button> <h3>{{coun ...

"Exploring Vue.js 2: the best way to retrieve data within a method

Currently, my setup involves single file components being triggered from vue-router. Within this setup, I have a components/Header.vue component that utilizes a child component called modals/HelpModal.vue, which in turn has another child component named co ...

What is the best way to implement a scroll-into-view feature for a newly added list item in Vue.js?

I am currently utilizing vueJS to create a task viewing application. My goal is to make the div containing the list focus on the newly added list item immediately after adding a new task. Here's the HTML code from my template for the task list: < ...

Refreshing Angular 9 component elements when data is updated

Currently, I am working with Angular 9 and facing an issue where the data of a menu item does not dynamically change when a user logs in. The problem arises because the menu loads along with the home page initially, causing the changes in data to not be re ...

Utilizing the useRef hook in React to retrieve elements for seamless page transitions with react-scroll

I've been working on creating a single-page website with a customized navbar using React instead of native JavaScript, similar to the example I found in this reference. My current setup includes a NavBar.tsx and App.tsx. The NavBar.tsx file consists o ...

The 'clientX' property is not recognized on the 'Event' type in Angular2 Directive

When attempting to retrieve the X position of my mouse in an Angular2 Directive using the following code: @HostListener('mousemove', ['$event']) onMousemove(event: Event): void { console.log(event.clientX) } I encountered an error ...

What is the method to insert a new <input> element after the last input field has been filled in

I recently started working on a form using StackBlitz, but I've hit a roadblock and need some guidance on how to proceed. My goal is to achieve a similar effect like the one shown in this gif: https://i.stack.imgur.com/76nsY.gif and I'd like to ...

Tips for managing onChange events in TypeScript

I'm still learning Typescript and I have a question regarding handling the onChange event in a TextField component when using Typescript. Can you provide guidance on how to approach this? I currently have a function called handleChangeDate(e: React. ...

What is the process for implementing a type hint for a custom Chai assertion?

As part of my typescript project, I decided to create a custom assertion for the chai assertion library. Here is how I implemented it: // ./tests/assertions/assertTimestamp.ts import moment = require("moment"); import {Moment} from "moment"; const {Asser ...

Vue.js and the flexibility of component paths

I decided to break up my vuejs application into components, but whenever I make updates to the architecture, it becomes a challenge to adjust the component paths accordingly. To simplify maintenance, I am attempting to store the component path in a setting ...

Is it possible to utilize Typescript and Browserify in tandem?

As I explore the compatibility of TypeScript and Browserify, one perplexing aspect stands out - they both utilize 'require' but for different purposes. TypeScript uses 'require' to import other TS modules, while Browserify employs it to ...

What is the correct way to use Observables in Angular to send an array from a Parent component to a Child

Initially, the task was to send JSON data from the parent component to the child component. However, loading the data through an HTTP request in the ngOnInit event posed a challenge as the data wasn't being transmitted to the child component. Here is ...

What's the best way to showcase an icon in a Vuetify 3 v-list-item component?

Is there a way to include an icon in a Vuetify 3 v-list-item now that v-list-item-icon has been removed in the latest version? My code snippet currently lacks an icon next to the title, and I'm seeking advice on how to integrate an icon (e.g. 'md ...

Error message: Duplicate identifier found in Typescript

Encountering an error while trying to run my angular-meteor client (ionic serve), specifically: [00:29:20] typescript: node_modules/meteor-typings/1.3/main.d.ts, line: 657 Duplicate identifier 'Status'. L657: type Status ...

Error encountered when attempting to install a new package by a composer

Dealing with macOS, Laravel, and Vue, while my partner works on Windows and updates the repository by installing DomPDF. However, whenever I attempt a composer update or install, I encounter the following error: Your requirements could not be resolved to ...