I am facing an issue with selecting a simple SVG <circle>
element in my DOM using TypeScript:
<svg viewBox="0 0 200 200">
<circle cx="50" cy="50" r="45" id="myCircle"/>
</svg>
Initial approach:
My initial attempt to select the circle was:
var circle: SVGCircleElement = document.getElementById("myCircle");
However, I encountered the error:
Type 'HTMLElement' is missing the following properties: cx, cy, r.
I then tried type-assertion:
Second approach:
var circle: SVGCircleElement = (<SVGCircleElement>document.getElementById("myCircle"));
But this resulted in the error:
Conversion of type HTMLElement to SVGCircleElement may be a mistake because...
This method seems to work for other elements like <canvas>
but not for SVGs:
https://i.sstatic.net/g6VxD.jpg
Query:
What is the recommended way in TypeScript to select myCircle
from the DOM without relying on type coercion? (I am using Typescript 3.3)