Following the tutorial on this page I have the following JavaScript:
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 () {
//Some logic
return this;
}
}
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 want to create a library based on TypeScript that has the same usage. How can that be done?
If I try to just put this in a '.ts' file and compile I get an error saying: Error TS2350: Only a void function can be called with the 'new' keyword.
Also I am not sure how the usage with and without the 'new' keyword can be achieved as I am trying to make a library in typescript that can be used in javascript and not to make the user have to use the 'new' keyword.
P.S.
I know I can create a javascript file with a '.d.ts' file but that is not the purpose.
new? what are you trying to achieve? also, what'sdoSomething? as it's not in the code at all. It will be best if you explain exactly what you want, because I'm not about to read that tutorial you linked to