Full answer
TypeScript is JavaScript with types. The compiler checks the types while you build, then removes them and produces plain JavaScript that the browser runs. Angular chose it because large apps need help: editors can autocomplete, refactoring is safe, and many mistakes appear as red lines and not as bugs in production. Decorators, which Angular uses for metadata, are also part of the TypeScript ecosystem.
Features used every day:
- Basic types and inference:
let marks: number = 80;andconst name = 'Asha'(TypeScript infersstring). Prefer inference when the type is obvious. - Interfaces and type aliases describe the shape of data from an API:
interface Student { id: number; name: string; email?: string }. The?means optional. - Generics:
signal<Student[]>([]),HttpClient.get<Student[]>(url),Observable<number>. The type in< >tells the tool what the container holds. - Union and literal types:
type Status = 'pending' | 'paid'allows only those two strings. This replaces many enums and removes typing mistakes. - Enums: still possible, but string literal unions are lighter and tree-shake better.
- Access modifiers:
private,protected,readonly. In templates, members must not beprivate, so useprotectedfor template-only members. - Strict mode (
"strict": true): turns onstrictNullChecks, soStudent | undefinedmust be checked before use. Angular projects use it by default, andstrictTemplatesapplies the same checking to templates. - Optional chaining and nullish coalescing:
student?.name ?? 'Unknown'. - Type guards and narrowing:
if (typeof value === 'string')lets TypeScript treat the value as a string inside the block.
A caution: types exist only at build time. A response from the server is not checked at run time, so get<Student[]> is a promise to the compiler and not a validation. If the data is untrusted, validate it.
any turns the checks off. Use unknown and narrow, or write the real type.
export {};
interface Student {
id: number;
name: string;
email?: string; // optional
}
type Status = 'pending' | 'paid'; // literal union
function label(student: Student | undefined, status: Status): string {
// Optional chaining and nullish coalescing under strict null checks
return `${student?.name ?? 'Unknown'} (${status})`;
}
function describe(value: string | number): string {
if (typeof value === 'string') {
return value.toUpperCase(); // narrowed to string
}
return value.toFixed(1); // narrowed to number
}
class Box<T> {
items: T[] = [];
add(item: T): void {
this.items.push(item);
}
}
const box = new Box<Student>();
box.add({ id: 1, name: 'Asha' });
// box.add({ id: 2 }); // compile error: name is missing
console.log(label(box.items[0], 'paid'), label(undefined, 'pending'));
console.log(describe('ok'), describe(3.14159));TypeScript 5.8 strict mode. The code is type-checked, and the script runs in Node 24 twice with identical output.
Key points
- TypeScript adds build-time types and compiles to JavaScript.
- Daily features: interfaces, generics, unions and literal types, optional chaining, strict null checks.
- `strict` and `strictTemplates` catch many errors early.
- Types do not validate data at run time. Avoid `any`.
Common mistakes
- Using `any` to silence errors.
- Thinking `get<Student[]>` checks the server data.
- Marking a field `private` and using it in the template.
Follow-ups
- What is the difference between `interface` and `type`?
- What does `strictNullChecks` change?
- When would you use `unknown` instead of `any`?
Tips
- Name five features and one run time limit.
- Mention strict mode, as Angular enables it.
- Prefer literal unions to enums, and say why.
Evaluation rubric
- Explains the role of TypeScript in Angular.
- Names practical features with examples.
- Knows that types are build-time only.