How can I generate types using AST?

This page summarizes the projects mentioned and recommended in the original post on /r/typescript

Our great sponsors
  • SurveyJS - Open-Source JSON Form Builder to Create Dynamic Forms Right in Your App
  • InfluxDB - Power Real-Time Data Analytics at Scale
  • WorkOS - The modern identity platform for B2B SaaS
  • TypeScript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • Been doing a little bit of digging into how to do code generation with typescript, but I'm struggling to find resources on how to actually use the ts factory API without going directly into the source code. So far, this has been the most useful excerpt I've found: https://github.com/microsoft/TypeScript/wiki/Using-the-Compiler-API#creating-and-printing-a-typescript-ast

  • MakeTypes

    Make TypeScript types and proxy objects from example JSON objects. Can use proxy objects to dynamically type check JSON at runtime.

  • Found a couple libraries like MakeTypes, but they are going in the wrong direction for my needs. My object describes how to make the typing information, but is not an implementation of that typing information.

  • SurveyJS

    Open-Source JSON Form Builder to Create Dynamic Forms Right in Your App. With SurveyJS form UI libraries, you can build and style forms in a fully-integrated drag & drop form builder, render them in your JS app, and store form submission data in any backend, inc. PHP, ASP.NET Core, and Node.js.

    SurveyJS logo
  • TypeScriptToLua

    Typescript to lua transpiler. https://typescripttolua.github.io/

  • You can take a look at TypescriptToLua which transforms the TS AST into Lua code using the compiler API, however I think you're barking up the wrong tree. The TS compiler works on *it's own\* AST generated from parsed Typescript. You can see how they implement your own type here. What you have some structured data and want to turn it into a type definition. That's just some loops you write yourself over data you've hopefully structured in a sane way. Turn it into a string, write it to a file with the extension `.d.ts`. No libraries needed.

NOTE: The number of mentions on this list indicates mentions on common posts plus user suggested alternatives. Hence, a higher number means a more popular project.

Suggest a related project

Related posts