2

I have an application which uses ClearScript to add JavaScript as an extension language. I've started using Typescript in other projects and thought I'd use it in this one as well.

My problem is that I have exposed quite a number of C# objects/methods/properties into the JavaScript environment. Aside from inserting

// ts-ignore

above every instance, is there a way of telling Typescript that certain symbols are "known"?

It would be nice to be able to be specific about the input and outputs of these functions so that TS could let me know when I'm not using them properly.

How do I declare external symbols?

For example, in this slice out of a object definition

  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();
  };

CSRestRequest is a symbol imported into the JavaScript environment from the C# side, using

 jSE.AddHostType("CSRestRequest", typeof(RestRequest)); 

(RestRequest is symbol provided by RestSharp.)

So how do I declare to Typescript that CSRestRequest is an external symbol that one news to generate an RestSharp RestRequest object?

1

2 Answers 2

1

Have you tried to use .d.ts files? These files contain type definitions and used by ts compiler for type checking.

You can find info here: https://www.typescriptlang.org/docs/handbook/declaration-files/introduction.html

For example, you could create external.d.ts file and add something like:

declare class CSRestRequest {
    // constructor(/* args */);

    // method(): void;
}

Then ts compiler should pick up external.d.ts file and resolve the type.

1
  • That is a winner! Thank you.
    – bugmagnet
    Jul 30, 2019 at 9:04
1

Generally typescript uses declarations to let the compiler know about objects that exist in the runtime environment but that are not created in typescript. Depending on the level of type safety you want you could just declare the external object as any or a more specific type.

Using any:

declare const CSRestRequest: any;
declare const client: any;
function getIp(username: string, password: string) {
  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();
};

Declare a class (more specific):

declare class CSRestRequest {
  AddParameter(name: string, value: any): void;
};
declare const client: {
  Execute(req: CSRestRequest): {
    Content: string
  }
};
function getIp(username: string, password: string) {
  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();
};

1
  • The latter form is where I just got to. Thanks.
    – bugmagnet
    Jul 30, 2019 at 9:05

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.