Static analysis project
Concept and boundary
A useful source inspector separates syntax selection from semantic resolution. Use the public checker to enumerate module exports and inspect alias targets; type-only exports can appear even though they have no runtime representation. Treat missing symbols as an explicit outcome rather than throwing blindly.
Local tooling setup
For each workshop use a fresh folder. Install the pinned compiler with npm install --save-dev typescript@5.8.3, then create the listed files and run node example.cjs with Node.js 22.16.0. Assertions fail if the expected behavior changes. These are local JavaScript tooling scripts, not browser CodeRunner tasks; they do not independently type-check the tooling source.
Workshop 1: get-exports-of-module
Revisit reference fixture ts-api-get-exports-of-module (historical V27, rerun in V74).
const ts = require("typescript");const assert = require("node:assert/strict");const fs = require("node:fs");const path = require("node:path");function inspect(text, extra = {}) { fs.writeFileSync("main.ts", text); for (const [name, content] of Object.entries(extra)) fs.writeFileSync(name, content); const program = ts.createProgram([path.resolve("main.ts")], { strict: true, types: [], target: ts.ScriptTarget.ES2022, module: ts.ModuleKind.CommonJS, noEmit: true }); assert.deepEqual(ts.getPreEmitDiagnostics(program).map(d => d.code), []); const source = program.getSourceFile(path.resolve("main.ts")); const checker = program.getTypeChecker(); function find(predicate) { function visit(node) { return predicate(node) ? node : ts.forEachChild(node, visit); } const node = visit(source); assert(node); return node; } function variable(name) { return find(n => ts.isVariableDeclaration(n) && ts.isIdentifier(n.name) && n.name.text === name); } return { source, checker, find, variable };}const {checker:c,source}=inspect("export { item as publicItem } from './dep'; export interface Shape { n:number } const hidden=1;",{"dep.ts":"export const item=7;"});const s=c.getSymbolAtLocation(source); assert(s);const names=c.getExportsOfModule(s).map(x=>x.getName()).sort();assert.deepEqual(names,["Shape","publicItem"]); console.log(names.join(","));Expected stdout (exit 0):
Shape,publicItemWorkshop 2: get-aliased-symbol
Revisit reference fixture ts-api-get-aliased-symbol (historical V27, rerun in V74).
const ts = require("typescript");const assert = require("node:assert/strict");const fs = require("node:fs");const path = require("node:path");function inspect(text, extra = {}) { fs.writeFileSync("main.ts", text); for (const [name, content] of Object.entries(extra)) fs.writeFileSync(name, content); const program = ts.createProgram([path.resolve("main.ts")], { strict: true, types: [], target: ts.ScriptTarget.ES2022, module: ts.ModuleKind.CommonJS, noEmit: true }); assert.deepEqual(ts.getPreEmitDiagnostics(program).map(d => d.code), []); const source = program.getSourceFile(path.resolve("main.ts")); const checker = program.getTypeChecker(); function find(predicate) { function visit(node) { return predicate(node) ? node : ts.forEachChild(node, visit); } const node = visit(source); assert(node); return node; } function variable(name) { return find(n => ts.isVariableDeclaration(n) && ts.isIdentifier(n.name) && n.name.text === name); } return { source, checker, find, variable };}const {checker:c,find}=inspect("import { item as local } from './dep'; const copy=local;",{"dep.ts":"export const item=7;"});const s=c.getSymbolAtLocation(find(ts.isImportSpecifier).name); assert(s.flags & ts.SymbolFlags.Alias);const target=c.getAliasedSymbol(s); assert.notEqual(s,target);assert.equal(s.getName(),"local"); assert.equal(target.getName(),"item");console.log("local -> item");Expected stdout (exit 0):
local -> itemWorkshop 3: export-report
const ts = require("typescript");const assert = require("node:assert/strict");const p = ts.createProgram(["input.ts"], { types: [], noLib: true });const c = p.getTypeChecker();const file = p.getSourceFile("input.ts");const s = c.getSymbolAtLocation(file);const report = c.getExportsOfModule(s).map(e => { const target = e.flags & ts.SymbolFlags.Alias ? c.getAliasedSymbol(e) : e; return { name: e.name, value: !!(target.flags & ts.SymbolFlags.Value), file: target.declarations[0].getSourceFile().fileName };}).sort((a,b) => a.name.localeCompare(b.name));assert.deepEqual(report,[{name:"Shape",value:false,file:"input.ts"},{name:"value",value:true,file:"input.ts"}]);console.log(JSON.stringify(report));Expected stdout (exit 0):
[{"name":"Shape","value":false,"file":"input.ts"},{"name":"value","value":true,"file":"input.ts"}]Practice and recap
Build a JSON report of export names, declaration files and whether each symbol has a value flag.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
Static analysis project — knowledge check
Check the concept and interpret the locally verified compiler tooling probes.
Which statement correctly describes Static analysis project?
What does workshop 1 (get-exports-of-module) print when run unchanged with the specified compiler?
const ts = require("typescript");const assert = require("node:assert/strict");const fs = require("node:fs");const path = require("node:path");function inspect(text, extra = {}) { fs.writeFileSync("main.ts", text); for (const [name, content] of Object.entries(extra)) fs.writeFileSync(name, content); const program = ts.createProgram([path.resolve("main.ts")], { strict: true, types: [], target: ts.ScriptTarget.ES2022, module: ts.ModuleKind.CommonJS, noEmit: true }); assert.deepEqual(ts.getPreEmitDiagnostics(program).map(d => d.code), []); const source = program.getSourceFile(path.resolve("main.ts")); const checker = program.getTypeChecker(); function find(predicate) { function visit(node) { return predicate(node) ? node : ts.forEachChild(node, visit); } const node = visit(source); assert(node); return node; } function variable(name) { return find(n => ts.isVariableDeclaration(n) && ts.isIdentifier(n.name) && n.name.text === name); } return { source, checker, find, variable };}const {checker:c,source}=inspect("export { item as publicItem } from './dep'; export interface Shape { n:number } const hidden=1;",{"dep.ts":"export const item=7;"});const s=c.getSymbolAtLocation(source); assert(s);const names=c.getExportsOfModule(s).map(x=>x.getName()).sort();assert.deepEqual(names,["Shape","publicItem"]); console.log(names.join(","));What does workshop 2 (get-aliased-symbol) print when run unchanged with the specified compiler?
const ts = require("typescript");const assert = require("node:assert/strict");const fs = require("node:fs");const path = require("node:path");function inspect(text, extra = {}) { fs.writeFileSync("main.ts", text); for (const [name, content] of Object.entries(extra)) fs.writeFileSync(name, content); const program = ts.createProgram([path.resolve("main.ts")], { strict: true, types: [], target: ts.ScriptTarget.ES2022, module: ts.ModuleKind.CommonJS, noEmit: true }); assert.deepEqual(ts.getPreEmitDiagnostics(program).map(d => d.code), []); const source = program.getSourceFile(path.resolve("main.ts")); const checker = program.getTypeChecker(); function find(predicate) { function visit(node) { return predicate(node) ? node : ts.forEachChild(node, visit); } const node = visit(source); assert(node); return node; } function variable(name) { return find(n => ts.isVariableDeclaration(n) && ts.isIdentifier(n.name) && n.name.text === name); } return { source, checker, find, variable };}const {checker:c,find}=inspect("import { item as local } from './dep'; const copy=local;",{"dep.ts":"export const item=7;"});const s=c.getSymbolAtLocation(find(ts.isImportSpecifier).name); assert(s.flags & ts.SymbolFlags.Alias);const target=c.getAliasedSymbol(s); assert.notEqual(s,target);assert.equal(s.getName(),"local"); assert.equal(target.getName(),"item");console.log("local -> item");What does workshop 3 (export-report) print when run unchanged with the specified compiler?
const ts = require("typescript");const assert = require("node:assert/strict");const p = ts.createProgram(["input.ts"], { types: [], noLib: true });const c = p.getTypeChecker();const file = p.getSourceFile("input.ts");const s = c.getSymbolAtLocation(file);const report = c.getExportsOfModule(s).map(e => { const target = e.flags & ts.SymbolFlags.Alias ? c.getAliasedSymbol(e) : e; return { name: e.name, value: !!(target.flags & ts.SymbolFlags.Value), file: target.declarations[0].getSourceFile().fileName };}).sort((a,b) => a.name.localeCompare(b.name));assert.deepEqual(report,[{name:"Shape",value:false,file:"input.ts"},{name:"value",value:true,file:"input.ts"}]);console.log(JSON.stringify(report));