ProgramGuru

Setting Up TypeScript in VS Code

Learn Setting Up TypeScript in VS Code

Visual Studio Code offers TypeScript-aware autocomplete, rename refactoring, diagnostic messages and navigation while you edit.

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

Understand the concept

Install VS Code, open a folder rather than an isolated file, and create main.ts. The editor includes TypeScript language support; a local typescript package is still useful for repeatable command-line checking. The status bar can help select the workspace TypeScript version.

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 Account = { owner: string; active: boolean };
 const account: Account = { owner: "Riya", active: true };
 console.log(account.owner);

Verified output

Riya

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

When you type account. the editor can suggest owner and active because it knows the Account shape. The program itself simply reads a property at runtime.

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

Autocomplete suggestions are not proof that a runtime value is valid. Check externally supplied data before trusting it.

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

Create this file in VS Code, hover over account, then try account.unknownProperty and examine the editor error. Undo the invalid access before running.

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

Check your understanding

What provides most of the editor suggestions for account?
Choose one answer. You can try again.

Answer and explanation

The statically known Account type.

TypeScript language services analyze the source locally to provide type-aware editing feedback.

Review the key idea

Setting Up TypeScript in VS Code: Install VS Code, open a folder rather than an isolated file, and create main.

TypeScript language services analyze the source locally to provide type-aware editing feedback.

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: Your First TypeScript Program →