How to set default value for stream type?

I defined my own stream type

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

I want the default type parameter to be 1 if there is no value. However, Flow complains about Unexpected token = .

Stream error

Can this be done using a thread?

Currently using stream v0.32.0 .

+5
source share
3 answers

You cannot have default values ​​in a type declaration.

Possible idea

Use the class and initialize any default values ​​using property initializers: https://basarat.gitbooks.io/typescript/content/docs/classes.html#property-initializer

+2
source

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

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

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

The stream and the types that it allows you to determine are missing at javascript runtime. This is why type declarations do not support value initializers.

+3
source

Functional parameters may also have default values. This is a feature of ECMAScript 2015.

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

In addition to their dial type, the default parameters may also be invalid or omitted altogether. However, they cannot be empty.

 // @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

+2
source

Source: https://habr.com/ru/post/1257378/


All Articles