I have write a class function that return the instance of myClass if it is not the instance of the class.
function myClass(){
if ( this instanceof myClass )
return myClass();
}
Hence, both new myClass() and myClass() and get the instance of the myClass.
myClass(); // return myClass instance
new myClass(); // return myClass instance
However, I don't know how to declare the above class in typescript declaration file.
If I write that:
class myClass{}
function myClass(): myClass;
This will be duplicate identifier error.
Is there any method to fix that? thanks!
// Uppercase first letter!
class Greeter {
greeting: string;
constructor(message: string) {
this.greeting = message;
}
}
// lowercase first letter
function getGreeterInstance() {
return new Greeter("Hello");
}
myClass
is not clear enough for who reads the code. Make sure to check out TypeScript's documentation on classes and functions .
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.