I understand optional chaining should be enough but I have gone a bit overboard here to try satisfy TypeScript:
const ref = useRef()
if (ref !== undefined) {
if(ref.hasOwnProperty('current')) {
if (ref.current !== undefined && ref.current !== null)
console.log(ref?.current?.getBoundingClientRect())
}
}
Error:
TS2339: Property 'getBoundingClientRect' does not exist on type 'never'.
I miss my non-typing days... Any solution other than @ts-ignore
TS2339: Property 'name' does not exist on type 'never'. Have no idea how to solve this. Seems that typescript doesn't understand at all that variable could be initialized from callback function.
When compiled/transpiled through TypeScript it generates error TS2339: Property 'myclassvar' does not exist on type 'MyClass'. If the above code snippet is valid ES6 then TypeScript should not generate the error. The generated javascript it valid. It's just that the error scares the developers trying to use ES6 without typings.
Getting error TS2339: Property does not exist on type for a valid ES6 class #6373. Getting error TS2339: Property does not exist on type for a valid ES6 class. #6373.
Here is a small example: TS2454: Variable 'user' is used before being assigned. TS2339: Property 'name' does not exist on type 'never'. Have no idea how to solve this. Seems that typescript doesn't understand at all that variable could be initialized from callback function.
You just have to provide an element type to useRef
const Test = () => {
const ref = useRef<HTMLInputElement>(null);
const rect = ref?.current?.getBoundingClientRect();
}
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