首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何在不出现这些代码重复错误的情况下使用Knockout.js与TypeScript?

如何在不出现这些代码重复错误的情况下使用Knockout.js与TypeScript?
EN

Stack Overflow用户
提问于 2020-04-04 15:15:52
回答 1查看 1K关注 0票数 1

我尝试将Knockout.js与TypeScript结合使用。

我在本地安装了这些相关软件包(只有一个根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

wpapi类型包与实际的wpapi包很好地工作,但是剔除似乎不是这样的。

有时,在浏览器控制台中会出现以下错误:

错误:无法从'/.../root-project-directory/subdir1/subdir2/js'.中找到模块“敲除”

其他时候,我把它作为tsc的输出

代码语言:javascript
复制
[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中,我有:

代码语言:javascript
复制
import ko = require("knockout");
import * as WPAPI from "wpapi";

代码语言:javascript
复制
import * as ko from "knockout";
import * as WPAPI from "wpapi";

在最后一个例子中,我得到了以下错误:

代码语言:javascript
复制
[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时会出现以下错误

代码语言:javascript
复制
[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

是我的一个相关问题。

EN

回答 1

Stack Overflow用户

发布于 2020-04-04 15:29:53

剔除@3.5.1有它自己的类型,所以没有必要安装@types/knockout包。从3.5.0RC开始,KnockoutJS已经绑定了自己的类型-installing来自DefinitelyTyped的类型将导致输入复制。

解决方案只需卸载@types/knockout,即npm uninstall @types/knockout

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/61030233

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档