我正在使用TypeScript 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
我的main/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
是一个接口(interface),因此可以在类型注释中使用。
关于jquery - 导入jQuery后,Typescript无法找到名称 '$'?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/28815866/