/**
* Checks if the value is an instance of the specified object.
isInstance(object: any, targetTypeConstructor: new (...args: any[]) => any) {
return targetTypeConstructor
&& typeof targetTypeConstructor ==="function"
&& object instanceof targetTypeConstructor;
}
function isInstance(object: any, targetTypeConstructor: new (...args: any[]) => any) {
return targetTypeConstructor
&& typeof targetTypeConstructor ==="function"
&& object instanceof targetTypeConstructor;
class Jerry{
constructor(private name:string){
this.name = name;
const jerry: Jerry = new Jerry('Jerry');
console.log(isInstance(jerry, Jerry));
输出:true
如果把 new 关键字去掉,反而会报错:
Argument of type ‘typeof Jerry’ is not assignable to parameter of type ‘(…args: any[]) => any’.
Type ‘typeof Jerry’ provides no match for the signature ‘(…args: any[]): any’.