如何在当前的TypeScript中使用Bluebird? [英] How to use Bluebird with current TypeScript?

查看:212
本文介绍了如何在当前的TypeScript中使用Bluebird?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我就是不明白.一旦这相对容易,我就从DefinitelyTyped下载了该代码段,假设它是全局声明的,我添加了脚本并且它起作用了.现在看来,我别无选择,只能使用复杂的程序包管理器和异步加载系统,并可能使用用于生产的优化程序.

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.

理想情况下,我只想要这样的TypeScript代码

Ideally I just want some TypeScript code like this

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

像这样编译成一些JavaScript:

compile to some JavaScript like this:

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

我希望它能够在浏览器中或像Rhino这样的最小环境中运行.如果需要,我可以包括require.js,almond.js,使用browserify或其他工具构建,但是我希望它不需要XHR就可以工作.

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:

  • 使用全局(环境)模块:无法再为Bluebird找到一个模块.
  • 使用ES6导入和AMD模块,将outFile设置为获取我的代码束,然后按以下顺序包含文件:almond.js,bluebird.js,my-code.js和一些需要主模块的代码这样就可以运行任何内容:Error: See almond README: incorrect module build, no module name(显然,我从官方页面下载的bluebird.js在AMD中未定义名称,我应该自己创建还是用它...?)
  • 与上一点相同,使用完整的require.js:missing bluebird.
  • 与上一点相同,但带有如下填充片:requirejs.config({shim: { bluebird: { exports: "Promise" }}}):exception from uncaught JavaScript throw: Error: Mismatched anonymous define() module以及错误消息中Bluebird的整个代码.
  • 修补Bluebird定义文件:在ES6模式下不允许:Subsequent variable declarations must have the same type.
  • Using global (ambient) modules: could not find one for Bluebird anymore.
  • Using ES6 imports with AMD modules, setting outFile 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 bluebird.js 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.

我觉得我在这样一个看似简单的请求上浪费了太多时间,所以我将尝试询问StackOverflow:)

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

推荐答案

代替使用DT的全局声明文件,现在认为使用键入,而是导入模块声明文件.

Instead of using the global declaration files from DT, it's now considered best practice to use typings instead, importing module declaration files.

例如.对于bluebird,您可以按typings install bluebird键.这段代码对我来说很好:

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) {
  console.log(msg)
})

这篇关于如何在当前的TypeScript中使用Bluebird?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆