Introduction to TypeScript
Learn Introduction to TypeScript
TypeScript adds a static type-checking layer to JavaScript. You write familiar JavaScript operations while making the expected shapes of values clear.
In this lesson, you will study the rule, run a complete TypeScript example, explain its output, and test a small change.
What you’ll learn
- Explain the main rule behind introduction to typescript.
- Predict the result of the complete runnable TypeScript example.
- Recognize one common mistake and correct it.
Understand the concept
A TypeScript source file commonly ends in .ts. The TypeScript compiler checks source against type rules, then can emit JavaScript for a browser or server. Types are a development-time aid: the output JavaScript does not retain ordinary type annotations. TypeScript does not automatically validate data arriving from a user or network.
See the concept visually
Use the diagram to locate the checks, values or branches involved in the example.
Run a complete TypeScript example
Save this code as main.ts or paste it into the TypeScript Playground. The example uses fixed values so it runs without prompting for input.
TypeScript code
const learner: string = "Mina";
const lessonsCompleted: number = 2;
console.log(`${learner} finished ${lessonsCompleted} lessons.`);Verified output
Mina finished 2 lessons.How to run locally: npx tsc --strict --target es2022 --module commonjs main.ts, then node main.js. When using a project configuration, run the matching project build instead.
Trace the execution
The declaration assigns a string to learner and a number to lessonsCompleted. The template literal inserts their runtime values. The annotations help the checker but do not print or travel to JavaScript.
Predict the output before you run the sample. Compare each line with the source expression that produced it, and distinguish the type checker’s work from what happens in JavaScript at runtime.
Common mistakes and how to avoid them
Do not assume writing : number converts input into a number. A type annotation is not an operation that changes a value.
Debugging habit: when a change is rejected, read the exact compiler diagnostic and inspect the operand types. When it compiles but behaves unexpectedly, follow the values through the JavaScript expressions.
Practice it yourself
Change the learner and count; then deliberately assign a string to lessonsCompleted and observe the type error before restoring the valid version.
Challenge: write down your prediction first, run the changed source, and explain any difference. Return to the verified version before continuing.
Check your understanding
Review the key idea
Introduction to TypeScript: A TypeScript source file commonly ends in.
Most annotations are erased from emitted JavaScript; TypeScript cannot replace runtime validation.
Before continuing, be able to explain what is checked before execution and what actually changes at runtime in the example above.
Reaching the recap and completing the quizzes are separate milestones.