Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

flow type question mark before or after param?

Can someone explain the difference between:

function foo(bar: ?string) {   console.log(bar); } 

and:

function foo(bar?: string) {   console.log(bar); } 

When to use one over the other?

like image 874
Vic Avatar asked Nov 15 '17 18:11

Vic


1 Answers

Basically

bar: ?string 

accepts a string, null or void:

foo("test"); foo(null); foo() 

While

bar?: string 

Accepts only a string or void:

foo("test"); foo(); 

As passing null instead of a string is somewhat senseless, theres no real life difference between them.

like image 198
Jonas Wilms Avatar answered Sep 22 '22 17:09

Jonas Wilms