Archive for the ‘FrontEnd Coding’ Category

TypeScript with Underscore, a simple Grunt way

May 16, 2016

I use Underscore an awful lot. I have many good things to say about Underscore in the context of Functional Programming, but this post explains a simple one-liner which got my Underscore code quickly into our ever-increasing collection of TypeScript modules.

This is not a long term, fully robust solution incorporated into our Grunt build process, but it keeps us moving. A longer-term solution will use Typings, I imagine.

Here it is: did you know that you can have .d.ts files mentioned in the src slot of the grunt-typescript task?

So: we downloaded the underscore.d.ts file from the DefinitelyTyped repository, saved it into a separate ‘typings‘ directory and modified our grunt task to mention it as a source:

    typescript: {
      base: {
        src: ['app/typings/*.d.ts', 'app/scripts/**/*.ts'],
        dest: 'app/scripts/.compiled_typescript',
        options: {
            target: 'es5',
            declaration: true

In this way we keep our external TypeScript definitions in the separate directory, away from our ‘real code’, and now have the benefit of proper type definitions for our usage of Underscore.