There seems to be a flow issue indicating that string [1] is not an object
in the code snippet below:
type User = {
name: string,
age: number,
gender?: string,
}
const user: User = {
name: 'xxx',
age: 23,
...(props.gender && { gender: props.gender }) // <----- the problematic line
}
Any idea why this error occurs?
It appears that conditionally setting a key is not properly supported with the object rest spread operator.
To address this, I ended up using:
const user: User = {
name: 'xxx',
age: 23,
}
if (props.gender) {
user.gender = props.gender
}
However, I prefer not to sacrifice a language feature because of issues with flow typing.