Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set a default value for a Flow type?

I have defined a custom Flow type

export type MyType = {
  code: number,
  type: number = 1,
}

I want the type parameter to default as 1 if no value is present. However, Flow is complaining with Unexpected token =.

Flow error

Can this be done with Flow?

Currently using Flow v0.32.0.

like image 962
amb Avatar asked Sep 28 '16 09:09

amb


People also ask

What are default values in coding?

In computer technology, a default (noun, pronounced dee-FAWLT ) is a predesigned value or setting that is used by a computer program when a value or setting is not specified by the program user.


1 Answers

Function parameters can also have defaults. This is a feature of ECMAScript 2015.

function method(value: string = "default") { /* ... */ }

In addition to their set type, default parameters can also be void or omitted altogether. However, they cannot be null.

// @flow
function acceptsOptionalString(value: string = "foo") {
  // ...
}

acceptsOptionalString("bar");
acceptsOptionalString(undefined);
acceptsOptionalString(null);
acceptsOptionalString();

https://flow.org/en/docs/types/primitives/#toc-function-parameters-with-defaults

like image 91
Rohman HM Avatar answered Oct 05 '22 14:10

Rohman HM