如何在 TypeScript 中导入 JavaScript 模块 | 珊瑚贝

How to import JavaScript modules in TypeScript


我有一些 JavaScript 代码正在尝试转换为 Typescript。

据说,typescript 是 JavaScript 的超集,除了以下有编译器错误。假设我没有将 ko 库导入typescript,我将如何转换以下代码:

1
2
3
4
5
(function(ko, viewModels){
    viewModels.MyViewModel = function(){
        //stuff in here
    }
}(ko, window.viewModels = window.viewModels || {}));

作为参考,这是我在 TypeScript 中的尝试

1
2
3
4
5
6
7
8
9
10
module viewModels {

    export class PartDetailsViewModel {
        public bar: string;
             constructor (){
                 this.bar = ko.foo(); //<– compiler error,”ko” does not exist in current scope
             }
        }
    }
}


查看 TypeScript\\’s “Ambient Declarations”,它允许您声明将在运行时提供的外部成员。因此,在您的示例中,添加以下内容将使编译器满意:

1
declare var ko;

顺便说一句,我还想在这篇文章中指导您:https://stackoverflow.com/a/12692174/806003

Sten 提供了一个基本的敲除接口,以便您可以在声明中指定一个类型以获取一些静态类型。在评论中也发现了这一点:https://gist.github.com/3833509

  • 更好地指出这个 KO 声明(因为它比你指向的要点更详细):github.com/sv01a/TypeScript-Knockoutjs
  • 更完整:github.com/borisyankov/DefinitelyTyped/blob/master/Definitio ns/


来源:https://www.codenong.com/12771258/

微信公众号
手机浏览(小程序)

Warning: get_headers(): SSL operation failed with code 1. OpenSSL Error messages: error:14090086:SSL routines:ssl3_get_server_certificate:certificate verify failed in /mydata/web/wwwshanhubei/web/wp-content/themes/shanhuke/single.php on line 57

Warning: get_headers(): Failed to enable crypto in /mydata/web/wwwshanhubei/web/wp-content/themes/shanhuke/single.php on line 57

Warning: get_headers(https://static.shanhubei.com/qrcode/qrcode_viewid_9340.jpg): failed to open stream: operation failed in /mydata/web/wwwshanhubei/web/wp-content/themes/shanhuke/single.php on line 57
0
分享到:
没有账号? 忘记密码?