Typescript在导入jquery后找不到名称'$'?
问题描述:
我用的打字稿1.4.1,并已经制定了像这样一个项目:
scripts/
libs/
jquery/
jquery.d.ts // Latest from DefinitelyTyped
jquery.js // 2.1.3
lodash/
lodash.d.ts // Latest from DefinitelyTyped
lodash.js // 3.3.1
main/
test.ts
我主/ test.ts包含以下内容:
/// <reference path="../libs/lodash/lodash.d.ts" />
/// <reference path="../libs/jquery/jquery.d.ts" />
import _ = require("lodash");
import $ = require("jquery");
function requireExistingElement($el: $.JQuery) {
if(_.isUndefined($el) || _.isNull($el) || $el.length === 0) {
throw new Error("It appears the requested element does not exist?");
}
}
requireExistingElement($("body"));
这是用下面的命令编译:
tsc --module amd scripts/main/test.ts
我希望这能正常运行,但是当我编译它,我得到:
scripts/main/test.ts(7,38): error TS2304: Cannot find name '$'.
scripts/main/test.ts(13,24): error TS2304: Cannot find name '$'.
我的问题是:我如何引用jQuery给定上述不工作?或者,我只是做错了什么?
答
变化($el: $.JQuery)
到($el: JQuery)
$
是一个变量,所以它不能在类型注释一起使用。 JQuery
是一个接口,所以它可以在类型注释中使用。
你说得对,那是问题所在。这看起来很不明显,但它确实有道理。谢谢! – Smartboy 2015-03-02 22:33:41