What is the difference between a question mark after a parameter and a colon in flow? What is the function of declare type? I have a little knowledge of reading English documents.

// vue
declare type ASTElementHandler = {
  value: string;
  params?: Array<any>;
  modifiers: ?ASTModifiers;// undefinednull
};

vue source


  1. params?: Array ; if the question mark comes first, the type can be array, void, but not null
  2. params:? Array ; after the question mark, the type can be an array, void, or null
  3. ASTModifiers is a user-defined structure type in the flow file, which is specified by the line according to the project requirements
The purpose of

4.declare type is because js itself is a weakly typed language, and it is inefficient to determine whether there is something wrong with the type or to do some operation at run time.
after the type is declared in advance, on the one hand, it is easier to cooperate and more standardized. On the other hand, syntax errors can be detected during the compilation phase. Save running time.


I would like to add one more point:
optional types (Maybe Types)
optional types are used where the value is optional, and the usage is to precede the type with a?, such as? string or? number. The optional type can be null or void.

// @flow
function acceptsMaybeString(value: ?string) {
  // ...
}

acceptsMaybeString("bar");     // Works!
acceptsMaybeString(undefined); // Works!
acceptsMaybeString(null);      // Works!
acceptsMaybeString();          // Works!

?voidnull:

// @flow
function acceptsObject(value: { foo?: string }) {
  // ...
}

acceptsObject({ foo: "bar" });     // Works!
acceptsObject({ foo: undefined }); // Works!
acceptsObject({ foo: null });      // Error!
acceptsObject({});                 // Works!

optional function parameters
the use of a function with optional parameters is to add a? after the parameter, which can be void or omitted, but cannot be null. For example,

// @flow
function acceptsOptionalString(value?: string) {
  // ...
}

acceptsOptionalString("bar");     // Works!
acceptsOptionalString(undefined); // Works!
acceptsOptionalString(null);      // Error!
acceptsOptionalString();          // Works!
Menu