Introduction
PureScript is a purely functional programming language with a powerful type system that compiles to clean, readable JavaScript. Inspired by Haskell, it brings algebraic data types, type classes, higher-kinded types, and row polymorphism to the JavaScript ecosystem while producing output that integrates naturally with existing JS libraries.
What PureScript Does
- Compiles to human-readable JavaScript or alternative backends (Erlang, Go, C++)
- Provides a sound type system with full type inference and no runtime type errors
- Supports algebraic data types, pattern matching, and type classes for expressive abstractions
- Offers row polymorphism for flexible, type-safe records and effects
- Interoperates with existing JavaScript code through a foreign function interface
Architecture Overview
The PureScript compiler is written in Haskell and translates source files into CommonJS or ES modules. It performs full type checking and inference before code generation, catching entire classes of errors at compile time. The Spago package manager handles dependency resolution from the PureScript package registry. The effect system tracks side effects in types, making pure and impure code explicitly separated.
Self-Hosting & Configuration
- Install via npm:
npm install -g purescript spagofor compiler and package manager - Use
spago initto scaffold a project withspago.yamlfor dependency configuration - Configure the output directory and backend in
spago.yamlunder thepackagesection - Set up IDE support with the PureScript language server for VS Code or other editors
- Use
spago bundle-appto produce a single JavaScript file for browser deployment
Key Features
- Sound type system guarantees no runtime type errors in PureScript code
- Row polymorphism enables extensible records and variant types without boilerplate
- Effect system (via the Eff or Aff monad) makes side effects explicit and composable
- Clean JavaScript output that is readable and debuggable without source maps
- Comprehensive standard library with Halogen and React bindings for web UIs
Comparison with Similar Tools
- Haskell — PureScript shares syntax and concepts but targets JavaScript instead of native code, with a simpler build story for web projects
- Elm — Simpler type system focused on web UIs; PureScript offers higher-kinded types and more flexibility at the cost of a steeper learning curve
- TypeScript — Gradual typing over JavaScript; PureScript provides full type safety with no escape hatches
- ReScript — ML-family language for JS with faster compilation; PureScript offers a richer type system with type classes
- ClojureScript — Dynamic functional language for JS; PureScript catches errors at compile time through static typing
FAQ
Q: Can PureScript use existing npm packages? A: Yes. PureScript provides a Foreign Function Interface (FFI) that lets you wrap any JavaScript module with PureScript type signatures for safe interop.
Q: What is Spago? A: Spago is the official PureScript package manager and build tool. It manages dependencies from the PureScript registry, handles builds, and can bundle output for browsers.
Q: Is PureScript suitable for production use? A: Yes. Companies use PureScript in production for web applications, internal tools, and backend services. The type system catches bugs at compile time, reducing runtime failures.
Q: How does PureScript handle side effects? A: All side effects are tracked in the type system using monads like Effect (synchronous) and Aff (asynchronous). Pure functions cannot perform side effects, enforced by the compiler.