Skip to content

Instantly share code, notes, and snippets.

@ksaldana1
Last active January 30, 2019 16:35
Show Gist options
  • Save ksaldana1/cca64fb04c0979711542ccf04821ef9e to your computer and use it in GitHub Desktop.
Save ksaldana1/cca64fb04c0979711542ccf04821ef9e to your computer and use it in GitHub Desktop.
ts-alias-1
import { protos } from './my_company_protos'
export type User = protos.user.User;
export namespace protos {
export namespace user {
export interface User {
username: string;
info: protos.Info.User;
}
}
export namespace Info {
export interface User {
name: protos.Info.Name;
}
export interface Name {
firstName: string;
lastName: string;
}
}
}
import ts from "typescript";
// hardcode our input file
const filePath = "./src/models.ts";
// create a program instance, which is a collection of source files
// in this case we only have one source file
const program = ts.createProgram([filePath], {});
// pull off the typechecker instance from our program
const checker = program.getTypeChecker();
// get our models.ts source file AST
const source = program.getSourceFile(filePath);
// create TS printer instance which gives us utilities to pretty print our final AST
const printer = ts.createPrinter();
// helper to give us Node string type given kind
const syntaxToKind = (kind: ts.Node["kind"]) => {
return ts.SyntaxKind[kind];
};
// visit each node in the root AST and log its kind
ts.forEachChild(source, node => {
console.log(syntaxToKind(node.kind));
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment