Error in Typescript for a function that accepts several types of lists - property does not exist on the interface

In my project, I have defined three interfaces: a base interface and two others that extend the base one with children as properties. One of the interfaces includes 'valueType' while the other includes 'returnType'. Here is how they are structured:

interface IBase {
  id:string;
  label:string;
  value:string;
  expanded:boolean;
  isGroup:boolean;
}

interface IFieldDefinitions extends IBase {
  children: Array<IFiedValues>;
}

interface ICustomFunctions extends IBase{
  children: Array<ICustomFunction & {id:string;label:string;value:string;}>
}

interface ICustomFunction{
  name:string;
  args: Array<IFunctionArgs>;
  returnType: string;
  description: string;
  array: boolean;
}

interface IFiedDefValues {
  id: string;
  name: string;
  value: string;
  label: string;
  valueType: string;
  columnType: string;
  description: string;
}

I then created a function to group customFunctions and fieldDefinitions based on their returnType/valueType. Initially, I had separate functions for grouping but realized they were similar. So, I decided to create a single function like this:

public get groupFieldsByType(){
      return computeGrouping(fields,"fields");
}

public get groupFunctionsByType(){
     return computeGrouping(functions,"functions");
 }

The function definition looks like this:

function computeGrouping(listItems:IFieldDefinitions[] | ICustomFunctions[],type:string  ){
    
      let modifiedFieldDefs = [] as IFieldListboxItem[];
      listItems.forEach(items => {
  
       const filteredStringValues = items.children.filter(item => type === 'fields' ? item.valueType === 'Text' : item.returnType === 'String');
       const filteredNumericValues = items.children.filter(item => type === 'fields' ? item.valueType === 'Number' : item.returnType === 'Number');

    // Other computations go here and I return the grouped array;

     return modifiedFieldDefs;
}

 }

When implementing this, TypeScript throws an error stating that "valueType" is not available on ICustomFunctions and "returnType" is not available on IFieldDefinitions. How can I define the interfaces and access them in the function to avoid this error?

Answer №1

To achieve the desired outcome, you can utilize the item:

function customizeGrouping(itemList:IFieldDefinitions[] | ICustomFunctions[],category:string  ){
    
    let updatedFieldDefs = [] as IFieldListboxItem[];
    itemList.forEach(items => {
  
        const filteredStringValues = items.children.filter(item => category === 'fields' ? (item as IFiedDefValues).valueType === 'Text' : (item as ICustomFunction).returnType === 'String');
        const filteredNumericValues = items.children.filter(item => category === 'fields' ? (item as IFiedDefValues).valueType === 'Number' : (item as ICustomFunction).returnType === 'Number');

    });
    return updatedFieldDefs;
}

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

initiate an animated sequence upon the initialization of the Angular server

Is there a way to launch a Netflix animation after my server has started without success using setTimeout? I don't want to share the lengthy HTML and CSS code. You can view the code for the animation in question by visiting: https://codepen.io/claudi ...

Immersive Visual Symphony through Dynamic Multi-Layered

My goal is to create captivating animations for my multiple background images. Here's the CSS code I've come up with: .header { background-image: url('/img/cloud2.png'), url('/img/cloud3.png'), url('/img/cloud1.png&apos ...

WordPress AJAX call results in a response code of zero

TL;DR: Unique - Go straight to code below for issue. You can view the demo I am working on by following this link. In my `functions.php` file, I first localize my script: function ajaxurl(){ wp_enqueue_script( 'product-selector', get_templ ...

Creating an If statement tailored for a particular image source: a step-by-step guide

In the program I am running in Dreamweaver, there is a specific line of code that looks like this: function go() { if((document.test.test1.src == document.test.test2.src && document.test.test2.src == document.test.test3.src )) ...... ...... ..... ...

Guidance on retrieving a boolean value from an asynchronous callback function

I'm looking to determine whether a user is part of a specific group, but I want the boolean value returned in the calling function. I've gone through and debugged the code provided below and everything seems to be working fine. However, since my ...

Leveraging the browser's console for transmitting AJAX data

I've created a PHP quiz page that uses AJAX to post answer data when a user clicks on an answer. If the answer is correct, the page then loads the next question using another AJAX function. Here's a snippet of the code: <ul class="choices"> ...

Using app.js in a blade file can cause jQuery functions and libraries to malfunction

In my Laravel application, I am facing an issue with my vue.js component of Pusher notification system and the installation of tinymce for blog posts. Adding js/app.js in my main layout blade file causes my tinymce and other jQuery functions to stop workin ...

"Incorporate an image into the data of an AJAX POST request for a web service invocation

I have been attempting (with no success thus far) to include an image file in my JSON data when making a call to a method in my webservice. I have come across some threads discussing sending just an image, but not integrating an image within a JSON data o ...

execute the command only if all ava tests succeed

I'm working on creating an npm script that will execute Ava, and if it is successful, will then run another deploy command. Is there a way to obtain the result of an Ava test in JavaScript, or to save it to a file or pass it to a subsequent command? ...

Whenever a click event is triggered, the Vue method is executed twice

Why is the set method being executed twice? Check the console when you click the star. Removing @click="set(rating)" results in no action, indicating it is not called elsewhere. http://jsfiddle.net/q22tqoLu/ HTML <div id="star-app" v-cloak> ...

What is the method for retrieving the locale value from the configuration in Next.js?

How can I retrieve the i18n.defaultLocale value from my Next.js app configuration? I thought it would be simple, but I'm struggling to find a clear solution for accessing the config settings in Next.js. Is there a specific built-in method for this, or ...

Changing the map behavior when it is moved in Leaflet

I am currently using Leaflet to design a map that includes a personalized sound layer. Each tile on the map features an <audio> element, and I am exploring methods to adjust the audio playback as the user navigates around the map (specifically by mod ...

Locate the selected radio button's label

There are 25 radio button groups on my page. Each group has a specific action that needs to be performed when a radio button is selected. In order to execute the correct action for each group, I require the NAME attribute of that particular radio group. ...

How to create a CSS animation that gradually darkens a background image during a

Currently in the process of constructing a page with an intriguing background image: body { background:url(images/bg.png) center center no-repeat fixed; -webkit-background-size:cover; -moz-background-size:cover; -o-background-size:cover; ...

Running Windows commands from Node.js on WSL2 Ubuntu and handling escape sequences

When running the following command in the CMD shell on Windows, it executes successfully: CMD /S /C " "..\..\Program Files\Google\Chrome\Application\chrome.exe" " However, attempting to run the same comman ...

Utilizing an array to pass a series of items to a function parameter

I am currently working on an Angular project that involves a controller and service. In this setup, the controller sends data and an endpoint to the service. As of now, the service handles the http request. However, I am in the process of refactoring my ...

Is there a way for me to change the value and placeholder attributes on the Clerk's SignIn component?

Within Clerk's documentation, there is guidance on accessing the input field using the appearance prop as demonstrated below: <SignIn appearance={{ elements: { formFieldInput: 'bg-zinc-300/30' } }}/& ...

Retrieve the predetermined value from the dropdown menu option

I currently have two classes with a mapping structure as follows: User *--------------------1 Sexe Users are listed in the file list-users.component.html. When selecting a user for modification, I am redirected to the modify-user.component.html B ...

Eslint for Typescript in Vue is throwing an error with a message "Unexpected token, expecting ','. Make sure to

Whenever I attempted to utilize vue's type assertion, I kept encountering this eslint error. To illustrate, consider the following snippet: data: function () { return { sellerIdToListings: {} as any, // 'as' Unexpected to ...

differences in opinion between JavaScript code and browser add-ons/extensions

As the owner and developer of an e-commerce website, I find that potential customers often contact us regarding ordering issues. Upon investigation, we consistently discover that the problem is caused by JavaScript errors. We frequently check their browse ...