TypeScript with Underscore, a simple Grunt way

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.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s