Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the syntax for defining a type when parameter has a default value?

Tags:

flowtype

How do I define the type of the config parameter given that it has a default value?

function (config = {}) {};
like image 716
Gajus Avatar asked Oct 01 '15 17:10

Gajus


People also ask

What is the correct syntax for declaring a default parameter?

Default function parameters allow formal parameters to be initialized with default values if no value or undefined is passed. function foo(a, b) { a = typeof a !==

What type of parameter can be given a default value?

Default Parameter Data TypesAny primitive value or object can be used as a default parameter value.

What is the correct syntax for defining a parameter?

In the function definition f(x) = x*x the variable x is a parameter; in the function call f(2) the value 2 is the argument of the function. Loosely, a parameter is a type, and an argument is an instance. A parameter is an intrinsic property of the procedure, included in its definition.

Which is the correct syntax to define the default parameter value in Python?

Python has a different way of representing syntax and default values for function arguments. Default values indicate that the function argument will take that value if no argument value is passed during the function call. The default value is assigned by using the assignment(=) operator of the form keywordname=value.


1 Answers

function f(config: Object = {}) {}

Or, more generally:

function f(p: T = v) {}

where T is a type, and v is a value of type T.

Interestingly, the type of function f is (p?: T): void. That is, Flow understands that providing a default value makes the parameter optional. You don't need to explicitly make the parameter type optional—although it doesn't hurt.

When writing declare function statement in a .js.flow file, you can't include the default value; it will cause an error. So you must explicitly declare that the parameter is optional:

declare function f(p?: T): void;

like image 109
Sam Goldman Avatar answered Oct 18 '22 04:10

Sam Goldman