0

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;
}
0

1 Answer 1

1

declare your dictionary class. and then variable CSSettings with this type

declare class IDictionary {
  constructor();
  public ContainsKey(key: string): boolean;
}

declare const CSSettings: IDictionary;

CSSettings.ContainsKey('key');

// edit

so what about to create 2 type and merge it? so you will be able to use CSSettings as function and even as object

declare type functionType = (key: string) => boolean;
declare const CSSettings: functionType & { ContainsKey: (key: string) => boolean };
CSSettings.prototype.ContainsKey = 1;


CSSettings('key');
CSSettings.ContainsKey('key');
3
  • He says it's not instantiable so that declaration is misleading. An interface would be appropriate. Jul 30, 2019 at 17:45
  • The above doesn't deal with the CSSettings(string) situation which is getting a value from the dictionary.
    – bugmagnet
    Jul 31, 2019 at 1:21
  • its was not clrear for me. CSSettings is not class. but it seems like an instance of a classic dictionary. but it can be interface whatever. Its just sample. Since it is only declaration not implementation does it really matter? Jul 31, 2019 at 7:55

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.