【问题标题】:Can't resolve type declarations despite having found the type declarations尽管找到了类型声明,但仍无法解析类型声明
【发布时间】:2017-07-22 12:24:52
【问题描述】:

当我在我的项目上运行 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”,因此我更改了模块名称以匹配,理论上这会有所帮助。)

【问题讨论】:

  • jQueryUI 的类型不导出任何成员。因此,您可以尝试只使用import "jquery"; import "jqueryui"; 并将其用作$("selector").autocomplete(...)
  • 我试试看。不过,作为参考,learn.jquery.com/jquery-ui/environments/amd 说我应该能够做到require([ "jquery-ui/autocomplete" ], function( autocomplete ) { ... }。我一直在尝试使用 Typescript 尽可能接近这一点。
  • 是的,这行得通。所以如果我想做这种 AMD 风格,我需要修改类型?
  • 是的,您必须更新类型。 AFAIK 要像这样使用它,您必须使用来自 GitHub 的源代码,而不是通过 npm 分发的版本。
  • 谢谢。把它写成答案,以便我接受?

标签: typescript


【解决方案1】:

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

import  "jquery";
import "jqueryui";

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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-11-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多