TypeScript Syntax
Learn TypeScript Syntax
TypeScript keeps JavaScript syntax for expressions, blocks and functions while adding type annotations and type-level declarations.
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 typescript syntax.
- Predict the result of the complete runnable TypeScript example.
- Recognize one common mistake and correct it.
Understand the concept
Identifiers name variables and functions. Braces delimit blocks; parentheses pass arguments; semicolons often mark statement boundaries but can frequently be omitted. A colon after a variable or parameter name introduces a type. TypeScript diagnoses invalid type relationships before emitted JavaScript runs.
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 items: number = 3;
const price: number = 25;
console.log(`Amount: ${items * price}`);Verified output
Amount: 75How 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
Both variables are declared with number annotations. The multiplication is a JavaScript expression whose result is substituted into a template literal.
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
A type annotation comes after the name, as in items: number, not before the name as in some other programming languages.
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
Add a third constant called tax using a number annotation, then print items * price + tax.
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
TypeScript Syntax: Identifiers name variables and functions.
The syntax is const identifier: Type = expression.
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.