mehrandvd
mehrandvd

Reputation: 9116

How to use module loading in TypeScript

In TypeScript, I have a code like this:

levenstein/LevensteinAlgorithm.ts

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

To have a unit test for it, I have written a test in another path:

tests/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();

//... 

Unfortunately it has a build error saying:

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

I'm using QUnit and Chutzpah to run the tests.

What's the problem with my module loading?

Upvotes: 1

Views: 61

Answers (1)

Ryan Cavanaugh
Ryan Cavanaugh

Reputation: 221302

Because you wrapped LevensteinAlgorithm in the internal module levenstein in an external module, the way to reference that object once you've imported it as levenstein is levenstein.levenstein.LevensteinAlgorithm.

The best fix here is to just remove the export module Levenstein { from LevensteinAlgorithm.ts and export the class directly out of the external module.

See also the "Needless Namepsacing" heading here: https://typescript.codeplex.com/wikipage?title=Modules%20in%20TypeScript

Upvotes: 2

Related Questions