我已经定义了一个自定义的流量类型如何设置Flow类型的默认值?
export type MyType = {
code: number,
type: number = 1,
}
我想type
参数默认为1
,如果没有价值存在。但是,Flow正在抱怨Unexpected token =
。
可以这样用流量呢?
当前使用流程v0.32.0
。
我已经定义了一个自定义的流量类型如何设置Flow类型的默认值?
export type MyType = {
code: number,
type: number = 1,
}
我想type
参数默认为1
,如果没有价值存在。但是,Flow正在抱怨Unexpected token =
。
可以这样用流量呢?
当前使用流程v0.32.0
。
在类型声明中不能有默认值。
使用类和使用性能initialisers初始化任何违约:https://basarat.gitbooks.io/typescript/content/docs/classes.html#property-initializer
围棋的想法@basarat提出,并使用一类。一个类同时存在一个类型和一个值。
该值可以初始化。流量识别提出属性初始化语法,因此使用人流量(类型)和巴贝尔(拟议功能的支持),您可以声明你的类像this:
// @flow
export class MyType {
code: number;
type: number = 1;
};
流,它可以让你定义的类型,不目前在JavaScript运行时。那就是为什么类型声明不支持值初始值设定项。
函数参数也可以有默认值。这是2015年的ECMAScript
function method(value: string = "default") { /* ... */ }
的特征除了它们的组类型,默认参数也可以是无效或完全省略。但是,它们不能为空。
// @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
这是不可能的。此外,混合和匹配类型和值是没有意义的。 – gcanti
但是,当注释一个函数时,也就是'function(code:number,type:number = 1){}'就可以做到这一点。 – amb
'= 1'部分处于数值级别 – gcanti