ProgramGuru

Features of TypeScript

Learn Features of TypeScript

TypeScript combines JavaScript compatibility with editor assistance, type inference, useful type abstractions and incremental adoption.

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 features of typescript.
  • Predict the result of the complete runnable TypeScript example.
  • Recognize one common mistake and correct it.

Understand the concept

Its main advantages include autocomplete from known types, safer refactoring, checking function arguments, and expressing object shapes with interfaces or type aliases. Type inference reduces annotation noise. TypeScript integrates with the JavaScript ecosystem; it does not guarantee correctness, and its types do not automatically exist at runtime.

See the concept visually

Use the diagram to locate the checks, values or branches involved in the example.

Features of TypeScript: illustrated flowObject shape; Static property check; Editor feedback; Runtime object1Object shape2Static property check3Editor feedback4Runtime objectFollow the change from source values to the final result.
Features of TypeScript: illustrated flow. Object shape; Static property check; Editor feedback; Runtime object.

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

type Product = { name: string; price: number };
 const book: Product = { name: "Notebook", price: 80 };
 console.log(`${book.name}: ${book.price}`);

Verified output

Notebook: 80

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

Product expresses a shape containing name and price. The assignment is checked for those properties. The program reads the same ordinary JavaScript object when it runs.

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

An interface or type alias is not a runtime validator. Data obtained from JSON must still be checked when trust boundaries matter.

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 an inStock boolean property to Product and update book. Check what error appears if you omit the new required property.

Challenge: write down your prediction first, run the changed source, and explain any difference. Return to the verified version before continuing.

Check your understanding

Why is type inference useful?
Choose one answer. You can try again.

Answer and explanation

It often determines types without repeating an annotation.

Inference lets you keep source concise while retaining checking where sufficient information exists.

Review the key idea

Features of TypeScript: Its main advantages include autocomplete from known types, safer refactoring, checking function arguments, and expressing object shapes with interfaces or type aliases.

Inference lets you keep source concise while retaining checking where sufficient information exists.

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.

    Next lesson: Why Learn TypeScript? →