score:2

Accepted answer

i don't think there is a general answer to this problem and i cannot give you a definite answer here. i have been working with flow now extensively for 1,5 years and also together with react for a year now. here are some tips for working with types.

1. use type inference

flow is really powerful when it comes to inferring types. together with the following tips you can avoid making a lot of annotations. flow will just figure out that things work. you can also make use of the typeof annotation to get the type of a value.

2. model your domain with types not the interfaces of the components

this was mentioned in the comments already. use types to model your domain. e.g. if you work with a component that displays a user, model the user object, not the interface of the component. you can then import the user type in the components that use it.

type user = { id: string, name: string };
type profileproptype = { user: user };

class profile extends component<profileproptype> {
  // ...
}

3. use state management

redux for example could help you with the types here. this will give you a structure for your domain types and also reduces how many props are passed around. another blessing is graphql and apollo client / relay.

4. generate types programmatically

we are generating types from our postgres schema via postloader in the backend and types for our graphql queries in the frontend. currently we are working on generating types for react-semantic-ui from their documentation. after that there are not many types left to write ourselves. there are probably some tools around for your use case.

one last thing:

types are verbose. sometimes it is good to be explicit. the idea is that types reduce the time you spend searching for bugs by forcing you to be explicit about things. often it is okay to redefine things. this will also lead to way better errors than using $propertytype because flow will give you two types that are incompatible instead of cryptic messages.


Related Query

More Query from same tag