Are there any JavaScript libraries that can add type safety to Hyperapp?

495 views Asked by At

I'm considering using Hyperapp for my next project, but I've noticed that while it is Elm-like, it does not offer type safety guarantees (which is one of the best features about Elm). Is there a combination of JavaScript libraries (maybe used with Hyperapp) that can give you the type safety that Elm does?

3

There are 3 answers

0
JosephStevens On

At its core, building software is about using abstractions with different tradeoffs. Hyperapp made the tradeoff of familiar syntax at the cost of reliability, as Hyperapp is using JavaScript which is mutable by default. With mutable variables, you cannot make guarantees as it is by definition indeterministic. You can, however, test code to make it more deterministic. But, that is another tradeoff.

Long story short, if you want type safety, you can't rely on JavaScript alone. You can transpile to JavaScript like Purescript, Fable and Elm do. If you want more reliability, I'd recommend putting in the time to learn one of those languages.

Best of luck!

1
Wolfgang On

Hyperapp provides many of the basic tools you need for building apps in a functional style while staying in the JavaScript ecosystem and not requiring a build step to get started. This is a significantly lower barrier to entry than stricter solutions like Elm and Reason.

If you think of the scale from imperative to functional style like a dial, Hyperapp starts you off by turning that dial halfway up to functional. It's up to you as the user to decide how to spin that dial. If you're having to integrate a hostile API that requires sneaky DOM manipulations then you have escape hatches that take you out of the virtual DOM layer and access the lower level DOM. If instead, you are able to write your app using entirely pure functions thanks to tools inspired by Elm like effects as data then you get many of the same benefits for very little cost.

Most of all - I believe it's important to not conflate type safety with correctness. Rich Hickey from the world of Clojure has a chart showing the problems of programming that I personally agree with: The Problems of Programming Notice that static types are a solution to the least important problems in programming in this hierarchy. If you want to hear more details of Hickey's thoughts on types I encourage you to check out his Clojure/Conj 2017 keynote address.

4
mat3e On

I use Hyperapp with TypeScript and it works fine so far. With tsconfig options

"jsx": "react",
"jsxFactory": "h",

it is even possible to use JSX.

TypeScript enables ES6+ features and adds typing. It also transpiles to the readable JavaScript code.