Video lecture
Watch on YouTube(opens in a new tab)Sub topic 2
Why Angular Uses TypeScript
This chapter assumes you know modern JavaScript (ES modules, classes, arrow functions) and basic TypeScript types. It explains why Angular is built on TypeScript and which TypeScript features you will use most often. It also covers the type errors new Angular developers run into most.
TypeScript in One Sentence
TypeScript is JavaScript plus static types. The compiler checks your code before it runs and then removes the types, so the browser receives plain JavaScript. Angular itself is written in TypeScript. The CLI sets up every new project to use it, so TypeScript is the normal way to write Angular code.
What Angular Gets from TypeScript
| Feature | How Angular uses it |
|---|---|
| Static types | Catches wrong property names and wrong argument types at build time |
| Decorators | `@Component` and `@Injectable` attach metadata to classes |
| Classes and access modifiers | Components and services are classes, and `private` hides internals |
| Generics | Typed APIs such as `signal<number>()` and `input<string[]>()` |
| Template type checking | The compiler checks the expressions in your HTML templates against your component class |
The last row matters most in practice. With React or Vue, template type checking is optional or needs extra tooling. Angular's compiler reads your templates and checks them against the component's types. If you write {{ user.nmae }}, the build fails and points you to the typo.
Types Catch Mistakes Early
The plain TypeScript example below shows the kind of error the compiler reports:
interface Product {
id: number;
name: string;
price: number;
}
function formatPrice(product: Product): string {
return `${product.name}: $${product.price.toFixed(2)}`;
}
const item: Product = { id: 1, name: 'Keyboard', price: 49.5 };
console.log(formatPrice(item)); // Keyboard: $49.50
// formatPrice({ id: 2, name: 'Mouse' });
// Error: Property 'price' is missing in type '{ id: number; name: string; }'Remove the // from the last call and the compiler rejects it. In plain JavaScript the same mistake would only show up when the code runs, as undefined.toFixed.
Strict Mode Is On by Default
ng new turns on strict settings in tsconfig.json:
{
"compilerOptions": {
"strict": true,
"noImplicitReturns": true,
"noFallthroughCasesInSwitch": true
},
"angularCompilerOptions": {
"strictInjectionParameters": true,
"strictInputAccessModifiers": true,
"strictTemplates": true
}
}strict turns on null checks and blocks untyped parameters from silently becoming any. strictTemplates applies the same rules inside templates.
Common Errors and Fixes
- "Property 'x' has no initializer": a class field is declared but never assigned. Give it a starting value (
title = '') or set it in the constructor. - "Object is possibly 'null'": you are using a value that may be missing. Add a check (
if (user) { ... }), use optional chaining (user?.name), or handle the missing case in the template with@if. - Reaching for
any:anyswitches off checking for that value, so the compiler can no longer catch your mistakes. Declare an interface for the data instead. - Decorator errors in a plain
.jsfile: Angular decorators need the Angular compiler, so keep component and service code in.tsfiles.
Exercise
A component template contains {{ product.prise }}, but the Product interface defines price. With strictTemplates enabled, when do you find out about the typo, and why?
Show answer
You find out at build time, from ng serve or ng build, because the Angular compiler type-checks template expressions against the component's types and reports that 'prise' does not exist on Product.
Exercise
The compiler reports "Object is possibly 'null'" for user.name. Name two ways to fix it without disabling strict mode.
Show answer
Guard the access with a check such as if (user) { ... }, or use optional chaining such as user?.name. In a template, you can also wrap the expression in an @if (user) block.