2017-07-22 79 views
1

当我跑我的项目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';` 

但我已经有@安装类型/ 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. ======== 

所以它解决了定义但仍然找不到定义?


Typescript version 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" 
    ] 
} 

在家庭behavior.ts的引用

import { Autocomplete } from "jqueryui"; 

(本来我提到的模块“jquery- ui/jquery-ui“,但由于tsc已经解决了”jqueryui“,我改变了模块名称以匹配,理论上这将有所帮助。)

+0

jQueryUI的类型不会导出任何成员。所以你可以尝试只是'导入“jquery”;导入“jqueryui”;'并将它用作$(“selector”)。autocomplete(...)'。 – Saravana

+0

我会试试看。尽管如此,https://learn.jquery.com/jquery-ui/environments/amd/表示我应该可以执行require([“jquery-ui/autocomplete”],函数(自动完成){。我一直在努力尽可能的接近那些我可以用Typescript。 – Emdot

+0

是的,那是有效的。所以如果我想这样做AMD风格,我需要修改类型? – Emdot

回答

1

你不能做import { Autocomplete } from "jqueryui";,因为jQuery UI的类型不会导出任何成员。你可以这样做:

import "jquery"; 
import "jqueryui"; 

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

如果你想要像它提到​​使用它here你必须从源使用jQuery UI的库和更新的打字稿分型。