Skip to content
Generate TypeScript declaration files for your elm ports!
Branch: master
Clone or download
Latest commit 8a28597 Apr 9, 2019
Type Name Latest commit message Commit time
Failed to load latest commit information.
bin Rename project to elm-typescript-interop. Aug 18, 2017
src Update to latest version of elm-typescript-interop. Sep 17, 2018
tests-approval Use `any` type for `Json.Decode.Value`s. Sep 17, 2018
.gitignore Clean up e2e test and its tsconfig setup. Aug 5, 2017
.npmignore Setup build pipeline for npm commandline executable. Jul 26, 2017
.travis.yml Fix failing test. Sep 17, 2018
InvalidIpc.elm Test output for invalid syntax in elm input file. Aug 5, 2017
LICENSE Update readme. Sep 15, 2018
UnsupportedParameterIpc.elm Capture expected error output for unsupported constructor parameter. Aug 5, 2017
elm-package.json Remove elm-cli-options-parser to workaround Elm 0.18 package bug. Sep 17, 2018
webpack.config.ts Use a d.ts for interop.ts script. Sep 17, 2018

Elm TypeScript Interop

Build Status npm version

Watch this short video tutorial to see elm-typescript-interop in action! Video tutorial

Use type-safe ports between Elm and TypeScript for end-to-end type-safety!

We love the safety and guarantees that Elm gives us. But we accept that we must give those up in our native javascript code for our Elm applications and the seams between the two languages. elm-typescript-interop gives the same guarantees of no runtime exceptions for sending and receiving data between Elm and TypeScript. If your code compiles, you have the guarantee that the data you're sending will be what Elm is expecting. No more unexpected data runtime errors from Elm!


Just run the elm-typescript-interop command-line interface in an Elm project directory and it will generate TypeScript type declaration files (.d.ts) in the appropriate places to give you type-safety and autocompletion for your Elm ports!

  • npm install --save-dev elm-typescript-interop.
  • Add an entry in your package.json scripts section to run elm-typescript-interop before each build.

Alternatively, you can run npx elm-typescript-interop.

See for reference or to setup a brand new project.

See for a real-world example of this library in action.

Elm Ports Vs. Elm-TypeScript-Interop Ports

The Elm Guide describes all the supported types for javascript interop. Note that this is improved with elm-typescript-interop:

Booleans and Strings – both exist in Elm and JS!
Numbers – Elm ints and floats correspond to JS numbers
Lists – correspond to JS arrays
Arrays – correspond to JS arrays
Tuples – *correspond to TypeScript tuples in elm-typescript-interop
Records – correspond to JavaScript objects *(with type-safety)
Maybes – Nothing and Just 42 correspond to null and 42 in JS
         *In elm-typescript-interop, the type will be (number | null)
Json – Json.Encode.Value corresponds to arbitrary JSON

Elm <-> TypeScript Type Conversions

To learn more about the types in TypeScript, see

Json.Encode.Value <-> any Avoid this escape hatch when possible. If your TypeScript value may take on multiple types, use this type paired with a decoder.

Boolean <-> boolean

String <-> string

Maybe Boolean <-> boolean | null

Int or Float <-> number

List String or Array String <-> string[] (or other types besides String, of course)

{ username: String, id: Int } <-> { username: string; id: number}

(Int, String) <-> [Int, String]

With regular Elm JS interop, the recommended approach is to use decoders so you can more gracefully handle unexpected types rather than ending execution with a runtime exception. With elm-typescript-interop, it is actually safer to use raw types because they are guaranteed to match up. With the caveat that you lose this guarantee if you are passing in something with an any type, in this case it is better to make this uncertainty explicit by declaring the type as a Json.Decode.Value. This is no longer the case when you use elm-typescript-interop.

The recommended approach with elm-typescript-interop is the opposite: avoid using Json.Encode.Value for ports and flags. Using Elm types here allows us to have guaranteed type-safety between Elm and TypeScript, so there's no need to use a decoder to safely handle unexpected types. With elm-typescript-interop, it is recommended that you use Json.Encode.Value only if for some reason a TypeScript type cannot be known at compile-time.

Both are excellent resources, and this was true with the limitations of javascript. TypeScript provides type-safety and more expressive types that allow us to interoperate better with Elm.

You can’t perform that action at this time.