We use ClearScript to extend our apps. We expose various C# objects into JavaScript, using ClearScript's AddHostType and AddHostObject methods.
Recently we've started using Typescript in our Google Apps Script projects and thought it would be good to use it to manage our ClearScript scripts as well.
This has proved to be an interesting experience. We know know about such things as external.d.ts and have started to write one to handle the various symbols that we're injecting in from the C# side.
Some things we've been able to work out. For example, in this code
function SchTasks(instruction:string) {
var proc = CSProcess.Start("C:\\Windows\\System32\\schtasks.exe", instruction);
return proc;
}
this declaration seems to work
declare namespace CSProcess {
function Start(a:string, b:string): any;
}
Likewise, with this block
that.getMyIP = function () {
var request = new CSRestRequest();
request.AddParameter("user", username);
request.AddParameter("pass", password);
request.AddParameter("command", "getmyip");
var response = client.Execute(request);
return response.Content.trim();
};
this declaration appears to work
declare class CSRestRequest {
constructor (str?:any) ;
AddParameter(a:string, b:string) : any;
}
What's puzzling us at the moment is how to declare the following. The symbol is called CSSettings and it's an instance of Dictionary<string,object>, so it has some methods hanging off it, like .ContainsKey. What's the best way to declare this? It can't be newed so it's not a class with a constructor, but it does have methods like a class.
var taskName = "\XChecker\M_XChecker_" + (function () {
if (CSSettings.ContainsKey("/TEST")) {
return "TEST";
}
if (CSSettings.ContainsKey("/PRODUCTION")) {
return "PRODUCTION";
}
return "UNKNOWN";
}
()) + "_" + CSSettings("/TASKRULESETGROUPLUTFKS") + "_" + CSSettings("/INSTANCENUMBER") + "_" + CSSettings("/INSTANCECOLUMN");
LATER
The following appears to work. What's curious is that VSCode is not reporting a clash of symbol names. Can CSSetting really be a function and a namespace at the same time? Apparently, yes.
declare function CSSettings(s:string):any;
declare namespace CSSettings {
function ContainsKey(s:string):boolean;
}