DEV Community

Discussion on: TypeScript and JSX Part II - What can create JSX?

Collapse
 
trusktr profile image
Joe Pea • Edited

Is there some way to make a JSX expression like <div></div> have a type other than JSX.Element without casting it?

I'm wondering, because with Solid (one of the fastest view libs), which uses JSX for convenient syntax for creating DOM with reactive expressions, the type of div in

cons div = <div></div>

// it is a div element, we can use DOM APIs:
div.setAttribute('foo', 'bar')

in plain JavaScript is actually HTMLDivElement; div is a reference to an actual <div> element after the assignment.

I'm wondering if there's a way to make that typing possible, without having to write something like

const div = (<div></div>) as unknown as HTMLDivElement