Flow: Convert Optional Type Properties to Required

Created on 18 Oct 2017  Â·  4Comments  Â·  Source: facebook/flow

I have a fairly common use case of typing server responses (specifically graphql responses) that contain many optional properties.

For example:

type Animal = {
  kind?: string,
  age?: number
}

Where kind and age may or may not be defined for the response. I'd like to use this same type definition for my React components, the difference being kind and age are required properties that I would cover with defaultProps. So my React component might look like this:

type Props = {
  kind: string,
  age: number
}

class Animal extends React.Component<Props> { 
  static defaultProps = {
    kind: 'dog',
    age: 2
  }
  ...
}

is my only option to rewrite the entire type definition of Animal, minus the ? syntax? Or is there a way to tell flow that I want to use the Animal type, but make all properties required properties?

Has PR destructors feature request

Most helpful comment

You can do an opposite thing

try

All 4 comments

You can do an opposite thing

try

$Shape works great! Why isn't it documented?

@mrkev Do you know why no one from PRs with docs for $Shape does not merged?

no idea, maybe it fell of the radar ¯\_(ツ)_/¯

Was this page helpful?
0 / 5 - 0 ratings