I'm so envious of this. In TypeScript I use ts-pattern and Effect Schema, and while they make this logic way nicer, it's insanely verbose and doesn't offer any of the niceties of being first class.
I have not used it at all, but Gleam does have a javascript target in it's compiler/build-tool. So in theory, you can write Gleam (strongly typed, etc) and produce js.
I've exclusively used the BEAM/Erlang target so far - but the js community within Gleam seems quite interesting.
I've been considering trying this, but my team already struggles to properly adopt TypeScript so I'm fairly sure introducing Gleam would cause a few people to throw me out a window.
Gleam is so much smaller and easier than typescript and the type system works harder for what it is. TS gets you because it is similar to javascript in some ways that make it easier to start the transition. But a complete js -> ts transition is about as big a deal as it would be from js to any other language, except you can use the same external libraries.