Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

React native: watchID: ?number = null what is this syntax?

I was reading react native doc and I came across this line of code

watchID: ?number = null;

I know this is kind of TypeScript syntax but I can't see TypeScript being used in React Native.

Can someone please explain about this?

like image 861
Praveena Avatar asked Feb 25 '26 05:02

Praveena


1 Answers

This is Facebook flow syntax. It's a way of introducing typing into regular Javascript. The source files can be statically parsed by flow to check for type problems. Then, when you compile your code with Babel, it takes these Flow annotations out so that it doesn't cause errors. You should see a .flowconfig file in your project's root directory—that's a dead giveaway your project is using flow.

This specific syntax is saying that if watchId is passed, it will be a number type, but the ? indicates that this is also a maybe type, meaning it may not be passed, in which case the = null will happen (this is not flow-related), which is how you assign a default value in JS.

like image 51
Rob Wise Avatar answered Feb 26 '26 22:02

Rob Wise



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!