flow
flow copied to clipboard
Support for Object.hasOwn()
Missing/Incorrect APIs
Are there plans to support Object.hasOwn()
?
Relevant documentation
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/hasOwn
Would be pretty awesome if this would also work as a refinement for optional properties
type A = { foo?: string }
declare var a: A
if (Object.hasOwn(a, 'foo')) {
a.foo.toUpperCase() // this is fine
}
I wasn't able to make this using string key arg in the second argument, but you can make a similar function that refines a bit if you change the API to accept object as the second arg and take keys from there, similarily to what could be used as a typesafe userland Pick
until the native version landed
if (hasOwnProperties(a, {foo: 0})) {
a.foo.toUpperCase()
}
Doesn't work with object union types, though.