CodeBlanks Logo Icon CodeBlanks

Summary - Custom type checker

Write a custom rule checker.

Mini type checker

A mini type checker combines three pieces: walk the AST with ts.forEachChild, recognize nodes you care about via guards like ts.isVariableDeclaration, and report violations with ts.getLineAndCharacterOfPosition. Collect errors in an array and print them at the end.

function visit(node: ts.Node) {
  if (ts.isVariableDeclaration(node) && !node.type) {
    const { line } = ts.getLineAndCharacterOfPosition(sf, node.getStart(sf));
    errors.push(`line ${line + 1}: missing type`);
  }
  ts.forEachChild(node, visit);
}

Practice Report untyped variables

Create a source with three variables, each on its own line: let x = 1;, let y: string = "a"; and let z = true;. Write a visit function that finds VariableDeclaration nodes without a type annotation and collects an error in the form line N: missing type with its line number. Walk with ts.forEachChild and print one error per line.

Starter code

index.ts
import * as ts from "typescript";

// Üç satırlık kaynak: let x = 1; / let y: string = "a"; / let z = true;
// SourceFile oluştur
// visit fonksiyonu yaz: Tür eklemesi olmayan VariableDeclaration bul
// Satır numarasıyla line N: missing type biçiminde topla
// forEachChild ile gez
// Hataları her satırda bir tane olarak yazdır

// kodunu buraya yaz

Sample solution

index.ts
import ts from "typescript";

const sourceText = 'let x = 1;\nlet y: string = "a";\nlet z = true;';
const sf = ts.createSourceFile("app.ts", sourceText, ts.ScriptTarget.Latest, true);

const errors: string[] = [];
function visit(node: ts.Node) {
  if (ts.isVariableDeclaration(node) && !node.type) {
    const { line } = ts.getLineAndCharacterOfPosition(sf, node.getStart(sf));
    errors.push(`line ${line + 1}: missing type`);
  }
  ts.forEachChild(node, visit);
}
visit(sf);
console.log(errors.join("\n"));

Expected output

line 1: missing type
line 3: missing type

Questions and answers

Review the lesson questions together with their answers. To test yourself, solve the lesson interactively.

1 Which trio comes together for a mini type checker?

  • getTypeChecker + writeFile + readFile
  • forEachChild + isVariableDeclaration + getLineAndCharacterOfPosition Correct answer
  • getSourceFile + getText + console.log
  • createSourceFile + JSON.stringify + writeFile

Explanation: forEachChild walks the AST, isVariableDeclaration recognizes the node, and getLineAndCharacterOfPosition reports the position. Together they form a mini checker.

2 Node start

Question
const start = node.____(sf);
Answer
const start = node.getStart(sf);

Explanation: node.getStart(sf) returns the node's start offset; getOffset, getPos and getStartPosition do not exist on Node.

3 Mini checker summary

Term Meaning
ts.forEachChild Walks the AST
ts.isVariableDeclaration Recognizes the node kind
node.getStart Gives the start offset
getLineAndCharacterOfPosition Gives the line number

All lessons in Expert