How do you use jquery ui with commonjs in typescri

2020-04-20 12:53发布

问题:

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.

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();
        })

    }
}

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.

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="..." />

回答1:

Try this:

import $ from "jquery";
declare var global: any
global.jQuery = $;
import "jqueryui";