The following code declares a Flowtype Union of string in two ways. 1) Using the built-in Union Type, with the caveat of having to type all the codes again, creating a duplication. 2) Taking advantage of $Keys: Flow v0.38.0 correctly infers makeObjectWithKeys types, but I would like to know if it is possible to manually write such annotations.
const codesArray = [
{
name: 'Lorem',
code: 'lm'
},
{
name: 'Ipsum',
code: 'ip'
},
// ...
]
// Define the CodeType "manually" with the Union built-in
type CodeTypeManual =
| "lm"
| "ip"
// ...
const noErrorManual: CodeTypeManual = 'lm'
const flowErrorPropertyNotFoundManual: CodeTypeManual = 'zz'
// Define the CodeType by taking advantage of $Keys
const makeObjectWithKeys = (inArray) => { // Type annotations?
return inArray.reduce(
(objAcc, curObj) => { // Type annotations?
const retObj = { ...objAcc }
const { code } = curObj
retObj[code] = code
return retObj
}
, {}
)
}
const objectWithCodesAsKeys = makeObjectWithKeys(codesArray)
type CodeType = $Keys<typeof objectWithCodesAsKeys>
let noError: CodeType = 'ip'
let flowErrorPropertyNotFound: CodeType = 'zz'
If I understand your question correclty, this following might help.
/* @flow */
const codesArray = [
{
name: 'Lorem',
code: 'lm'
},
{
name: 'Ipsum',
code: 'ip'
},
// ...
]
const makeObjectWithKeys = function<T: Object>(inArray: Array<T>, key: $Keys<T>) {
return inArray.reduce((objAcc: { [name: typeof key]: T }, curObj: T) => {
return Object.assign({}, objAcc, { [curObj[key]]: curObj })
}, {})
}
const objectWithCodesAsKeys = makeObjectWithKeys(codesArray, 'code')
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With