flow icon indicating copy to clipboard operation
flow copied to clipboard

$Keys of a subclass errors incorrectly on superclass keys

Open kylehg opened this issue 9 years ago • 3 comments

Example

In this example, 'x' should satisfy the type $Keys<D> since x is a field on the superclass, but instead it throws an error.


/* @flow */

class C {
  x: number;
}

class D extends C {}

let d = new D()

let x: number = d.x // No error
let k: $Keys<D> = 'x' // Error

kylehg avatar Sep 20 '16 21:09 kylehg

Expected behavior, since $Keys are designed to model Object.keys

SamChou19815 avatar Jun 02 '23 16:06 SamChou19815

I'm not particularly attached to this issue anymore, but jfyi:

If you call Object.keys() on d in this example, you'd get ['x']:

Welcome to Node.js v18.16.0.
Type ".help" for more information.
> class C { constructor() { this.x = 1 } }
undefined
> class D extends C {}
undefined
> Object.keys(new C())
[ 'x' ]
> Object.keys(new D())
[ 'x' ]

So even modeling off Object.keys(), I'd think you'd expect $Keys<D> to be 'x'. (This works in the browser as well.)

kylehg avatar Jun 03 '23 22:06 kylehg

(Also fwiw this works in TypeScript: link)

kylehg avatar Jun 03 '23 22:06 kylehg