The Many Faces of Statically Typed JavaScript

The Rationale Behind Statically Typed Languages

All programming languages have types, even dynamically typed ones like JavaScript. The difference lies in when these types are checked. In dynamically typed languages, type checking occurs at runtime, which can lead to unexpected errors. Statically typed languages, on the other hand, check types at compile time, providing a shorter feedback loop and more confidence in the code.

TypeScript vs. PureScript

TypeScript, created in 2012, focuses on adding type annotations to existing JavaScript. Its syntax is familiar to C-family language users, and it’s designed to work seamlessly with JavaScript.

function greet(name: string) {
  console.log(`Hello, ${name}!`);
}

TypeScript’s popularity has grown significantly, with many big-name projects like Angular and Vue.js adopting it.

PureScript, created in 2013, takes a different approach. As a purely functional language inspired by Haskell, it provides features like immutability, pattern matching, and type classes. Its syntax may seem strange at first, but it’s designed to improve code correctness and developer productivity.

module Main where

import Prelude

greet :: String -> String
greet name = "Hello, " <> name <> "!"

Dissecting the Output

When we compile TypeScript and PureScript code, we get JavaScript output. However, the two languages approach compilation differently. TypeScript’s output resembles the input code, while PureScript’s output is more abstract, with features like effectful computations and immutable data structures.

Expressing Types

In TypeScript, type information is intertwined with values and keywords. We can provide explicit types or let the compiler infer them.

let name: string = 'Alice';
// or
let name = 'Alice'; // type inferred as string

In PureScript, type information is placed above the identifier, and the language has a stronger focus on correctness.

greet :: String -> String
greet name = "Hello, " <> name <> "!"

A Language Made for Composition

PureScript’s design makes it an ideal language for composition. Its do notation allows for effectful computations to be composed together, and its type system enables us to encode what we want to do and how we want to do it. This leads to more predictable and maintainable code.

program :: Effect Unit
program = do
  putStrLn "Hello, world!"
  putStrLn "This is a composed program."

Choosing the Right Language

TypeScript and PureScript offer different approaches to statically typed JavaScript. While TypeScript is more focused on tooling and flexibility, PureScript prioritizes correctness and composition. Ultimately, the choice between these languages depends on your team’s needs and priorities. By understanding what each language provides, you can make an informed decision about which one is right for your project.

  • TypeScript: ideal for teams familiar with JavaScript and seeking flexibility and tooling support.
  • PureScript: ideal for teams prioritizing correctness, composition, and functional programming principles.

Leave a Reply