# Type-safe DOM+CSS library

**URL:** <https://users.scala-lang.org/t/type-safe-dom-css-library/12309>\
**Category:** Libraries\
**Created:** [July 13, 2026, 9:59am UTC](https://users.scala-lang.org/t/type-safe-dom-css-library/12309 "2026-07-13T09:59:43Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![yaitskov](https://users.scala-lang.org/user_avatar/users.scala-lang.org/yaitskov/32/3503_2.png) [@yaitskov](https://users.scala-lang.org/u/yaitskov)\
**Post date:** [July 13, 2026, 9:59am UTC](https://users.scala-lang.org/t/type-safe-dom-css-library/12309/1 "2026-07-13T09:59:43Z")

</div>

Hi,

I managed to express constraints of CSS selectors in types, so if CSS class applied to a tag that doesn’t have a proper context formed by ancestors and/or siblings of the tag, then a compilation error is produced.

E.g. for rule

```css
.a > .b { }

```

and the DOM snippet where prerequisites for class _b_ is not satisfied:

```scala
div_ </ div_ .= b 

```

```xml
<div>
  <div class="b">
  </div>
</div>

```

Error: parent tag doesn’t have _a_ class.

```auto
[([B, C "a"], [], [])]

```

Another case for siblings

```css
.a + .b { }

```

```scala
div_ </ div_ </ div_ .= b 

```

```auto
[([B], [], [[[B], [C "a"]]])]

```

As far as I understand, Scala has support for GADTs and can generate/execute code at compilation. So I am curious about the feasibility of replicating such library in Scala.

Tag type has just 9 parameters responsible for tracking CSS and DOM interplay

```auto
data E
     model
     action
     (en :: Symbol)
     (es :: ElementStructure)
     (re :: Maybe Root)
     (ei :: Maybe Symbol)
     (atrs :: [Symbol])
     (knownIds :: KnownIDS)
     (cls :: [Symbol])
     (l :: [[[Seg]]])
     (children :: [[SubSeg]])

```

---

<div class="post-metadata">

**Author:** ![Sporarum](https://users.scala-lang.org/user_avatar/users.scala-lang.org/sporarum/32/3012_2.png) [@Sporarum](https://users.scala-lang.org/u/Sporarum)\
**Post date:** [July 13, 2026, 10:57am UTC](https://users.scala-lang.org/t/type-safe-dom-css-library/12309/2 "2026-07-13T10:57:21Z")

</div>

That sounds really cool, and I would gladly help you, however I don’t understand anything ^^’

In particular:

> [@yaitskov](#):
>
> I managed to express constraints of CSS selectors in types

> [@yaitskov](#):
>
> So I am curious about the feasibility of replicating such library in Scala

So did you manage to do it in Scala’s type system, or a different language ?  
And if in Scala, then which library are you talking about ?

---

<div class="post-metadata">

**Author:** ![yaitskov](https://users.scala-lang.org/user_avatar/users.scala-lang.org/yaitskov/32/3503_2.png) [@yaitskov](https://users.scala-lang.org/u/yaitskov)\
**Post date:** [July 13, 2026, 12:52pm UTC](https://users.scala-lang.org/t/type-safe-dom-css-library/12309/3 "2026-07-13T12:52:33Z")

</div>

> [@Sporarum](#):
>
> So did you manage to do it in Scala’s type system, or a different language ?

The library is written in Haskell (GHC 9.12).  
Before investing a lot of time in the project, I tried to find out if anyone had created such library before and besides Haskell, I check Scala and Rust. I have experience with Java, but Scala is mostly a mystery for me. At first glance the language has all prerequisites to achieve the goal (GADTs and TemplateHaskell equivalent), but devil is in details…

---

<div class="post-metadata">

**Author:** ![Sporarum](https://users.scala-lang.org/user_avatar/users.scala-lang.org/sporarum/32/3012_2.png) [@Sporarum](https://users.scala-lang.org/u/Sporarum)\
**Post date:** [July 13, 2026, 6:29pm UTC](https://users.scala-lang.org/t/type-safe-dom-css-library/12309/4 "2026-07-13T18:29:39Z")

</div>

I see now, Scala probably has what you need given its type system is really powerful (+ [typed macros](https://docs.scala-lang.org/scala3/guides/macros/macros.html) + [givens](https://docs.scala-lang.org/scala3/reference/contextual/givens.html))

Given the above however, the “most Scala” implementation might look different from the haskel implementation.  
And as you might guess, there is a lot to learn !  
I hope this doesn’t disuade you tho, the language is very fun and powerful

---

<div class="post-metadata">

**Author:** ![christopherd](https://users.scala-lang.org/letter_avatar_proxy/v4/letter/c/dec6dc/32.png) [@christopherd](https://users.scala-lang.org/u/christopherd)\
**Post date:** [August 3, 2026, 2:34am UTC](https://users.scala-lang.org/t/type-safe-dom-css-library/12309/5 "2026-08-03T02:34:04Z")

</div>

Yeah, the original post’s a bit hard to parse. But looking at that type signature — tracking both ancestor context and sibling relationships plus all the CSS data simultaneously — that’s the real lift in any language. Even with GADTs you’re juggling a lot of interdependent constraints.

---

<div class="post-metadata">

**Author:** ![Jasper-M](https://users.scala-lang.org/user_avatar/users.scala-lang.org/jasper-m/32/872_2.png) [@Jasper-M](https://users.scala-lang.org/u/Jasper-M)\
**Post date:** [August 10, 2026, 8:42am UTC](https://users.scala-lang.org/t/type-safe-dom-css-library/12309/6 "2026-08-10T08:42:50Z")

</div>

Reminds me a bit of [parboiled2](https://github.com/sirthias/parboiled2). They generate a parser at compile time, and keep track of all the inputs and outputs of the parsing rules in heterogeneous lists in the types.
