Answer the question
In order to leave comments, you need to log in
How to properly type this piece of code?
const [showTooltip, setShowTooltip] = useState<boolean>(false); // показывает тултип если true
const timer = useRef<ReturnType<typeof setTimeout>>();
const handleMouseEnter = () => {
timer.current = setTimeout(() => {
setShowTooltip(true);
}, 500);
};
const handleMouseLeave = () => {
clearTimeout(timer.current as NodeJS.Timeout);
setShowTooltip(false);
};
Answer the question
In order to leave comments, you need to log in
If you are not using Node.js, then simply exclude the types for it from the TS config.
Then you setTimeout
will return a simple number
, but clearTimeout
will accept . And no problem.)
If for some reason the node code is mixed with the front code, then for front timers you can explicitly write and . number | undefined
window.setTimeout
window.clearTimeout
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question