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))
: [];
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))
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With