npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

elm-typescript-interop

v0.0.17

Published

Code generator for type-safe elm-typescript interprocess messages.

Downloads

856

Readme

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!

Usage

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 github.com/dillonkearns/elm-typescript-starter for reference or to setup a brand new project.

See github.com/dillonkearns/mobster 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 https://basarat.gitbooks.io/typescript/docs/types/type-system.html.

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.