'How to generate function's names dynamically from the type declaration files?

I wish typescript had better(any) macros!

I'm trying to dynamically generate an interface to existing (but unknown) code given a set of type declaration files. These .d.ts typedefs are generated by the awesome json-schema-to-typescript package and look similar to the ones below. Basically i want to generate a class or a small library based on these declaration that other people can use.

Context

There are two types of thing that these typedefs describe that are roughly state and methods.

State

{
  "$schema": "http://json-schema.org/draft-07/schema#",
  "title": "RandomMessage",
  "type": "object",
  "required": [
    "property1",
    "property2"
  ],
  "properties": {
    "property1": {
      "type": "string"
    },
    "property2": {
      "type": "integer",
      "format": "int32"
    }
  }
}

// gets transformed to `random_message.d.ts`:
// ↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓
export interface RandomMessage {
  tag1: string;
  tag2: number;
  [k: string]: any;
}

Method

{
  "$schema": "http://json-schema.org/draft-07/schema#",
  "title": "QueryMsg",
  "anyOf": [
    {
      "type": "object",
      "required": [
        "get_count"
      ],
      "properties": {
        "get_count": {
          "type": "object"
        }
      },
      "additionalProperties": false
    }
  ]
}

// gets transformed to `query_msg.d.ts`:
// ↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓
export type QueryMsg = {
  get_count: {
    [k: string]: any;
  };
};

Given a bunch of these(unknown in advance), i'd like to generate and export a class that captures all of the state and methods functionality described by these typedefs such that the person who imports that class basically gets intellisense for those methods and properties.

My problems are:

  1. i don't know in advance what names (i.e. RandomMessage and QueryMsg) these state and method declarations provide to the objects they type and therefore what to import.
  2. even if i knew the names, i wouldn't know how to import them dynamically and combine them into one class... or object doesn't really matter so long as it's exportable
  3. i'm not sure how to differentiate between state-carrying properties and methods. For example, in the example above i know that RandomMessage is a state container because the top-level schema object has "type":"object" specified, whereas QueryMsg is probably a function because it can be satisfied by an empty object, otherwise said: it requires no properties and even if it did, it doesn't have to be an object.

I imagine it's a pretty standard task to generate code from typedefs like this or instrument a library with methods whose functionality is specified way but whose names are unkown in advance. Short of macros, which typescript doesn't have, i reached for Reflection so far, but am still struggling a little bit putting it together.

How would you do this?



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source