回顾这种方法,我只是好奇为什么它使用Object.keys(this).map(key => (this as any)[key])
? 只打电话给Object.keys(this).indexOf(type) !== -1
是否同样有效:
/**
* Checks if validation type is valid.
*/
static isValid(type: string) {
return type !== "isValid" &&
type !== "getMessage" &&
Object.keys(this).map(key => (this as any)[key]).indexOf(type) !== -1;
}
该行不会创建对象键的数组,而是创建对象值的数组并检查数组中是否包含type
。如果是这样会更清楚
/**
* Checks if validation type is valid.
*/
static isValid(type: string) {
return type !== "isValid" &&
type !== "getMessage" &&
Object.values(this).includes(type);
}
(当然,必要时包括填充物(