Open Dimava opened 1 year ago
As far as I understand, you want to match the type Record
(object
) of the typescript, which a priori does not have object methods (__proto__
, constructor
, valueOf
etc) with the method of its initialization:
let a = new Record([['a', 1], ['b', 2]]) // object `a` has no `prototype` and `__proro__` in ts and runtime
instead of the usual:
let a = {a: 1, b: 1} // object `a` has no `prototype` and `__proro__` in ts, but has in runtime
Did I get your point right?
I would say the original point was being able to use typed Record.keys
/ Record.entries
for objects you know are records (to avoid typing Object.keys
/ Object.values
) and then it's just unwrapped from there to its logical conclusion of having a complete RecordConstructor
which can make nullproto records
I would say the original point was being able to use typed
Record.keys
/Record.entries
for objects you know are records (to avoid typingObject.keys
/Object.values
) and then it's just unwrapped from there to its logical conclusion of having a completeRecordConstructor
which can make nullproto records
That's what I thought at first. But I was confused that in javascript the prototype of an object does not affect to the output of Object.keys
/entries
:
let tt = Object.setPrototypeOf({a: 1}, {b: 2})
console.log(Object.keys(tt)) // will be printed just ['a'] !
console.log(tt.a) // will be printed 1
console.log(tt.b) // will be printed 2
Thus, I did not see how the presence of __proto__
and constructor
in runtime or typescript could break Object.keys
behavior.
However, I see that the construction is to some extent more type-safe than the usual work with objects, it cannot protect against type covariance like this:
let a = new Record([['a', 1]])
let aa = {a: 1, b: 2, c: ''}
a = aa;
for (let k of Record.keys(a)){
console.log(a[k].toFixed()) // runtime error!
}
Of course, this example is artificial, but possible. Such covariant behavior of types in the typescript caused the rejection of the Object.keys
patch (as described in the readme), as the community perceived this as a potential vulnerability.
I think this can be bypassed by making Record having #private
which will make it not not crossasignable
this makes user able to use proper records, without caring about
o.constructor
ando.__proto__
breaking the expected behaviour, and usingRecord.keys()
on objects user believes to be records