node.js - 如何在没有这些代码重复错误的情况下将 Knockout.js 与 TypeScript 一起使用?
问题描述
我尝试将 Knockout.js 与 TypeScript 一起使用。
我在本地安装了这些与 npm 一起安装的相关软件包(只有一个根 npm 项目),如下所示npm ls
:
@types/knockout@3.4.67
knockout@3.5.1
wpapi@1.2.1
UNMET PEER DEPENDENCY typescript@>= 2.x
(我不知道这意味着什么,但也许它是相关的)@types/jquery@3.3.34
@types/wpapi@1.1.0
types 包与实际的wpapi
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 代码。
请帮我解决这个问题。
另一个相关的未决问题在这里。
谢谢你。
更新 1
@types/knockout
如果我使用卸载npm uninstall @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
一切正常,所以我不知道实际问题是什么。也许问题已经解决了,因为我import * as ko from "knockout";
从main.ts
. 所以我@types/knockout
再次安装包。
更新 2
这是我的一个相关问题。
解决方案
Knockout@3.5.1 有它自己的类型,所以你不需要安装这个@types/knockout
包。从 3.5.0 RC 开始,KnockoutJS 已经捆绑了它自己的类型——从 DefinitiveTyped安装类型会导致类型重复。
该解决方案将简单地卸载@types/knockout
,即npm uninstall @types/knockout
。
推荐阅读
- javascript - 如何根据搜索关键字对字符串数组进行排序?
- ruby-on-rails - Rails ActiveStorage:链接到云端
- coq - 在 Coq 中正确地进行双重归纳
- memory - 在打开的项目窗口之间共享 Intellij IDEA 的 ls -Xmx 内存限制?
- selenium - selenium.common.exceptions.WebDriverException:消息:进程意外关闭,状态为 1
- c++ - 在 C++ 中构造字符串
- java - 如何从向量中正确添加和删除对象
- php - 使用 Codeigniter 从 Thunderbird 导入所有联系人详细信息?
- r - Rstudio,用坐标填充自治市镇
- java - Java 应用程序调用的所有第三方类的列表