尽管找到了类型声明,但无法解析类型声明

时间:2017-07-22 12:24:53

标签: typescript

当我在项目上运行tsc时,会报告

src/app/home-behavior.ts(8,30): error TS7016: Could not find a declaration file for module 'jqueryui'. 'C:/myProjectPath/src/lib/jquery-ui/jquery-ui.js' implicitly has an 'any' type.
Try `npm install @types/jqueryui` if it exists or add a new declaration (.d.ts) file containing `declare module 'jqueryui';`

但我已安装@ types / jqueryui,tsc --traceResolution甚至显示

======== Resolving type reference directive 'jqueryui', containing file 'C:/myProjectPath/__inferred type names__.ts', root directory 'C:/myProjectPath/node_modules/@types'. ========
Resolving with primary search path 'C:/myProjectPath/node_modules/@types'.
Found 'package.json' at 'C:/myProjectPath/node_modules/@types/jqueryui/package.json'.
'package.json' does not have a 'typings' field.
'package.json' does not have a 'types' field.
File 'C:/myProjectPath/node_modules/@types/jqueryui/index.d.ts' exist - use it as a name resolution result.
Resolving real path for 'C:/myProjectPath/node_modules/@types/jqueryui/index.d.ts', result 'C:/myProjectPath/node_modules/@types/jqueryui/index.d.ts'.
======== Type reference directive 'jqueryui' was successfully resolved to 'C:/myProjectPath/node_modules/@types/jqueryui/index.d.ts', primary: true. ========

所以它解决了定义但仍无法找到定义?

打字稿版本2.4.1。使用带有requirejs的AMD风格加载。我的jquery-ui类型来自npm模块@types/jqueryui。我的目录结构是

projectroot
+ src
| + app
| | + home-behavior.ts
| + lib
|   + jquery
|   | + dist
|   |   + jquery.js
|   + jquery-ui
|     + jquery-ui.js
+ node_modules
| + @types
|   + jquery
|   | + index.d.ts
|   + jqueryui
|     + index.d.ts
+ tsconfig.json

我的tsconfig.json是

{
  "compilerOptions": {
    "alwaysStrict": true,
    "baseUrl": ".",
    "lib": [ "es2015", "dom" ],
    "module": "commonjs",
    "moduleResolution": "classic",
    "noEmit": true,
    "noImplicitAny": true,
    "paths": {
      "jqueryui": [
        "src/lib/jquery-ui/jquery-ui.js"
      ],
      "*": [
        "src/lib/*"
      ]
    },
    "sourceMap": false,
    "strictNullChecks": true,
    "target": "es5"
  },
  "include": [
    "src/app"
  ]
}  

home-behavior.ts中的引用是

import { Autocomplete } from "jqueryui";

(最初我把模块称为“jquery-ui / jquery-ui”,但由于tsc已经解析“jqueryui”,我改变了模块名称以匹配,理论上这将有所帮助。)

1 个答案:

答案 0 :(得分:2)

由于jQuery UI的输入不会导出任何成员,因此无法执行import { Autocomplete } from "jqueryui";。你可以这样做:

import  "jquery";
import "jqueryui";

$("selector").autocomplete(...);

如果您想像使用here那样使用它,则必须使用源代码中的jQuery UI库并更新TypeScript类型。