Un expected an arithmetic operation must be a number throw
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?
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;
Thank you for your reply!
I know it, but I think default props will provide default values, so this.props.a is always number.
Currently, this doesn't throw errors:
@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.