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「,我改變了模塊名稱以匹配,理論上這將有所幫助。)
jQueryUI的類型不會導出任何成員。所以你可以嘗試只是'導入「jquery」;導入「jqueryui」;'並將它用作$(「selector」)。autocomplete(...)'。 – Saravana
我會試試看。儘管如此,https://learn.jquery.com/jquery-ui/environments/amd/表示我應該可以執行require([「jquery-ui/autocomplete」],函數(自動完成){。我一直在努力盡可能的接近那些我可以用Typescript。 – Emdot
是的,那是有效的。所以如果我想這樣做AMD風格,我需要修改類型? – Emdot