Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

flow type question mark?

So confused on using '?' in flow. AFAIK (thanks to flow type question mark before or after param?):

  1. When '?' before ':', means bar is optional, can be string or undefined: bar?: string

  2. When '?' after ':', means bar is maybe type, can be string, undefined, or null. bar: ?string

My question is: In which circumstance we should the first option over the second? How about bar?: ?string ?

flow is hard...

like image 264
nicemayi Avatar asked Jul 05 '18 01:07

nicemayi


People also ask

What is flow data type?

Supported data types in an Apex class are Boolean, Integer, Long, Decimal, Double, Date, DateTime, and String. Each data type supports single values and Lists.

What is flow typed?

In programming language theory, flow-sensitive typing (also called flow typing or occurrence typing) is a type system where the type of an expression depends on its position in the control flow. In statically typed languages, a type of an expression is determined by the types of the sub-expressions that compose it.

Is flow deprecated?

Microsoft Dataverse (legacy) connector (CDS 2.0 connector) for Power Automate flows will be deprecated and replaced with another connector. Effective October 2022, the Microsoft Dataverse (legacy) connector, also referred to as the CDS 2.0 connector, will be deprecated.

What is flow syntax?

To bring static typing to JavaScript, Flow specifies a number of syntax extensions, which are used to describe types, annotate programs, and share types between modules. Flow's syntax extensions are only additions which can be easily stripped away and don't change the runtime behavior of JavaScript in any way.


1 Answers

Optional means that the property can be omitted. Have a look at this example:

type Foo = {
  optional?: string
}

const foo: Foo = {}; // OK

type Bar = {
  maybe: ?string;
}

const bar: Bar = {}; // Error: `maybe` is missing in object literal

Regarding combination of optional and maybe - it allows assigning null to optional property:

type Baz = {
  maybeAndOptional?: ?string;
}

let baz: Baz = {}; // OK
baz = { maybeAndOptional: null }; // OK

type Foo = {
  optional?: string
}

let foo: Foo = {}; // OK
foo = { optional: null } // Error: null is incompatible with string
like image 59
Aleksey L. Avatar answered Sep 20 '22 13:09

Aleksey L.