I'm struggling with calling a JavaScript file from TypeScript. After resolving one import issue and adjusting the base function for tsc recognition, I'm now stuck on recognizing a declared function prototype in the JavaScript file.
Although I have set "allowJs": true
.
This is what my fileTransfer.ts looks like:
import { XmlRpcRequest } from "./mimic";
const updateCommentBtn: HTMLButtonElement = document.getElementById(
'makeComment',) as HTMLButtonElement;
updateCommentBtn.addEventListener('click', async () => {
const method = "MakeComm";
let request:any = XmlRpcRequest("http://localhost:1337/RPC2", method);
request.addParam(document.getElementById("n1")).value;
request.addParam(document.getElementById("n2")).value;
let response = await request.send();
console.log(response);
});
And here are the key parts of the mimic.js file that I am importing:
export const XmlRpcRequest = (url, method) => {
this.serviceUrl = url;
this.methodName = method;
this.crossDomain = false;
this.withCredentials = false;
this.params = [];
this.headers = {};
};
XmlRpcRequest.prototype.addParam = (data) => {
// Variables
var type = typeof data;
switch (type.toLowerCase()) {
case "function":
return;
case "object":
if (!data.constructor.name){
return;
}
}
this.params.push(data);
};
The project compiles successfully with tsc and there are no linting errors. However, Chrome's console displays the following error:
mimic.js:8 Uncaught TypeError: Cannot set property 'addParam' of undefined
It seems like an issue with accessing the exported function's prototype, but I'm unsure how to resolve it. Worth mentioning, the file runs without any problems in a pure JavaScript environment; the issue only arises when transitioning to TypeScript.