what `circle: (null : ?{ setNativeProps(props: Object): void })` means

Viewed 164

I am learning react native. I am trying to understand the code Here. The snippet below seems very weird to me. Can you please explain what it does?

circle: (null : ?{ setNativeProps(props: Object): void })

I know about if statement like:

blabla = (if-this-is-true) ? this-should-be-used : otherwise_this

but don't know if the circle one is the same thing.

2 Answers

It Flow type annotation. Flow is a static type checker for JavaScript (https://flow.org/).

?Type syntax means that type of variable is Maybe type, so it can be undefined, null or accept provided "Type". For example ?string would mean string, null, or undefined. More about his type in flow here.

The (null: Type) syntax is a type cast expression (link). Using type cast expressions you can assert that values are certain types or cast value to certain type.

In your question (null : ?{ setNativeProps(props: Object): void }) null will be casted to Maybe type that accepts:

  • object with method setNativeProps, taking object as argument and returning nothing (void type),
  • null,
  • undefined

(x: y) is how type casting is performed in flow. I.e. the value x is cast to type y.

In your case null is refined as

?{ setNativeProps(props: Object): void }

which is a nullable object that has method setNativeProps. This method accepts an object and reruns nothing (void).

Learn more about flow at https://flow.org/.

Related