簡體   English   中英

如何在TypeScript中使用模塊加載

[英]How to use module loading in TypeScript

在TypeScript中,我有這樣的代碼:

萊文施泰因/ LevensteinAlgorithm.ts

export module levenstein {
    export class LevensteinAlgorithm
    {
        getDistance(left: string, right: string): number {
        // Some code for the alogorithm...
        }
    }
}

為了對此進行單元測試,我在另一條路徑中編寫了一個測試:

測試/ levensteinAlgorithmTests.ts

/// <reference path="../scripts/typings/qunit/qunit.d.ts" />
/// <reference path="../levenstein/levensteinalgorithm.ts" />

QUnit.module("levensteinalgorithm.ts tests");

import levenstein = require("levenstein/LevensteinAlgorithm");

test("Simple update cost is equal to 1", ()=> {
// Arrange
var leven = new levenstein.LevensteinAlgorithm();

//... 

不幸的是,它有一個構建錯誤,說:

Property LevensteinAlgorithm doesn't exist on type: ....

我正在使用QUnitChutzpah來運行測試。

我的模塊加載有什么問題?

因為您將LevensteinAlgorithm包裝在內部模塊levenstein中的外部模塊中,所以將對象導入為levenstein引用該對象的方式是levenstein.levenstein.LevensteinAlgorithm

最好的解決方法是從LevensteinAlgorithm.ts刪除export module Levenstein {並將類直接導出到外部模塊中。

另請參見此處的“無需命名”標題: https ://typescript.codeplex.com/wikipage?title = Modules%20in%20TypeScript

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM