1

I've got a variable whose initial value I want to be null, but I want to be able to set it as a number as well.

const [intervalId, setIntervalId] = useState(null);

When I write setIntervalId(2); in my code later, it complains that argument type number is not assignable to SetActionState<null>.

What's the proper way to define such a variable in React?

1 Answer 1

3

Pretty simple:

const [intervalId, setIntervalId] = useState<null | number>(null);
Sign up to request clarification or add additional context in comments.

1 Comment

I figured this out literally moments after i asked the question, thank you very much. I'll mark as correct when the timer lets me.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.