ntrrgc ntrrgc - 1 year ago 438
TypeScript Question

How to use Bluebird with current TypeScript?

I just don't get it. Once this was relatively easy, I downloaded the snippet from DefinitelyTyped, it assumed it was globally declared, I added the script and it worked. Now it seems that I have no other option that to use a complex package manager and asynchronous loading system, possibly with an optimizer for production.

Ideally I just want some TypeScript code like this

// maybe some import?
Promise.resolve("foo").then(function(msg) {

compile to some JavaScript like this:

Promise.resolve("foo").then(function(msg) {

I want it to be able to run in a browser or in a minimal environment like Rhino. If needed, I can include require.js, almond.js, build with browserify or whatever, but I want it to work without needing XHR.

So far I've tried:

  • Using global (ambient) modules: could not find one for Bluebird anymore.

  • Using ES6 imports with AMD modules, setting
    to get a bundle of my code and then including files in this order: almond.js, bluebird.js, my-code.js and some code that requires the main module so anything runs:
    Error: See almond README: incorrect module build, no module name
    (apparently the
    I downloaded from the official page does not define a name in AMD, should I build it myself or what...?)

  • The same as in the previous point using full require.js:
    missing bluebird

  • The same as in the previous point with a shim like this:
    requirejs.config({shim: { bluebird: { exports: "Promise" }}})
    exception from uncaught JavaScript throw: Error: Mismatched anonymous define() module
    and the entire code of Bluebird in the error message.

  • Patching the Bluebird definition file: Not allowed in ES6 mode:
    Subsequent variable declarations must have the same type.

I feel like I've wasted too much time for such an apparently simple request, so I'll try asking StackOverflow :)

Answer Source

Instead of using the global declaration files from DT, it's now considered best practice to use typings instead, importing module declaration files. For a general overview, have a look at the SO documentation page.

Eg. for bluebird, you can just typings install bluebird. This code compiles fine for me:

import Promise = require('bluebird')
Promise.resolve("foo").then(function (msg) {
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download