What is the best way to set wrapper props without hiding it from showing up in attrs?

Creating a Wrapper Component with Specific Props

<script setup lang="ts">
import InputBase,{ Props as InputBaseProps } from "./InputBase.vue";


interface Props extends InputBaseProps {
  label?: string;
  labelClassName?: string;
...other props
}
const props = defineProps<Props>();
</script>

<template>
  // Custom logic for the wrapper component using the defined props
<InputBase v-bind="$attrs"  />
</template>

Implementation of the Wrapped Component

<script setup lang="ts">
import InputBase,{ Props as InputBaseProps } from "./InputBase.vue";


export interface Props {
  name:string;
...other props
}
const props = defineProps<Props>();
</script>

<template>
  // Logic specific to InputBase component implementation

</template>

Question on Extracting Wrapper Props and Passing Down to Base Component

The goal is to extract only the Wrapper Props and pass them to the base component. How can this be achieved in a way that allows easy extraction of InputBaseProps while maintaining type safety? Removing the "extends" keyword loses IntelliSense on the base component's props. Destructuring with toRefs has caused issues.

Answer №1

Using attrs may no longer be the most practical approach as it allows for passing the entire set, including event listeners. If this is not required, props can be manipulated using the reactivity API instead.

To use label and labelClassName in the wrapper component while passing the rest without any changes to the wrapped component, separate props objects can be created from the readonly reactive object props. Utilizing destructuring or type-aware helper functions like pick and omit can simplify this process:

const wrapperProps = computed(() => {
  const { label, labelClassName } = props;
  return { label, labelClassName };
});


const wrappedProps = computed(() => {
  const { label: _label, labelClassName: _labelClassName, ...rest } = props;
  return rest;
});

By ensuring that props is correctly typed, the types of computed props should be automatically inferred.

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

Execute a Vue.js script once all Axios calls have been resolved

In this scenario, it is necessary to execute the carResult function only after all axios requests have been completed. Placing it inside the success method of method2 won't work because the component ends up executing the code twice. It would be great ...

What is the process for upgrading TypeScript to the latest version?

Is there a way to upgrade TypeScript version for ASP.net MV5 project in Visual Studio 2015? I attempted searching through Nuget but couldn't locate it. There seems to be an issue with the razor intellisense (index.d.ts file) and I'm hoping that ...

Why does the Vue i18n $t('hello') not function properly within a data property until the page is refreshed, yet it works fine when used in a normal template?

**Below are the components I require assistance with: The home component contains an array that needs to have its title and content translated, then binded as props to the info component. Since $t does not work with data method, how can I accomplish this u ...

Dealing with Typescript Errors in Ionic3: How to Handle "Property 'x' does not exist on value of type 'y'" Issues

I stumbled upon this particular post (and also this one) while searching for a solution to my issue. I am looking to suppress these specific errors: 'Payload' property is missing from the 'Matatu' type. 'Key' property is no ...

Discovering Geo Coordinates with Vue.js

To ensure accuracy, I am setting a 10,000 millisecond timeout to retrieve the client's current geolocation. var options = { timeout: 10000 }; This function is written in JavaScript: function getCoordinates() { return new Promise(functi ...

Resolving the non-null assertion error in TypeScript and React: A step-by-step guide

My code snippet is as follows: type ItemProps = { status?: string; } <Items status={status!} /> // encountering an error with a warning about forbidden non-null assertion // @typescript-eslint/no-non- ...

Connect the Vue component to the Vue instance

I am currently working with a Vue component that looks like this: Vue.component('number-input', { props: {}, template: `<textarea class="handsontableInput subtxt area-custom text-center text-bold" v-model="displayValue" @blur="isInput ...

Stopping navigation in Vue router

<router-link to="SOME_ROUTE"> <div class="outer-div"> <div class="inner-div" v-on:click="doSomething"></div> </div> </router-link> I am currently working on creating a link with an image included. My goa ...

The Void Ionic type does not have the specified property available

Struggling to load markers when the button is clicked to find nearby gyms. Despite loading the map to my location upon click, no markers are displayed and an error indicating that then does not exist on type 'void' within IonViewDidLoad. gyms.ht ...

Replicating entities in TypeScript

I am currently developing an Angular 2 application using TypeScript. In a User Management component, I have implemented a table that displays all the users in my system. When a user is clicked on within the table, a form appears with their complete set of ...

What are the steps to expand the WebSocket interface using the 'ws' library?

I am struggling to extend an interface in a library and I need some help. I am trying to extend the WebSocket interface from the ws library. ... declare class WebSocket extends events.EventEmitter { ... } declare namespace WebSocket { ... } export ...

Issue with subscribing in a MEAN stack application

Currently, I have completed the backend development of my application and am now working on the frontend. My focus at the moment is on implementing the register component within my application. Below is the code snippet for my Register Component where I a ...

Combining objects with arrays to create a single unified data structure

I am trying to merge two array objects to achieve a specific structure, similar to the example below. "feedBackList" : [ { "questionNo" : 1, "scoring" : "5" }, { ...

Ways to access the content of the chosen <td> element using Vue?

I have a dynamic table where I retrieve data using $.ajax() from a database. The content in the rows is editable, and I need to save the changes made with vue.js. After updating the content, an $.ajax() function with 4 parameters (name, client_id, url, and ...

Avoid obtaining cookies within the TRPC environment

Recently, I've been setting up TRPC with Next.js and attempting to implement server-side rendering where I fetch user data before the page loads using the trpc.useQuery hook. However, I'm facing an issue where I cannot access the JWT token cookie ...

Angular not successfully passing ID in for loop

I am trying to pass the res[i].id value to my ArrayList while maintaining the sequence. Can anyone help me understand why 809 and 806 are not getting added to the arrayList correctly? 0: {id: 0, ArrayListID: 809, VarName: "TEST001A"} 1: {id: 0, ...

Tips for sorting an array with various data types in TypeScript while explicitly defining the type

I need help with a class that contains two fields, each being an array of different types but sharing the common field id. I am trying to create a method that filters the array and removes an item based on the provided id. enum ItemType { VEGETABLES = &a ...

Connecting two sets of data from a mongoDB database using vue.js

Hey there, I'm a newcomer to vue and mongodb. I've set up two collections - one for storing user details and the other for business details. When a business registers through a form, their information is saved in mongodb. Now, I've created a ...

Deploying a Vue/Nuxt application to Firebase as a Universal App with Server-Side Rendering (SS

I am currently facing challenges with deploying my Universal Nuxt.js app on Firebase. Despite attempting various methods, I have been unable to achieve full functionality. While SSR and hosting static assets seem to be functioning properly, middleware is ...

What are the steps to installing Typescript on my computer?

npm ERROR! encountered code EACCES during installation npm ERROR! while trying to create a directory npm ERROR! at path /usr/local/lib/node_modules/typescript npm ERROR! with error number -13 npm ERROR! Error: EACCES: permission denied, mkdir '/usr/lo ...