0
votes

I am trying to create a set of static "helper" functions that I want to reuse in multiple scripts across my application. However, I run into a problem compiling because the helper functions' arguments need to be defined as types from an external module.

I have an external definition file, external.d.ts:

declare module "external" {
    export class ExternalClass { }
}

I create a set of static helper functions that I want to resuse in multiple scripts across my application, in app.helpers.ts:

/// <reference path="external.d.ts" />

import * as external from "external";

module App {
    export module Helpers {
        export function helperFunction(thing: external.ExternalClass) {
            //do stuff
        }
    }
}

Notice the function argument has a type parameter of the class from the external module definition. Now I want to use this helper function in my app.ts:

/// <reference path="external.d.ts" />
/// <reference path="app.helpers.ts" />

import * as external from "external";

var thing: external.ExternalClass;
//instantiate thing
App.Helpers.helperFunction(thing);

However, this does not compile. On the last line, I get the error Cannot find name 'App'. The error goes away if I comment out the import statement in app.helpers.ts, but then I get an error in that file Cannot find namespace 'external'., which is obvious because I can't reference the external module definition any more (and thus still can't compile).

I want to have a set of helper functions that I can reference via an internal module, but those functions have type parameters of types in external modules. My question is: Is there a way to make this work?

1

1 Answers

0
votes

I create a set of static helper functions that I want to resuse in multiple scripts across my application, in app.helpers.ts

It is good practice to explicitly import these helpers where you need them.

import * as helpers from "../path/to/app.helpers.ts";

Also don't use module keyword as with external modules each file is already a module. More on modules : https://basarat.gitbooks.io/typescript/content/docs/project/modules.html