【发布时间】:2020-07-16 16:56:36
【问题描述】:
我尝试将 Knockout.js 与 TypeScript 一起使用。
我在本地安装了这些与 npm 相关的软件包(只有一个根 npm 项目),如 npm ls 所列:
@types/knockout@3.4.67knockout@3.5.1wpapi@1.2.1-
UNMET PEER DEPENDENCY typescript@>= 2.x(我不知道这是什么意思,但也许是相关的) @types/jquery@3.3.34@types/wpapi@1.1.0
wpapi types 包与实际的 wpapi 包配合得很好,但敲除似乎不能以这种方式工作。
有时我会在浏览器控制台中收到上述错误:
错误:无法从 '/.../root-project-directory/subdir1/subdir2/js' 中找到模块 'knockout'。
其他时候我把它作为tsc的输出:
[17:52:36] Starting compilation in watch mode...
../node_modules/@types/knockout/index.d.ts:1062:13 - error TS2403: Subsequent variable declarations must have the same type. Variable 'ko' must be of type 'typeof import("/.../root-project-directory/node_modules/knockout/build/types/knockout")', but here has type 'KnockoutStatic'.
1062 declare var ko: KnockoutStatic;
~~
../node_modules/knockout/build/types/knockout.d.ts:5:1
5 export as namespace ko;
~~~~~~
'ko' was also declared here.
[17:52:43] Found 1 error. Watching for file changes.
在main.ts 我有:
import ko = require("knockout");
import * as WPAPI from "wpapi";
或
import * as ko from "knockout";
import * as WPAPI from "wpapi";
在最后一种情况下,我收到以下错误:
[18:12:15] Starting compilation in watch mode...
wp-content/themes/custom-theme/assets/ts/main.ts:1:21 - error TS2307: Cannot find module 'knockout'.
1 import * as ko from "knockout";
~~~~~~~~~~
wp-content/themes/custom-theme/assets/ts/main.ts:39:19 - error TS2304: Cannot find name 'KnockoutObservable'.
39 language: KnockoutObservable<string>;
~~~~~~~~~~~~~~~~~~
wp-content/themes/custom-theme/assets/ts/main.ts:40:15 - error TS2304: Cannot find name 'KnockoutObservable'.
40 slug: KnockoutObservable<string>;
~~~~~~~~~~~~~~~~~~
wp-content/themes/custom-theme/assets/ts/main.ts:41:26 - error TS2304: Cannot find name 'KnockoutObservable'.
41 renderedContent: KnockoutObservable<string>;
~~~~~~~~~~~~~~~~~~
[18:12:21] Found 4 errors. Watching for file changes.
我包括使用<script> 标签的淘汰赛。我使用 VS 代码。
请帮我解决这个问题。
另一个相关的未决问题here。
谢谢。
更新 1
如果我使用npm uninstall @types/knockout 卸载@types/knockout,运行cd html; tsc 时会出现这些错误:
[13:48:34] Starting compilation in watch mode...
wp-content/themes/custom-theme/assets/ts/main.ts:40:19 - error TS2304: Cannot find name 'KnockoutObservable'.
40 language: KnockoutObservable<string>;
~~~~~~~~~~~~~~~~~~
wp-content/themes/custom-theme/assets/ts/main.ts:41:15 - error TS2304: Cannot find name 'KnockoutObservable'.
41 slug: KnockoutObservable<string>;
~~~~~~~~~~~~~~~~~~
wp-content/themes/custom-theme/assets/ts/main.ts:42:26 - error TS2304: Cannot find name 'KnockoutObservable'.
42 renderedContent: KnockoutObservable<string>;
~~~~~~~~~~~~~~~~~~
wp-content/themes/custom-theme/assets/ts/main.ts:50:29 - error TS2304: Cannot find name 'ko'.
50 this.language = ko.observable(language);
~~
wp-content/themes/custom-theme/assets/ts/main.ts:51:25 - error TS2304: Cannot find name 'ko'.
51 this.slug = ko.observable(slug);
~~
wp-content/themes/custom-theme/assets/ts/main.ts:52:36 - error TS2304: Cannot find name 'ko'.
52 this.renderedContent = ko.observable("");
~~
wp-content/themes/custom-theme/assets/ts/main.ts:97:9 - error TS2304: Cannot find name 'ko'.
97 ko.applyBindings(new PageViewModel(null, "home"));
~~
[13:48:40] Found 7 errors. Watching for file changes.
在卸载@types/knockout 之前一切正常,所以我不知道实际问题是什么。也许问题解决了,因为我从main.ts 的开头注释掉了import * as ko from "knockout";。所以我再次安装@types/knockout 包。
更新 2
This 是我的一个相关问题。
【问题讨论】:
标签: node.js typescript npm knockout.js tsc