How to use Bluebird with current TypeScript?

I just do not understand. When it was relatively easy, I downloaded the fragment from DefinitelyTyped, it suggested that it was declared globally, I added a script, and it worked. Now it seems like I have no other choice to use a sophisticated package manager and asynchronous boot system, possibly with an optimizer for production.

Ideally, I just need TypeScript code like this

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

compile some JavaScript like this:

 Promise.resolve("foo").then(function(msg) { console.log(msg); } 

I want it to work in a browser or in a minimal environment like Rhino. If necessary, I can enable require.js, almond.js, build using a browser or something else, but I want it to work without the need for XHR.

So far I have tried:

  • Using global (surrounding) modules: it was no longer possible to find one for Bluebird.
  • Using ES6 import with AMD modules, setting outFile to get a package of my code, and then including the files in that order: almond.js, bluebird.js, my-code.js and some code requiring the main module so that everything works: Error: See almond README: incorrect module build, no module name (apparently, bluebird.js , downloaded from the official page, does not determine the name in AMD, should I create it myself or what ...?)
  • Same as previous point using full require.js: missing bluebird .
  • The same as at the previous point using a pad as follows: requirejs.config({shim: { bluebird: { exports: "Promise" }}}) : exception from uncaught JavaScript throw: Error: Mismatched anonymous define() module and all Bluebird code in the error message.
  • Bluebird definition file fix: Not allowed in ES6 mode: Subsequent variable declarations must have the same type.

It seems to me that I spent too much time on such an obviously simple request, so I will try to ask for StackOverflow :)

+3
source share
2 answers

Instead of using global declaration files from DT, it is now better to use typings by importing module declaration files.

Eg. for a blue bird you can just typings install bluebird . This code is great for me:

 import Promise = require('bluebird') Promise.resolve("foo").then(function (msg) { console.log(msg) }) 
+3
source

If, like me, you think that using Promise.resolve() to bring / enforce the promise of Bluebird, it looks like he is both defeating the stated goal of Bluebird ("Zero Abstraction Abstraction", "Works Everywhere", "Program Compatibility ", ...) and brings TypeScript to the bulky polling point, consider using @types/bluebird-global as follows:

 npm install --save-dev @types/bluebird-global 

Import it once into your main entry point.

 // The same Promise API, everywhere. import * as Promise from 'bluebird' global.Promise = Promise 

See Definitely a problem with type # 11027 for further context.

+2
source

Source: https://habr.com/ru/post/1261981/


All Articles