如何制作流动型细化棒?

时间:2018-03-20 19:35:42

标签: javascript flowtype

我怎样才能打开类似swift可选类型的类型,如下所示:

guard let certainVar = optionalVar else {
  return;
}

我问的原因是精炼常常会丢失,我想要一种方法让它们像我们在swift中那样坚持下去。据我所知,做到这一点有点尴尬,也许是这样的:

let certainVar;
if (maybeVar) {
  certainVar = maybeVar; // must save that certain value now before
                         // the refinement gets invalidated
} else {
  return;
}

这种方法的缺点:

  1. CertainVar和maybeVar不能具有相同的名称
  2. 非常详细,我想在很多地方这样做

1 个答案:

答案 0 :(得分:0)

您可以使用const

if (maybeVar) {
    const certainConst = maybeVar;
    ...

函数调用不会导致常量

的细化失效