0

I'm trying to get a simple node project with typescript and break code into two separate files, using a shared module. But having some problems!

1) FileReader.ts defines + exports a static function within a shared module

var fs = require("fs");

export module Qbot {

    export class Utils {
        static read(filename: string) {
            var buf = fs.readFileSync(filename, "utf8");
            return data;
        }

    }

    Qbot.Utils.read("test");  // compiles OK

}

2) FileReader-spec.ts a separate file to try and use the above.

/// <reference path="./FileReader.ts" />

// should be in same module but TS compiler gives an error
module Qbot {
    var u = Qbot.Utils;  // ERR does not exist
    var data = Qbot.Utils.read("somefile.json");  // ERR cant find name
}

// outside the module? nope.
var data = Qbot.Utils.read("somefile.json");  // Utils doesnt exist

// this works but why the extra file thingy needed?
import something = require("./FileReader");
var filepath = "./topics.json";
var data = something.Qbot.Utils.read(filepath);

Do I need to always have the extra file import to "contain" import modules before I can access them? Any pointers on how to achieve this would be appreciated!

This example is also just trying to call a static function on the Qbot.Utils module ( per this example https://stackoverflow.com/a/13212648/1146785 )

Thanks!

Community
  • 1
  • 1
dcsan
  • 11,333
  • 15
  • 77
  • 118

1 Answers1

0

Do I need to always have the extra file import to "contain" import modules before I can access them? Any pointers on how to achieve this would be appreciated!

Don't mix internal modules with external modules. Just use external modules. e.g. in your case FileReader.ts:

import fs = require("fs");

export class Utils {
    static read(filename: string) {
        var buf = fs.readFileSync(filename, "utf8");
        return data;
    }

}

Utils.read("test");  // compiles OK
basarat
  • 261,912
  • 58
  • 460
  • 511