I'm trying to add type annotations to an existing code and I have an Array instance that doubles as an object with custom properties set on it.
Like So:
const foo = [];
foo[0] = 13;
foo.push(42);
foo.superLevel = 'extreme'; // Flow complains about this
I thought I might declare a new type called SuperArray, something along the lines of this:
type SuperArray = Array<number> & {
superLevel: string,
}
const foo: SuperArray = [];
// ...
But that doesn't work either. I've Goggled and browsed through the docs, and failed to find an answer to:
A) Is this at all supported? and
B) if so, how do I annotate it?
Also see: https://github.com/facebook/flow/issues/631
Not sure if there's any more recent info on that.
Yay! using $Supertype<> seems to work now:
https://flow.org/try/#0MYGwhgzhAED6DKBXADgUwE4EF3rAT2lQA8AXVAOwBMZtc8AecxAWwCMMA+aAbwChpoEFBgAyqAG6oQALkEl0AS3IBzANy8AvrxJ400JGiw580ALzQAJAYw609BMKN0OvXsAD25CCWgAzd+6y1k4m5gDaALrq-u5hAAwRZtAAjADM0QEAdMiIEAAWABQALABMAJTqvDGZQoZikiBJAOQxrGDoTeoA9F1+WbWiElJJyarQPdAAojju6ACEvEA
For the record, this works:
type SuperArray = (Array<number> & {
superLevel: string,
})
declare var foo: SuperArray
but this doesn't
type SuperArray = (Array<number> & {
superLevel: string,
})
var foo: SuperArray = []
The bug here is that [] should be assignable to SuperArray since it's a super type of Array.