FunScript is an F# compiler library enabling developers to write single-page web applications in F# and running them in the browser or on the server via Node.js.
FunScript has two components: a compiler library used to convert F# code into JavaScript and a TypeScript type provider used to expose the APIs of various JavaScript libraries and the DOM as static types.
A sample “Hello, World” example written in FunScript looks like this:
[<FunScript.JS>]
module Page
open FunScript
open FunScript.TypeScript
type lib = Api<"lib.d.ts">
let hello () = lib.window.alert("Hello, World!")
do Runtime.Run(components=Interop.Components.all)
Each module is saved in a “.fs” file and it must be annotated with the FunScript.JS attribute. The page’s DOM is accessed via the type lib
. The last line is used to start the launcher. The module is stored on a simple web server by default.
FunScript modules can be created in Visual Studio, MonoDevelop or a simple text editor. The library gets powerful because it allows the developer to interrogate the types found in various JavaScript libraries, having access to autocomplete and tooltip features. The types are provided via TypeScript, and, currently, the FunScript GitHub project contains type definitions for HTML DOM, jQuery, jQueryUI, Node.js, Google Maps, and HighCharts, which are interactive JavaScript charts. A much larger collection of JS type definitions can be found here.
Another advantage of using FunScript for web applications is the ability to use F#’s generics and asynchronous programming capabilities. Also, the developer can work with JSON data and REST APIs using F# 3.0 type providers. All of that while maintaining F#’s conciseness.
FunScript’s website contains a number of samples, including Charting World Bank data with F# and Searching The Movie Database with F#. More samples can be found on project’s GitHub page.
FunScript is an open source project licensed under Apache License 2.0