Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

FlowType: null check fails

Why does the null-check fail in func1 while it is ok in func2

/* @flow */

const func1 = (arr?: Array<*>) => {
  const isArrayNotEmpty = arr && arr.length;

  if (isArrayNotEmpty) {
    arr.forEach((element) => console.log(element));
  }
}

const func2 = (arr?: Array<*>) => {
  if (arr && arr.length) {
    arr.forEach((element) => console.log(element));
  }
}

Live example

like image 886
wizardzloy Avatar asked Mar 10 '17 13:03

wizardzloy


People also ask

Why can't I create a flow with a null field?

In Microsoft Power Automate, when you create a flow, here's what could happen with null field that cause trouble: You may be expecting wrong behavior when doing an action with null field. If you aren't handling null fields correctly, you may be expecting runtime errors like: InvalidTemplate. Unable to process template language expressions.

How to set up a condition check for null field?

You can set up a condition check for null field. Here are the steps to create a condition check for null. Add a new condition action. Choose dynamic content output (for example, user email) you want to check. Set the operation to be (for example) is not equal to. Put the value field as the expression value null.

What is NULL check operator used on null value in flutter?

In this example, we are going to show you how to show you the cause and solution of the "Null check operator used on a null value" error in the Flutter App. This error occurs when you add (!) Null check operator on Null value. For example: The operator (!) is used on null value "str". To solve this issue, see the different examples below:

How to handle null values in flutter or dart?

Here, "str" is null, and we set the fallback operator with fallback value in case of "str" is null. You need to do this before using it on the code. You can use this method to handle Null values to escape the "Null check operator used on a null value" error in Flutter or Dart.


1 Answers

I don't know the reasons why Flow doesn't support this, but it doesn't. It currently requires that checks for type refinements actually happen in the if statement, and does not track them if they are abstracted out into a separate variable.

There is an alternative that may be acceptable to you (I'm not sure it's documented anywhere):

/* @flow */

const func1 = (arr?: Array<*>) => {
  if (isArrayNotEmpty(arr)) {
    arr.forEach((element) => console.log(element));
  }
}

function isArrayNotEmpty(x: mixed): %checks {
  return x && x.length;
}

(tryflow)

The special %checks return type indicates to Flow that it should look into the body of the function to figure out what it implies about the types of the variables it is passed. I believe there are some restrictions on what can be in the body of such a function. It may even be the case that it just has to return a single expression. This should give you enough to experiment with it, though.

like image 67
Nat Mote Avatar answered Oct 09 '22 07:10

Nat Mote