Flow: Flow should never consider React Component props readable

Created on 21 Mar 2018  路  5Comments  路  Source: facebook/flow

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>

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?

All 5 comments

$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?

Was this page helpful?
0 / 5 - 0 ratings

Related issues

l2silver picture l2silver  路  3Comments

jamiebuilds picture jamiebuilds  路  3Comments

ctrlplusb picture ctrlplusb  路  3Comments

cubika picture cubika  路  3Comments

funtaps picture funtaps  路  3Comments