flow type question mark before or after param?

前端 未结 2 648
傲寒
傲寒 2020-12-15 04:15

Can someone explain the difference between:

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

and:

function foo(bar?: stri         


        
相关标签:
2条回答
  • 2020-12-15 04:26

    ?string (maybe type) means that bar property can be string aswell as null and void.

    bar? means that this property is optional.

    More info: https://flow.org/en/docs/types/primitives/

    0 讨论(0)
  • 2020-12-15 04:41

    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.

    0 讨论(0)
提交回复
热议问题