Parse TypeScript Source

import { fs as promises } from 'fs';
import typescript from 'typescript';

const source = await fs.readFile('/path/to/a-typescript-file.ts', 'utf8'),
const node = typescript.createSourceFile('name.ts', source, typescript.ScriptTarget.Latest);

The createSourceFile generates an Abstract Syntax Tree (AST) for the given source file.

Each node has the numerical kind property, that represents the type of that node.

Let's go over each child and list their type:

node.forEachChild(child => console.log(typescript.SyntaxKind[child.kind]))
> ImportDeclaration
> ClassDeclaration
> EndOfFileToken

Subscribe to My Newsletter

The latest programming-related news, articles and resources - sent to your inbox monthly. Unsubscribe anytime.