重载不匹配
import React, { useEffect, useState, useRef } from 'react'
function Button() {
const [ctr, setCtr] = useState(0)
let interval = useRef<NodeJS.Timeout | null>(null)
useEffect(() => {
interval.current = setInterval(() => {
setCtr(prev => prev + 1)
},1000)
return () => {
clearInterval(interval.current)
}
}, [])
return (
<>
<span> Count : {ctr} </span>
<button onClick={ () => clearInterval(interval.current) }> Clear </button>
</>
)
}
export default Button
错误:
No overload matches this call.
Overload 1 of 2, '(intervalId: Timeout): void', gave the following error.
Argument of type 'Timeout | null' is not assignable to parameter of type 'Timeout'.
Type 'null' is not assignable to type 'Timeout'.
Overload 2 of 2, '(handle?: number | undefined): void', gave the following error.
Argument of type 'Timeout | null' is not assignable to parameter of type 'number | undefined'.
Type 'null' is not assignable to type 'number | undefined'.ts(2769)
图片: