你如何在打字稿中使用jquery ui和commonjs [英] How do you use jquery ui with commonjs in typescript
问题描述
对于更多上下文,我决定将所有模块保持为commonjs格式以便于移植,并使用browserify和其他一些东西来捆绑前端的东西。
For more context I've decided to keep all my modules in commonjs format for portability and use browserify with a few other things to bundle up stuff on the frontend.
我使用TypeScript进行类型检查,但我不确定如何在我的配置中使用jquery ui。请参阅下面的简单示例。
I use TypeScript to do type checking but I'm not sure how to use jquery ui with my configuration. See below for a simple example.
package.json
{
"main": "Main.js",
"license": "MIT",
"dependencies": {
"@types/jquery": "^2.0.34",
"@types/jqueryui": "^1.11.31"
}
}
tsconfig.json
{
"compilerOptions": {
"module": "commonjs",
"baseUrl": ".",
"allowSyntheticDefaultImports": true,
"outDir": "build/js",
"target": "es5",
"sourceMap": true,
"noImplicitAny": true,
"typeRoots": ["./node_modules/@types/*"]
}
}
Main.ts
'use strict';
import $ from 'jquery'
export class Main {
constructor(public app: string) {
$(() => {
$('#date').datepicker();
})
}
}
我从typescript编译器得到的错误是错误:(7,24)TS2339:类型'JQuery'上不存在属性'datepicker'。
但是datepicker在 node_modules/@types/jqueryui/index.d.ts中定义:1091
。
The error I get from the typescript compiler is Error:(7, 24) TS2339: Property 'datepicker' does not exist on type 'JQuery'.
yet datepicker is defined in node_modules/@types/jqueryui/index.d.ts:1091
.
所以我的问题是我如何使用在打字稿中工作的commonjs来获取jquery ui。我试图避免使用三次斜杠指令(例如 ///< reverence path =.../>
So my question is how do I get jquery ui with commonjs working in typescript. I'm trying to keep from using triple slash directives (e.g. ///<reverence path="..." />
推荐答案
试试这个:
import $ from "jquery";
declare var global: any
global.jQuery = $;
import "jqueryui";
这篇关于你如何在打字稿中使用jquery ui和commonjs的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!