Last active
January 30, 2019 16:35
-
-
Save ksaldana1/cca64fb04c0979711542ccf04821ef9e to your computer and use it in GitHub Desktop.
ts-alias-1
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { protos } from './my_company_protos' | |
export type User = protos.user.User; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | |
} | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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