Why does Angular use TypeScript instead of plain JavaScript, and what benefit does it provide?
Short answer
TypeScript adds type checking to JavaScript, catching many common mistakes, like passing the wrong kind of value to a component, before the code even runs, right inside your code editor.
Advertisement
AModel answer
TypeScript adds type checking to JavaScript, catching many common mistakes, like passing the wrong kind of value to a component, before the code even runs, right inside your code editor. This makes larger Angular codebases easier to maintain and helps new developers understand exactly what data a component or service expects.
@Component({
selector: 'app-user-card',
template: `<p>{{ name }} is {{ age }} years old</p>`
})
export class UserCardComponent {
@Input() name!: string;
@Input() age!: number;
}Real-world example
A team catches a bug before it ever reaches production, where a component was accidentally passed a string instead of the expected number for a user's age, thanks to TypeScript immediately flagging the mismatch right inside the code editor.
Likely follow-ups
- Does Angular require TypeScript, or could you technically build an Angular app in plain JavaScript?
- What is the difference between a type and an interface in TypeScript?
Related topics