Currently, I am in the process of migrating some older websites to TypeScript. However, I keep encountering a type error during the build process. The specific error message is
Type error: Argument of type 'number' is not assignable to parameter of type 'string'.
.
The problematic block of code looks like this:
useEffect(() => {
let iframWrap = document.querySelectorAll<HTMLElement>('.video-wrapper iframe');
iframWrap.forEach( (thing, key) => {
const thingWidth : number = parseFloat( thing.getAttribute('width') );
const thingHeight: number = parseFloat( thing.getAttribute('height') );
let thingRatio : number = 1;
if( typeof thingWidth === 'number' && typeof thingHeight === 'number'){
thingRatio = parseFloat(thingWidth/thingHeight);
}else{
thingRatio = 1;
}
thing.style.aspectRatio = thingRatio;
})
},
[]);
The issue arises when the division operation
thingRatio = parseFloat(thingWidth/thingHeight);
is performed.
I have made sure to assign number types to all variables within the function, so it's confusing why the error mentioning a string type is occurring.