javascripttypescriptclearscript

How to define items to be ignored in Typescript


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?


Solution

  • 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.