Hartesic
Hartesic

Reputation: 153

Exotic syntax in Javascript ": ?"

I was looking at a repo on Github when I stumbled on this file : https://github.com/cdebotton/react-universal/blob/master/app/entryPoints/client.js And more precisely the line 13:

let createDevToolsWindow: ?Function;

And I have no idea what this "[...]? :Function;" syntax is. I looked in the commits which introduced it, I searched in ES2015+, tried to find it on Google but I don't know how it's called so I didn't find anything.

Is this some Node thing? Or am I just really bad at JS?...

Upvotes: 1

Views: 101

Answers (1)

Andy Gaskell
Andy Gaskell

Reputation: 31761

This code is using flow and the prefixed ? means that null is allowed. More examples:

var array_of_num: number[] = [];
var array_of_num_alt: Array<number> = [];
var optional_array_of_num: ?number[] = null;
var array_of_optional_num: Array<?number> = [null, 0];

Another example:

// okay, 1 is a number
var good: number = 1;  

// okay, we have a ?
var good_nullable: ?number = null; 

// not okay, no ? means null is not a valid value for this variable.    
var bad: number = null;

Upvotes: 1

Related Questions