[英]Object equality comparison typescript. Cannot apply Object properties to generic type
Trying to obtain understand better object comparison in Javascript, along with the property access with typescript, I tried to make for my class an Equal comparer from this starting point.为了更好地理解 Javascript 中的对象比较,以及使用 typescript 的属性访问,我试图从这个起点为我的班级制作一个 Equal 比较器。
TypeScript : Object Equality Comparison (Object Equals Object) TypeScript:对象相等比较(对象等于对象)
I know this can be done with stringify and loadash, but I find it interesting to dig into the object properties to apply logic.我知道这可以通过 stringify 和 loadash 来完成,但我发现深入研究对象属性以应用逻辑很有趣。
This is the code to compare the two objects (from the link above):这是比较两个对象的代码(来自上面的链接):
private equals<T>(x: T, y: T): boolean {
if (x === y) {
return true; // if both x and y are null or undefined and exactly the same
}
if (!(x instanceof Object) || !(y instanceof Object)) {
return false; // if they are not strictly equal, they both need to be Objects
}
if (x.constructor !== y.constructor) {
// they must have the exact same prototype chain, the closest we can do is
// test their constructor.
return false;
}
for (const p in x) {
if (!x.hasOwnProperty(p)) {
continue; // other properties were tested using x.constructor === y.constructor
}
if (!y.hasOwnProperty(p)) {
return false; // allows to compare x[ p ] and y[ p ] when set to undefined
}
if (x[p] === y[p]) {
continue; // if they have the same strict value or identity then they are equal
}
if (typeof (x[p]) !== 'object') {
return false; // Numbers, Strings, Functions, Booleans must be strictly equal
}
if (!this.equals(x[p], y[p])) {
return false;
}
}
for (const p in y) {
if (y.hasOwnProperty(p) && !x.hasOwnProperty(p)) {
return false;
}
}
return true;
}
My problem is that I cannot access the Object properties constructor and hasOwnProperty.我的问题是我无法访问 Object 属性构造函数和 hasOwnProperty。 I obtain the error:
我得到错误:
Property 'hasOwnProperty' does not exist on type 'Vec3'.ts(2339).
“Vec3”.ts(2339) 类型上不存在属性“hasOwnProperty”。
I tried to implement the interface:我尝试实现接口:
interface ObjectPropertiesHandlable {
constructor: void;
hasOwnProperty: void;
}
So that in the declaration private equals<T extends ObjectPropertiesHandlable>(x: T, y: T): boolean {
I can fix the compilation error.所以在声明
private equals<T extends ObjectPropertiesHandlable>(x: T, y: T): boolean {
我可以修复编译错误。
But if so, the arguments here won't fit:但如果是这样,这里的论点就不适合了:
if (!this.equals(x[p], y[p])) {
return false;
}
As p is of type const p: Extract<keyof Vec3, string>
(by the typescript intellisense onHover)由于 p 是
const p: Extract<keyof Vec3, string>
类型const p: Extract<keyof Vec3, string>
(通过打字稿智能感知 onHover)
If I substitute the generic by my class type (it's a Vec3
class, however not relevant) although it has a constructor, I keep having the errors:如果我用我的类类型替换泛型(它是一个
Vec3
类,但不相关),尽管它有一个构造函数,我仍然有错误:
Property 'hasOwnProperty' does not exist on type 'Vec3'.ts(2339).
“Vec3”.ts(2339) 类型上不存在属性“hasOwnProperty”。
Property 'constructor' does not exist on type 'Vec3'.ts(2339).
类型“Vec3”.ts(2339) 上不存在属性“构造函数”。
If I apply an interface to avoid the object property error, I obtain this error:如果我应用一个接口来避免对象属性错误,我会得到这个错误:
to make it work, try next code :要使其工作,请尝试下一个代码:
type Indexed = {
[index: string]: unknown
}
const equals = <T extends Indexed>(x: T, y: T) => {
// ... your code
}
You just should asure TS that your arguments are actually objects.您只需向 TS 保证您的参数实际上是对象。
Please take a look on answer请看一下答案
UPDATE Try:更新尝试:
type Indexed = {
[index: string]: Indexed
}
I ti a little bit dirty, but if you will explain me what are U trying to achieve maybe I will provide better solution我有点脏,但如果你能向我解释你想要实现的目标,也许我会提供更好的解决方案
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.