flow icon indicating copy to clipboard operation
flow copied to clipboard

Support for Object.hasOwn()

Open andrewreedy opened this issue 2 years ago • 3 comments

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

andrewreedy avatar Mar 02 '22 23:03 andrewreedy

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
}

noppa avatar Mar 22 '22 17:03 noppa

Added in #9084

Although it's not refining in it's current state... try flow

pascalduez avatar Sep 30 '23 07:09 pascalduez

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()
}

Try flow.

Doesn't work with object union types, though.

noppa avatar Sep 30 '23 12:09 noppa