Quick look at TypeScript Compiler API

by Chau Tran

What is TypeScript Compiler API?

import * as ts from "typescript";

const source = "let x: string  = 'string'";

let result = ts.transpileModule(source, { compilerOptions: { module: ts.ModuleKind.CommonJS }});

console.log(JSON.stringify(result));

Transformer

const transformer = someCode => someCode

Compilation Stages

  1. Scan

  2. Parse

  3. Bind

  4. Transform

  5. Emit

Transform Stages

  1. before
  2. after
  3. afterDeclarations

So far...

We know that a Transformer is a function that takes in the AST (representation of our Source Code) and returns the altered (or not) AST.

We know that Transform stage happens AFTER the Parse stage. Which means our Transformer will be able to receive the AST from the Parser

What is the AST?

Ultimately, our goal is to modify this AST in our favor.

DEMO

Q&A

Made with Slides.com