3

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?

Jorge Bucaran
  • 5,588
  • 2
  • 30
  • 48
user2167582
  • 5,986
  • 13
  • 64
  • 121
  • 2
    I think the type system is a large part of Elm, without it you lose many of the things that Elm can promise. That being said, in my limited experience, Flow is similar to Elm's type system. Flow and Hyperapp could give a development feel similar to Elm. – Sidney Mar 01 '18 at 23:23

3 Answers3

4

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!

Jorge Bucaran
  • 5,588
  • 2
  • 30
  • 48
JosephStevens
  • 1,668
  • 1
  • 15
  • 17
3

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.

Wolfgang
  • 155
  • 9
  • 1
    I think that strong types can be used to solve rather more problems than just the bottom two - especially in the context of something like Elm. One might reasonably argue that a type system like that in Elm or F#, used appropriately can help you with domain complexity and misconception too... – Murph Mar 02 '18 at 15:32
1

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.

mat3e
  • 781
  • 5
  • 17
  • i guess my question is, if i apply typescript everywhere, would i get the benefit to type safety guarantee? – user2167582 Mar 31 '18 at 17:24
  • TypeScript gives you type safety wherever it is "plugged". If one file in your codebase is written in pure JS, it won't be safe. Same when variables in TypeScript files are with type `any`. But thanks to those trade-offs, you can usually introduce TS into your project without bigger problems. – mat3e Apr 01 '18 at 22:42
  • 1
    that is awesome. personally i feel like elm kind of loses its value if typescript has all that. – user2167582 Apr 02 '18 at 02:46
  • Yes, seems TypeScript is the most popular transpiler at the moment (https://stateofjs.com/2017/flavors/results/). – mat3e Apr 03 '18 at 16:03