Home > database >  Reference a typescript file as a library in script lab
Reference a typescript file as a library in script lab

Time:11-10

I have written a super small logger in a typescript class and placed it in a file call Log.ts. The file also contains a type definition. I would like to reference it as a library so that I can create an instance of that class in the script lab script. How do I do that?

I have placed the Log.ts file on a public website and referenced it in the Libraries tab in script lab but it is not getting picked up.

What do I need to do to be able to create a new logger as const log = new Log()?

Update

I have tried to create a minimal example. This is now the log.ts file:

export type PongType = "pong";

export class Log {
  ping(): PongType {
    return "pong";
  }
}

I have compiled this to a log.js as:

var Log = /** @class */ (function () {
    function Log() {
    }
    Log.prototype.ping = function () {
        return "pong";
    };
    return Log;
}());
export { Log };

I have then placed log.js on a public server and then tried to import it in the HTML section in script lab as suggested in the comments. This is done as:

<script src="https://somedomain.com/log.js"></script>

But I still don't understand how I could create an instance of Log() in the Script lab script.

CodePudding user response:

To my knowledge, there isn't a way to have Script Lab load TypeScript code directly from a .ts file using the Libraries tab. Script Lab can load public npm packages and JavaScript bundles. For example, the Office JS API is loaded as a JavaScript bundle from a .js file hosted on a CDN. I recommend you try compiling your logging code into a JavaScript bundle or publish a public npm package.

CodePudding user response:

The ScriptLab add-in allows running JS/TS code, but not loading external web pages with links to external scripts. If you need to run an external script from a file hosted anywhere on the web JS file you need to embed a reference to it to the HTML page to get it loaded. TS file must be processed before by the compiler to get JS. There is no way to run .ts files directly.

Due to the fact that ScriptLab is a Microsoft tool/add-in, I guess you can post or vote for an existing feature request on Tech Community where they are considered when the Office dev team go through the planning process.

  • Related