After following the instructions on this particular website, I have implemented the following JavaScript code:
function A(id) {
var about = {
Version: "0.0.0.1",
};
if (id) {
if (window === this) {
return new A(id);//<-Error in typescript
}
this.e = document.getElementById(id);
return this;
} else {
return about;
}
};
A.prototype = {
doSomething: function () {
//Implement some logic here
return this;
}
}
Example of JavaScript Usage:
var result = A("test");//The type of result should be 'A'
result = new A("test");//The type of result should be 'A'
result = A("test").doSomething();//The type of result should be 'A'
result = new A("test").doSomething();//The type of result should be 'A'
I am now interested in developing a library based on TypeScript that follows similar usage patterns. What would be the best approach to achieve this?
If I attempt to simply place this in a '.ts' file and compile, an error is generated stating: Error TS2350: Only a void function can be called with the 'new' keyword.
In addition, I am unsure of how to handle usage both with and without the 'new' keyword in TypeScript, as my goal is to create a library that seamlessly integrates with JavaScript without necessitating the use of the 'new' keyword by the user.
P.S.
I am aware that I could create a javascript file along with a '.d.ts' file but that method does not align with my current objectives.