Sub topic 1
Why TypeScript?
TypeScript is JavaScript with a static type system. A static type system checks how values are used before the program runs. TypeScript then removes the type information and produces ordinary JavaScript.
Prerequisites
You should know JavaScript functions, objects, modules, and basic terminal commands. You should also have Node.js and npm installed and understand the purpose of package.json. This chapter assumes that you know JavaScript can run without a separate compilation step.
The problem TypeScript solves
JavaScript is flexible. A variable can hold a string at one point and a number later. An object can gain new properties while the program runs. This flexibility is useful, but it also allows mistakes to travel far from where they were created.
function formatUser(name: string, age: number): string {
return `${name} is ${age} years old`;
}
const user = {
name: "Ada",
age: 36
};
console.log(formatUser(user.name, user.age));
console.log(formatUser(user.age, user.name));The second call passes the arguments in the wrong order. TypeScript reports the error while you edit or compile the file. In plain JavaScript, the call runs and produces misleading output:
36 is Ada years oldThe mistake might be easy to fix in this small example. In a larger application, the incorrect value could pass through several functions before causing a failure.
Static types in practice
A type describes the values that a variable, parameter, or return value may contain. In the example, name: string says that name must be text. The return annotation : string says that the function must return text.
TypeScript can also infer many types without explicit annotations:
const userName = "Ada"; // inferred as string
const userAge = 36; // inferred as number
function describeUser(name: string, age: number) {
return `${name} is ${age} years old`;
}Inference keeps code readable while preserving checks. Explicit annotations are most useful at boundaries, such as function parameters, exported functions, API responses, and configuration values.
| JavaScript risk | TypeScript help |
|---|---|
| Misspelled object property | Reports that the property does not exist |
| Wrong function argument | Checks the argument type |
| Missing return value | Checks the declared return type |
| Possible `undefined` value | Requires a check when strict settings are enabled |
Types do not prove that a program is correct. They cannot tell whether a tax calculation uses the right formula or whether an API returned truthful data. They do catch a large class of shape and usage errors before execution.
What TypeScript does not change
TypeScript is not a different runtime. Browsers and Node.js execute JavaScript, not TypeScript type annotations. The TypeScript compiler, commonly run as tsc, checks the source and can emit JavaScript.
flowchart LR
A[TypeScript source] --> B[Type checking]
B --> C[JavaScript output]
C --> D[Node.js or browser]
B --> E[Compiler errors]The type annotations are erased during this process. A type such as string does not validate data arriving from a network request at runtime. Data from outside the program still needs validation when it enters the system.
The trade-off
TypeScript adds a development step and requires teams to understand types. Small scripts may not benefit enough to justify that cost. Larger codebases often gain safer refactoring, clearer function contracts, better editor assistance, and earlier feedback.
The value grows when many people change the same code, when modules have complicated data flows, or when a failure would be expensive to discover in production. TypeScript works best when the team treats compiler errors as useful feedback instead of bypassing every difficult check.
Exercise
A function receives an object from an HTTP request. Does declaring that parameter as a TypeScript type prove that the received object has the expected fields at runtime?
Show answer
No. The type helps check code during development, but runtime input must be validated separately.