An Expression of Type 'Void' Cannot Be Tested for Truthiness
I Don't Want to Push Null, So I'm Putting a Condition to Check, but There's a Syntax Error That Says: "An Expression of Type 'Void' Cannot Be Tested for...
I don't want to push null, so I'm putting a condition to check, but there's a syntax error that says:
"An expression of type 'void' cannot be tested for truthiness"
How do I do this correctly?
localStorage.setItem('todoitems', JSON.stringify(this.todoitems))
? localStorage.setItem('todoitems', JSON.stringify(this.todoitems))
: [];
1 Answer
Since setItem doesn't return anything, it is complaining about using a void type in a ternary operator where a boolean is expected
You can add an if condition on this.todoitems before setItem
if (this.todoitems !== null)
localStorage.setItem('todoitems', JSON.stringify(this.todoitems))