liveBook · Manning

TypeScript compiler

As explained in Pro Angular 16

The TypeScript compiler checks TypeScript source and emits JavaScript output without retaining TypeScript-only type annotations in the generated JavaScript.

The TypeScript compiler turns TypeScript files into JavaScript that a browser can execute. Type annotations guide compile-time checks but are not retained in the generated JavaScript. In an Angular development build, this is one part of a larger process: the Angular compiler transforms templates, and webpack bundles the resulting code for the browser. section 2.3.1 section 9.3.2

Listing 2.8. The contents of the todoList.ts file in the src/app folder
import { TodoItem } from "./todoItem";
 
export class TodoList {
    
    constructor(public user: string, private todoItems: TodoItem[] = []) {
        // no statements required
    }
 
    get items(): readonly TodoItem[]  { 
        return this.todoItems;
    }
 
    addItem(task: string) {
        this.todoItems.push(new TodoItem(task));
    }
}

The to-do list uses a typed constructor parameter, a typed array, and a readonly view of its items. The compiler reports attempts to modify that readonly array; the class can still add items through its own method. section 2.3.1

Type checking catches incompatible uses before the generated code reaches the browser. For example, a constructor parameter declared as `string` is not intended to receive an incompatible type. A `readonly TodoItem[]` return type also prevents callers from modifying the returned array through that typed view. These are compiler checks, not restrictions enforced by TypeScript types in the browser. section 2.3.1

Follow a development rebuild

Edit `src/main.ts` to add a `console.log` statement and save it. Angular’s development tools detect the change, rebuild the project, and reload the browser; the new message appears in its JavaScript console. With `ng serve`, the build starts from `main.ts`, follows imports, compiles dependencies, and produces bundles such as `main.js`. section 3.4.1 section 9.3.2

How Pro Angular 16 covers it

How deep it goes
Explains type-checking errors and the compiler’s place in the build, but does not detail compiler internals or manual invocation.
Examples
Uses typed to-do classes, edits to main.ts, browser console output, and generated bundle names.
What it assumes
Assumes an Angular project configured for TypeScript while introducing statements, saving changes, and checking the browser console from basics.
What you can do after
Diagnose/debug with it by recognizing type-checking errors and distinguishing TypeScript compilation from template compilation and bundling.
How it connects
Connects static types and readonly arrays to Angular template compilation, webpack bundling, and browser execution.

Also explained in

Compare how 2 books explain TypeScript compiler →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Pro Angular 16. The section links point to the parts of the book it draws on.