-
Notifications
You must be signed in to change notification settings - Fork 1
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Consider adding safe Record type #2
Comments
As far as I understand, you want to match the type 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 |
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 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 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 |
I think this can be bypassed by making Record having |
I've tried. But it didn't impact to cross assign ability behavior |
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 recordsThe text was updated successfully, but these errors were encountered: