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);
}
}();
}