Skip to main content

The typescript interface parser parses interfaces defined in typescript and outputs a JSON object describing the interfaces.

Project description

Typescript Interface Parser (and to JSON converter)

The typescript interface parser parses interfaces defined in typescript and outputs a JSON object describing the interfaces.

Preview

Features

  • output to file and to stdio
  • comments are also considered and provided by the JSON representation
  • supports a wide range of typescript syntax (if you find something missing please file a feature request)
  • can be easily integrated in your own program for parsing typescript interfaces

Table of Contents

  1. Installation
    1. Pypi
    2. Manual Installation
    3. Running the Unit Tests
  2. The JSON Representation
    1. Translation of Attributes
    2. Indexed Attributes
    3. Function Attributes

MISC

I needed to communicate with a server via RPC from a Python client, but all valid messages were specified in Typescript interfaces. There were a huge number of different messages and I slowly got bored translating the interfaces manually. So I implemented this translator. Hope it will serve you, as well as it served me.

Installation

Pypi

Manual installation

Clone the repo and install the requirements with pip install -r requirements.txt. Than type python3 ts_interface_parser.py -h to get an overview of the possible options.

Running the Unit Tests

python3 -m unittest test.test_parser.TestParser

The JSON Representation

The general translation works as follows:

/**
* <some comment>
*/
interface <interface_name> extends <extension_a>, <extension_b> {
        <attribute_1> : <type_1>;
}

is translated to

{
        "<interface_name>" : {
                "description" : "<some comment>",
                "extends": [
                        "<extension_a>",
                        "<extension_b>"
                ]
                "<attribute_1> : {
                        "type" : [
                                "<type_1>"
                        ]
                }
        }
}

The comment becomes the decsription and potential extensions are provided in the extends field. For every specified attribute an object is added referenced by the name of the attribute. Here <attribute_1>.

Translation of Attributes

In the following I give examples of different attribute definitions and how those are translated into JSON.

interface <interface_name> {
        <attribute_name_1> : <type_1> | <type_2>; // <comment_1>
        /**
        * <comment_2>
        */
        <attribute_name_2> : {
                <attribute_name_3> : <type_3>
        }

        <attribute_name_4>? : <type_4>;

        const <attribute_name_5> : <type_5>;

        readonly <attribute_name_6> : <type_6>;

        <attribute_name_7> : "value 1" | "value 2";
}

is translated to

{
        "<interface_name>" : {
                "<attribute_name_1>" : {
                        "description" : "<comment_1">,
                        "type" : [
                                "type_1",
                                "type_2"
                        ]
                },
                "<attribute_name_2>" : {
                        "description" : "<comment_2">,
                        "type": {
                                "<attribute_name_3>" : {
                                        "type" : [
                                                "type_3"
                                        ]
                                }
                        }
                },
                "<attribute_name_4>" : {
                        "optional" : true,
                        "type" : [
                                "<type_4>"
                        ]
                },
                "<attribute_name_5>" : {
                        "constant" : true,
                        "type" : [
                                "<type_5>"
                        ]
                },
                "<attribute_name_6>" : {
                        "readonly" : true,
                        "type" : [
                                "<type_6>"
                        ]
                },
                "<attribute_name_7>" : {
                        "type" : [
                                "'value 1'",
                                "'value 2'",
                        ]
                }
        }
}

Indexed Attributes

interface ReadonlyStringArray {
        [index: number]: string;
}

The indexed attribute is named with the variable name within the square brackets. The attribute has a field indexed which contains the index type and the type the index is refering to.

{
        "ReadonlyStringArray": {
                "index": {
                        "indexed": {
                                "type": [
                                        "number"
                                ]
                        },
                        "type": [
                                "string"
                        ]
                }
        }
}

Function Attributes

Functions are marked by a field function in the JSON representation of the function. Thus

interface ClockInterface {
        setTime(d: Date): void;
}

translates to

{
        "ClockInterface": {
                "setTime": {
                        "function": true,
                        "parameters": {
                                "d": {
                                        "type": [
                                                "Date"
                                        ]
                                }
                        },
                        "type": [
                                "void"
                        ]
                }
}

For anonymous function declarations, as for instance:

interface SearchFunc{
        (source: string, subString: string): boolean;
}

This interface is translated to:

{
        "SearchFunc": {
                "anonymous_function": {
                    "function": true,
                    "parameters": {
                        "source": {
                            "type": [
                                "string"
                            ]
                        },
                        "subString": {
                            "type": [
                                "string"
                            ]
                        }
                    },
                    "type": [
                        "boolean"
                    ]
                }
        }
}

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

ts_interface_parser-0.0.1.tar.gz (4.5 kB view details)

Uploaded Source

Built Distribution

ts_interface_parser-0.0.1-py3-none-any.whl (5.4 kB view details)

Uploaded Python 3

File details

Details for the file ts_interface_parser-0.0.1.tar.gz.

File metadata

  • Download URL: ts_interface_parser-0.0.1.tar.gz
  • Upload date:
  • Size: 4.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/2.0.0 pkginfo/1.5.0.1 requests/2.22.0 setuptools/41.4.0 requests-toolbelt/0.9.1 tqdm/4.36.1 CPython/3.7.2

File hashes

Hashes for ts_interface_parser-0.0.1.tar.gz
Algorithm Hash digest
SHA256 5f4c7b2425441df2204b9e1bcada7ce5ccd7b2b25beba0e39db8e592c22589bd
MD5 1ae7e4e87ba9840475ccda503f9279b7
BLAKE2b-256 05ef03cb70743e53ca6a47a2be01dc56d3a8ffc6a660b8269643f741b12e81f4

See more details on using hashes here.

File details

Details for the file ts_interface_parser-0.0.1-py3-none-any.whl.

File metadata

  • Download URL: ts_interface_parser-0.0.1-py3-none-any.whl
  • Upload date:
  • Size: 5.4 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/2.0.0 pkginfo/1.5.0.1 requests/2.22.0 setuptools/41.4.0 requests-toolbelt/0.9.1 tqdm/4.36.1 CPython/3.7.2

File hashes

Hashes for ts_interface_parser-0.0.1-py3-none-any.whl
Algorithm Hash digest
SHA256 a43c90661f9deb32fba5617c94f42a49a03b662766bfce36e8460c71f6914efe
MD5 3d6021254925e8d43006d713ba3f8ea2
BLAKE2b-256 2982e06ab1d8e9b4fe0fa70b74ce82777be976f42781d8a70737a5b4c6f44534

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page