How to set a default value for a Flow type?

Go with the idea @basarat proposes, and use a class. A class exists as both a type and a value.

The value can be initialized. Flow recognizes a proposed property initializer syntax, so using Flow (for types) and babel (for proposed feature support) you can declare your class like this:

// @flow
export class MyType {
  code: number;
  type: number = 1; 
};

Flow, and the types it lets you define, are not present in the javascript runtime. Thats why type declarations don't support value initializers.


You cannot have defaults in a type declaration.

Possible idea

Use a class and initialise any defaults using property initialisers : https://basarat.gitbooks.io/typescript/content/docs/classes.html#property-initializer


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