Jexah
Jexah

Reputation: 183

How to use an exported function within the local module

This is the Javascript version of what I want to do in Typescript, with the important thing to note being the way in which I reference exports.inform from the exports.error function ("exports.inform"):

exports.error = function(_string, _kvp){
    for(var _i = 0; _i < server.clients.length; _++i){
        if(server.clients[_i] != undefined && server.clients[_i].isValid()){
            exports.inform(server.clients[_i], _string, _kvp);
        }
    }
}

exports.inform = function(_client, _string, _kvp){
    var _output = tag + processAll(_string, _kvp);
    _client.printToChat(_output);
}

This is my equivalent in Typescript, but "export function inform" is being referrenced incorrectly within function 'error' ("inform"):

export function error(_str:string, _kvp:Object) {
    for (var _i: number = 0; _i < server.clients.length; ++_i) {
        if (server.clients[_i] != undefined && server.clients[_i].isValid()) {
            inform(server.clients[_i], _str, _kvp);
        }
    }
}

export function inform(_client:Entity, _str:string, _kvp:Object) {
    var _output = tag + processAll(_str, _kvp);
    _client.printToChat(_output);
}

Sorry for the vague explanation, I hope you understand and will try to clarify if it's too hard to comprehend.

Edit: The error it was giving me was 'invalid call signature' caused by performing string concatenations within the argument, which TS apparently does not allow (unless in closed parenthesis), so Ryan's comment was correct: just call inform. Thank you Nypan.

Upvotes: 0

Views: 949

Answers (1)

Nypan
Nypan

Reputation: 7246

I am not entirely sure that i understand your problem or what you are trying to do for that matter. But if i am understanding you correctly the following should work:

module exports {

    export function error(message: string){

        inform(message);
    }

    export function inform(message: string) {

        alert(message);
    }   
}

exports.error("some message");

It might however be better to place the functions in a class like this:

class exports {

    public static error(message: string) {
        exports.inform(message);
    }

    public static inform(message: string) {
        alert(message);
    }   
}

exports.error("some message");

I might misunderstand what you are trying to do.. let me know.

Upvotes: 1

Related Questions