我正在 next.js 应用程序中制作一个简单的动画。
let flipInterval = useRef();
const startAnimation = () => {
flipInterval.current = setInterval(() => {
setIsFlipping((prevFlipping) => !prevFlipping);
}, 10000);
};
Run Code Online (Sandbox Code Playgroud)
因为flipInterval.current我收到“类型‘超时’不能分配给类型‘未定义’”。所以我检查了如何使用超时类型,我看到人们正在使用,但那不起作用。
let flipInterval = useRef<typeof window.settimeout>();
Run Code Online (Sandbox Code Playgroud)
我也通过了号码useRef<number>()我收到“类型‘超时’不能分配给类型‘数字’”
这也不起作用
let flipInterval = useRef<typeof window.setInterval>();
Run Code Online (Sandbox Code Playgroud) 我知道可选链接应该足够了,但我在这里尝试满足 TypeScript 有点过火:
const ref = useRef()
if (ref !== undefined) {
if(ref.hasOwnProperty('current')) {
if (ref.current !== undefined && ref.current !== null)
console.log(ref?.current?.getBoundingClientRect())
}
}
Run Code Online (Sandbox Code Playgroud)
错误:
TS2339: Property 'getBoundingClientRect' does not exist on type 'never'.
Run Code Online (Sandbox Code Playgroud)
我想念我不打字的日子......除了 @ts-ignore