我正在阅读class-validator库的代码,其中包含以下isInstance
方法:
/**
* 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;
}
对于如何理解类型new (...args: any[]) => any
,你有什么想法吗?这是我第一次见到这种构造方式...
new (...) => ...
表示构造函数。 - Patrick Robertsfunction (object, targetTypeConstructor)
,以及在运行时情况下,也许targetTypeConstructor是其他类型而非函数(用户错误场景)吗? - Ole