0
0
Typescriptprogramming~10 mins

TypeScript compiler API basics - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - TypeScript compiler API basics
Start: Load TypeScript API
Create Program with files
Get Source Files
Parse and Analyze AST
Perform Type Checking
Emit JavaScript Output
End
The TypeScript compiler API flow starts by loading the API, creating a program with source files, parsing and analyzing the code, checking types, and finally emitting JavaScript output.
Execution Sample
Typescript
import * as ts from 'typescript';

const program = ts.createProgram(['example.ts'], {});
const sourceFile = program.getSourceFile('example.ts');

console.log(sourceFile?.statements.length);
This code loads the TypeScript API, creates a program for 'example.ts', gets its source file, and logs the number of top-level statements.
Execution Table
StepActionInput/ConditionResult/Output
1Import TypeScript APIts modulets object available
2Create ProgramFiles: ['example.ts'], Options: {}Program object created with files
3Get Source FileFilename: 'example.ts'SourceFile object or undefined
4Access StatementssourceFile?.statementsArray of statements in source file
5Log statements lengthsourceFile?.statements.lengthNumber of top-level statements printed
6EndNo more actionsProgram ends
💡 All steps complete; program ends after logging statement count.
Variable Tracker
VariableStartAfter Step 2After Step 3After Step 4Final
tsundefinedts module importedts module importedts module importedts module imported
programundefinedProgram object createdProgram object createdProgram object createdProgram object created
sourceFileundefinedundefinedSourceFile object or undefinedSourceFile object or undefinedSourceFile object or undefined
sourceFile?.statements.lengthundefinedundefinedundefinedNumber of statementsNumber of statements
Key Moments - 2 Insights
Why might sourceFile be undefined after calling getSourceFile?
If the filename is not included in the program's files or the file does not exist, getSourceFile returns undefined, as shown in step 3 of the execution_table.
What does sourceFile?.statements.length represent?
It represents the number of top-level statements in the TypeScript source file, as accessed in step 4 and logged in step 5.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the result of step 3 (Get Source File)?
AA SourceFile object or undefined
BAn array of statements
CA Program object
DA number representing statement count
💡 Hint
Check the 'Result/Output' column for step 3 in the execution_table.
At which step is the number of top-level statements logged?
AStep 2
BStep 4
CStep 5
DStep 6
💡 Hint
Look for the action 'Log statements length' in the execution_table.
If 'example.ts' is missing, how does the variable 'sourceFile' change after step 3?
AIt becomes a Program object
BIt becomes undefined
CIt contains an empty array
DIt throws an error
💡 Hint
Refer to key_moments explanation about sourceFile being undefined if file is missing.
Concept Snapshot
TypeScript compiler API basics:
- Import 'typescript' module
- Create a Program with source files
- Get SourceFile objects
- Access AST nodes like statements
- Use API to analyze or emit code
- Handle undefined if file missing
Full Transcript
This visual execution shows how to use the TypeScript compiler API. First, the API module is imported. Then, a Program is created with a list of source files. Next, a SourceFile object is retrieved for a specific file. The code accesses the statements array from the SourceFile, which represents top-level code parts. Finally, it logs the number of statements. If the file is missing, the SourceFile is undefined. This step-by-step trace helps beginners see how the API loads and processes TypeScript code.