Closed Phinome closed 8 hours ago
no body care?
In typescript you can always force different type if you are sure it is different type or check it
let ref;
createResizeObserver(() => ref, ({ width, height }, el) => {
// if you are sure of the type
(el as HTMLElement).offsetWidth;
// checking if it contains prop you want
if (offsetWidth in el) el.offsetWidth;
});
<div ref={ref}/>
Generics will simplify this for you, but note that typescript also generalize types whenever it can, so if we had generics and
let ref: HTMLElement;
let ref2: Element;
createResizeObserver(() => [ref, ref2], ({ width, height }, el) => {
// `el` would be generalized here to type `Element`, because `HTMLElement` extends `Element`
// still would have to check it manually
});
<div ref={ref}/>
Describe The Problem To Be Solved
in case:
el
isHTMLElement
, but handler of parameterel
toElement
.i want to get
offsetWidth
oroffsetHeight
fromel
, buttypescript
would checked failed: "The Element type has no offsetWidth or offsetHeight attribute."Suggest A Solution
just like this: