首页 > 解决方案 > 如何在没有这些代码重复错误的情况下将 Knockout.js 与 TypeScript 一起使用?

问题描述

我尝试将 Knockout.js 与 TypeScript 一起使用。

我在本地安装了这些与 npm 一起安装的相关软件包(只有一个根 npm 项目),如下所示npm ls

types 包与实际的wpapiwpapi 包配合得很好,但敲除似乎不能以这种方式工作。

有时我会在浏览器控制台中收到上述错误:

错误:无法从“/.../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

是我的一个相关问题。

标签: node.jstypescriptnpmknockout.jstsc

解决方案


Knockout@3.5.1 有它自己的类型,所以你不需要安装这个@types/knockout包。从 3.5.0 RC 开始,KnockoutJS 已经捆绑了它自己的类型——从 DefinitiveTyped安装类型会导致类型重复。

该解决方案将简单地卸载@types/knockout,即npm uninstall @types/knockout


推荐阅读