Reputation: 780
How can I reuse functions? I want to declare them once then include them in other files.
I created a module Global, containing some functions which I may want to add to other typescript files
I tried the following in another typescript file:
import test = require("./Global");
import * as testFunctions from "Global"
Both lines give errors saying the module cannot be found. The module is definitely visible to typescript as I am actually referencing this module at other places in the file, calling it's functions, which is working (EXAMPLE: Global.stopSpinner()).
Im not sure what I am doing wrong however, as I am following examples. Could someone explain me the correct way?
Upvotes: 22
Views: 68053
Reputation: 41
//This is how it should look in order to work
import * as testFunctions from "./Global";
I hope this helps!
Upvotes: 4
Reputation: 13148
One option is simply to compile your library into a .js output file (and .d.ts typescript definitions file), and then include the .js file in your projects. You don't need to use a module system then, though it can be tricky getting your .js files in the right place and properly referenced and published.
Upvotes: 0
Reputation: 23682
An example:
// global.ts
export function abc() {
}
// main.ts
import { abc } from "./global"
abc();
I suggest to read the introduction to ES6 modules from Mozilla.
Upvotes: 35