flow
flow copied to clipboard
$Keys of a subclass errors incorrectly on superclass keys
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
Expected behavior, since $Keys are designed to model Object.keys
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.)
(Also fwiw this works in TypeScript: link)