I guess we should always be marking our component props covariant, but should Flow really allow writing to component props under any circumstances?
In other words shouldn't StatelessFunctionalComponent<Props> essentially be equivalent to the following, and likewise for React.Component?
(props: ObjMap<Props, <T>(t: T) => $Supertype<T>>) => React.Node
// @flow
import * as React from 'react'
type UserCardImplProps = {
name: string,
address: string,
}
const UserCardImpl = (props: UserCardImplProps) => <div>{props.name = 'foo'}</div>
$Supertype is not the same as read-only. In fact, all writes are allowed to a $Supertype property.
You're right that React props should be immutable objects, although React does not enforce that internally. The best way we have currently for expressing that is through covariant property annotations, which is what you should use.
@samwgoldman is there any way to turn all properties of an object type covariant with any utility types? I can't seem to find a way...
I think $ReadOnly is what you're asking for?
This:
type Props = {
+name: string,
+age: number
}
is the same as
type Props = $ReadOnly<{
name: string,
age: number
}>
@saadq oh, thank you, I somehow hadn't discovered $ReadOnly!
@samwgoldman is there a reason you guys didn't decide to make StatelessFunctionalComponent<Props> essentially equivalent to (props: $ReadOnly<Props>) => React.Node?
Most helpful comment
@samwgoldman is there a reason you guys didn't decide to make
StatelessFunctionalComponent<Props>essentially equivalent to(props: $ReadOnly<Props>) => React.Node?