Can someone please help me troubleshoot the issue of "Does not exist on type never" error?

My goal is to define a function that may or may not exist depending on the JSON passed to the parent function. The name and parameters of the function are unknown, but I do know it will be an array of arguments, and I will use the apply method on my function.

I have set up my interfaces with `any[]` for fnParams, which should work in theory. However, I keep encountering the error message "APPLY DOES NOT EXIST ON TYPE NEVER"

declare let window: any;

interface CallbackJSON {
    fnContext: string;
    fnName: string;
    fnParams: any[];
}

interface UpdateJSON {
  someKey: string;
  anotherKey: string;
  oneMoreKey: string;
  callBackFn: CallbackJSON;  
}

export default function updateFunc(e: JQueryEventObject){
  const json: UpdateJSON = $(e.target).data('json');  
  const {someKey, anotherKey, oneMoreKey, callBackFn} = json;
  let fnContext, fnName, fnParams, _callBackFn: false | any[] = false;

  //Setting up callback function if it exists
  if(typeof callBackFn === 'object') {
    fnContext = callBackFn.fnContext
    fnName = callBackFn.fnName
    fnParams = callBackFn.fnParams
    _callBackFn = (fnName ? (fnContext ? window[fnContext][fnName] : window[fnName]) : false);
  }

  /* Additional code execution */

  /* Running callback function only if it's a valid function */
  const runCallbackFn = function runCallbackFn() {
    if(typeof _callBackFn === 'function') {
      // APPLY DOES NOT EXIST ON TYPE NEVER
      _callBackFn.apply(null, fnParams);
    }
  }();
}

Answer №1

By specifying the type of _callBackFn as false | any[], you are essentially stating that it cannot be called.

To correct this, consider changing it to

_callBackFn: false | any[] | Function = false
.

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

Encountering error in Angular project: Unable to locate 'SpotifyApi' namespace from (@types/spotify-api)

I'm currently working on two projects simultaneously - one is an API project and the other is a client project built with Angular. In my Angular project, I've configured the tsconfig.json file like this: { "compileOnSave": false, &q ...

In TypeScript, what specific term denotes a type of data?

Given the following code snippet: class Foo { } interface TypeProvider() { type(): ?; } class Bar implements TypeProvider { type(): ? { return (Foo); } } class Baz implements TypeProvider { type(): ? { return (Bar); ...

Verify the completeness of data types within an array in typescript

I am currently developing a comprehensive match function that I want to ensure is exhaustive during compile time. Although adding a default case would help with this, I am intrigued by some interesting dependent typing techniques I have come across. It wou ...

Analyze the information presented in an HTML table and determine the correct response in a Q&A quiz application

I need to compare each row with a specific row and highlight the border accordingly: <table *ngFor="let Question from Questions| paginate: { itemsPerPage: 1, currentPage: p }"> <tr><td>emp.question</td></tr> <tr> ...

Attach [!hidden] to a dropdown menu choice using Angular 2

How can I implement a show/hide feature for a select box in Angular 2+? Here's what I have so far: <select> <option disabled selected>Flow progress</option> <option *ngFor='let flow of flows'>{{flow}}< ...

Encountering an issue when expanding the bookshelf model with TypeScript

I've set up my bookshelf instance in a configuration file as follows: // Omitted irrelevant code const knex = Knex(knexfile[env]); const bookshelf = Bookshelf(knex as any); const { Model } = bookshelf; export default Model; export { bookshelf }; E ...

AngularJS, sort through "afoo" excluding "foo"

I am attempting to implement a filter within an ng-repeat Main.HTML <table> <tr ng-repeat="param in MyParam | filter: UnrequestValue"> <td>{{param.Label}}</td> </tr> </table> Main.js MyParam: ...

Tips for creating a unique custom selector function to choose specific properties from an object

While developing a custom hook called useReduxState, I incorporated Redux's useSelector and lodash's pick module. To make it easier to understand, I have simplified my code for this question. Here is a snippet of what I came up with: interface IR ...

Monitoring for adjustments in variable with RXJS

One of my classes has a boolean variable called isLoading with a default value of false. It gets set to true when a certain action (HTTP request) occurs and then back to false once the action is completed. I am interested in using RXjs to monitor this vari ...

Checking nested arrays recursively in Typescript

I'm facing difficulty in traversing through a nested array which may contain arrays of itself, representing a dynamic menu structure. Below is how the objects are structured: This is the Interface IMenuNode: Interface IMenuNode: export interface IM ...

Tips for sidestepping the need for casting the class type of an instance

Looking to develop a function that can accept an argument containing a service name and return an instance of that particular service, all while ensuring proper typing. Casting the instance seems necessary in order to achieve the desired result. To illust ...

Is it possible to selectively export certain interfaces within a .d.ts file?

// configuration.d.ts export interface Configuration { MENU_STRUCTURE: Node[]; } interface Node { name: string; } Looking at the snippet above, I am aiming to only export Configuration. However, I noticed that I can also import Node from an ext ...

Observing Array behaviors in TypeScript

Struggling to generate an observable from an array due to the recurring error message (A tuple type element list cannot be empty). Despite multiple attempts, I'm unable to resolve this issue. listDonationsHistory(): Observable<[]> { const ...

An error occurred when attempting to access data within a variable that is undefined, resulting in a TypeError at the errorHandler function

Every time I attempt to send a post, patch, or put request, I keep getting this error. However, there are no issues with get requests. TypeError: Cannot read properties of undefined (reading 'data') at errorHandler (/home/joe/Documents/mypro ...

Updating Icons in TreeView in Custom ViewsContainer using Visual Studio Code

After my previous post, I've made some progress with a duplicate TreeView schema in my VS Code extension. You can find the code in this repository: https://github.com/trebleCode/dxdevcheck.git I have managed to display static JSON file contents in c ...

What causes TypeScript to malfunction when using spread in components?

What is the reason for typescript breaking when props are sent to a component using a spread statement? Here's an example code snippet: type SomeComponentProps = Readonly<{ someTitle: string }> const SomeComponent = ({ someTitle }: SomeCompo ...

What causes a boolean to evaluate to true?

Check out this code: export class Smth { private flag: boolean; public update() { this.flag = true; this.inner(); if (this.flag === false) { // Operator '===' cannot be applied to types 'true' and 'false'. ...

Enhance your list items created with ngFor by incorporating a variety of icons using Ionic

Is there a way to assign custom icons to individual items in a list created with ngFor? Would it be more effective not to automatically generate the menu items? app.html <ion-menu [content]="mainContent"> <ion-content id="side-menu" style="backg ...

Retrieve a particular element from an array within a JSON object using Ionic

I am currently facing a challenge in extracting a specific array element from a JSON response that I have retrieved. While I can successfully fetch the entire feed, I am struggling to narrow it down to just one particular element. Here is what my service ...

Troubleshooting Nested Handlebars Problem

After creating a customized handlebar that checks for equality in this manner: Handlebars.registerHelper('ifEquals', (arg1, arg2, options) => { if (arg1 == arg2) { return options?.fn(this); } return options?.inverse(t ...