为什么打字稿编译器会在生成的javascript中省略"should.js"导入? [英] Why is typescript compiler omitting 'should.js' import in generated javascript?

查看:43
本文介绍了为什么打字稿编译器会在生成的javascript中省略"should.js"导入?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我面临一个奇怪的问题.在我的(让我说)a.ts中,我-

I am facing a weird issue. In my (lets say) a.ts I have -

/// <reference path="../typings/mocha/mocha.d.ts" />
/// <reference path="../typings/should/should.d.ts" />

import should = require('should');

import something_else = require('../something-else');

现在,当我使用命令编译时-

Now when I compile using command -

tsc -m commonjs --outDir "./build" "src/test/a.ts"

我生成的javascript没有should-

My generated javascript is not having require for should -

/// <reference path="../typings/mocha/mocha.d.ts" />
/// <reference path="../typings/should/should.d.ts" />
var service_manager = require('../routes/service-manager');

这似乎是打字稿编译器中的错误,但我可能做错了.或者,如果有解决方法,请分享.

This seems like a bug in typescript compiler, but I may be doing it incorrectly. Or if there is some workaround, please share.

推荐答案

之所以这样做,是因为您没有使用它.实际使用should变量后,它将立即保留.例如

It does that because you are not using it. It will stick as soon as you you actually use the should variable. e.g.

/// <reference path="../typings/mocha/mocha.d.ts" />
/// <reference path="../typings/should/should.d.ts" />

import should = require('should');
var persist = should; 

原因:它允许您单独使用类型信息,而无需依赖require('should')的运行时.它还允许您在AMD方案中进行延迟加载.

Reason: It allows you to use type information on its own without taking a runtime dependency on require('should'). It also allows you to do lazy loading in AMD scenarios.

这篇关于为什么打字稿编译器会在生成的javascript中省略"should.js"导入?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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