flow icon indicating copy to clipboard operation
flow copied to clipboard

Un expected an arithmetic operation must be a number throw

Open jyane opened this issue 9 years ago • 4 comments

I have found a strange behavior of flow.

Here is an example of valid that using plus operator. Re-produce

Here is an example of invalid that using minus operator. Re-produce

I think type of a is number. Why I have got an error?

jyane avatar Dec 16 '16 09:12 jyane

I believe, in case of + operator it acts as a concatenation operator. Other operators like multiplication and - can not work on undefined.

Adding a null check or if it's not a maybe type, you don't see the error.

// This will not show an error
    if(this.props.a != null){
    	const aa: number = this.props.a - 1;
    }

// Neither this will show an error
type Props = {
  a: number;
};
// ...
const aa: number = this.props.a - 1;

kvkirthy avatar Dec 16 '16 12:12 kvkirthy

Thank you for your reply!

I know it, but I think default props will provide default values, so this.props.a is always number.

jyane avatar Dec 16 '16 12:12 jyane

Currently, this doesn't throw errors:

alfondotnet avatar Oct 02 '17 23:10 alfondotnet

@alfonsoperez no, this dose throw error: if we change const aa: number = this.props.a + 1; to const aa: number = 2 * this.props.a;

error: const aa: number = 2 * this.props.a; ^ undefined. The operand of an arithmetic operation must be a number.

stevesuyao avatar Dec 13 '17 20:12 stevesuyao