0

I'm trying to use the trace function in my TypeScript code, which has been declared in a .d.ts file as follows:

declare function trace(arg: string | number | boolean);
declare function trace(arg: { id: number; name: string });

However, when I try to use the trace function in my code in the file index.ts as follows:

trace("trace with string"); // calls the trace function with a string argument
trace(true); // calls the trace function with a boolean argument
trace(1); // calls the trace function with a number argument
trace({ id: 1, name: "test" }); // calls the trace function with an object argument

I get the following error:

ReferenceError: trace is not defined

Can anyone help and tell me what I am doing wrong? The declaration option is enabled in tsconfig.json too.

Thank you in advance.

EDIT:

Here is the JS code in a JS file where the trace function is being declared

var ErrorHelper = (function () {
    return {
        containsErrors: function (response) {
            if (!response || !response.responseText)
                return false;

            var errorValue = response.responseText;

            if (String(errorValue.failure) === "true"
                || Boolean(errorValue.failure)) {
                return true;
            }
            return false;
        },
        trace: function (msg) {
            var traceMessage = msg;
            if (msg.responseText) {
                traceMessage = msg.responseText.errorMessage;
            }
            console.log("[" + new Date().toLocaleTimeString()
                + "] " + traceMessage);
        }
    }
})();
Usama Ahmed
  • 21
  • 1
  • 4
  • Where do you define the actual function implementation? – ghybs Mar 16 '23 at 05:59
  • Did you import the required Class which trace function has been declared.? It would be better if you can provide stack trace of the error. – Tharaka Ananda Mar 16 '23 at 06:02
  • I am learning this from a book but stuck here. The trace function was defined in a JS file earlier. I have added the additional JS code in the edits above. Here is the GitHub [https://github.com/PacktPublishing/Mastering-TypeScript-Fourth-Edition/tree/main/ch07 for the book materials. – Usama Ahmed Mar 16 '23 at 06:08
  • @UsamaAhmed The thing is you are trying to access "trace" function which was defined inside "ErrorHelper" without referencing "ErrorHelper". Please try like this "ErrorHelper.trace("trace with string");". You can see sample example in this link. https://github.com/PacktPublishing/Mastering-TypeScript-Fourth-Edition/blob/d4855c98c9b66f95b05482e168555fa23cc73dd9/ch07/jsFiles/ErrorHelperSample.js – Tharaka Ananda Mar 16 '23 at 06:29
  • @TharakaAnanda thank you, this worked! However, what would I need to do to make it work like it is stated here in this file -> https://github.com/PacktPublishing/Mastering-TypeScript-Fourth-Edition/blob/main/ch07/declaration_file_samples.ts – Usama Ahmed Mar 16 '23 at 06:32

1 Answers1

0

To make your function visible to other classes, you should also export it in the d.ts file:

You can find the information here: https://www.typescriptlang.org/docs/handbook/declaration-files/templates/module-d-ts.html

declare function trace(arg: string | number | boolean);
declare function trace(arg: { id: number; name: string });
export = trace;
export as namespace trace;
sam it
  • 54
  • 7