Fable for busy moms and dads: use a JS lib in one minute!

François Nicaise
3 min readDec 22, 2017

--

Yesterday I wanted to try a new JS Lib with Fable: IS.js. This is the short story of what went well. (Yes. Nothing went wrong)

Updating ts2Fable

So the very first move was making sure ts2fable was installed and updated:

yarn global add ts2fable

ts2Fable is a “Fable parser for TypeScript declaration files.” It means that it will take a definition file (*.d.ts) and create a nice looking fsharp file.

Grab typescript definition file

Like most -if not all- JS libs out there-we can get the Typescript definition directly from the Definitely Typed repository. There’s a search form provided.

So here I just typed Is.js.

And went to the official npm page.

From here I went straight to the files on GitHub using the provided url.

Then I loaded the raw version of the index.d.ts file and saved it on my computer.

Create Fable.Import.Is.fs

Then I grabbed a terminal, cd to the directory where I stored my index.d.ts file and just typed:

ts2fable index.d.ts > Fable.Import.Is.fs

which then created a Fable.Import.Is.fs file.

Add the JS library using yarn

I just typed:

yarn add is_js

And voilà! The library was now included to my project.

Add the Fable wrappers to my project

I edited my .fsproj file to add my newly created wrapper.

Gluing everything

From this point. I had everything I needed. I just had to glue things in my project to be able to actually use the library.

And here’s a sample code:

let Is = importAll<Is> "../../node_modules/is_js"let name = "Fable"let isItOk = Is.empty name

So how much time did it take?

Well, let’s summarize. Let’s assume we do it gently on a cold winter morning, with a hot tea in one of our hand while listening to Christmas songs:

  1. Finding and downloading typescript definitions: 20 secs
  2. Creating the “wrapper” with ts2fable: 10 secs
  3. Getting the Is.js lib from npm: 10 secs
  4. Adding the Fable.Import.Is.fs file to our project: 10 secs
  5. Adding the import directive and testing: 10secs

That’s pretty much it: one minute.

Does it work nicely like this every time?

No. There are libraries that need much more work. But we do have some real nice tutorials here and there.

But for simple libs like Is.js which consists in static methods and a global object, the process is just plain crazy fast.

Last but not least…

Fable is plain awesome. There are now many blog posts and articles demonstrating its power. If you haven’t yet used, I think you should really give it a try.

You can get started fast. Right now. In a few seconds. No joking.

--

--