HexLibraryHexLibrary
</> HexCodeSign inSign up
PythonJavaScriptTypeScriptCC++JavaPHPGoRustRubyBashSQLLuaPerlCOBOLPascalDelphiFortranDartSwiftC#JuliaGroovyRElixirHaskellKotlinScalaClassic ASPClipperMS-DOS
LESSONS
Introduction to TypeScriptBasic TypesUnion & Literal TypesInterfacesTyped FunctionsArrays & TuplesGenerics & Enums
REFERENCE
📖 Overview
🖥️ Basics & Output
🏷️ Types
📐 Interface & Type Alias
🧩 Function
📋 Array & Object Types
🚀 Next
TypeScript›Introduction to TypeScript

Introduction to TypeScript

What is TypeScript, why type safety? First typed variables and how TS catches errors before running.

Simple enough for a 5-year-old to understand

TypeScript = JavaScript + Types

TypeScript adds "types" on top of JavaScript. All your JS code works here too; additionally, you can specify the types of variables.

Benefit: catches errors before the code runs.

First typed variable
1
let userName: string = "Alex";
userName must be a string.
VariableuserName must be a string.
2
let age: number = 36;
age must be a number.
Variableage must be a number.
3
console.log(userName, age);
Alex 36
Output / InputAlex 36
VariableOutput / Input
💡 Hint: : type annotation means "this variable must be of this type". If you assign the wrong type, TS warns you.

🎯 Exercise & Practice

Test what you've learned — answer, write code, check instantly. (2 left)

1.What is the main thing TypeScript adds to JavaScript?
2.Define a string type variable named city ("Athens") and print it.

🎯 Expected output: Athens

Loading editor…

About this topic

TypeScript is a language that adds a type system on top of JavaScript. It does not introduce a new runtime environment — when compiled, it turns into plain JavaScript and the browser or Node.js runs the exact same code. Type information lives only at compile time and does not exist at runtime at all.

This design has two important consequences. First: every valid JavaScript file is a valid TypeScript file, so you can migrate an existing project step by step. Second: TypeScript does not protect you at runtime — if data coming from an API does not match the type you declared, TypeScript won't notice it.

The value of TypeScript is not in finding bugs, but WHEN it finds them. In JavaScript, the user finds the error "undefined is not a function"; in TypeScript, the editor finds that same error as you are typing. In large projects, this difference means half of your maintenance cost.

What you will learn

  • How TypeScript compiles to JavaScript
  • That type information lives only at compile time
  • Compiling with tsc and the tsconfig file
  • What the strict mode brings
  • When type inference is sufficient

When to use?

TypeScript quickly proves its worth in any project touched by multiple people or expected to live for months. For a single-file script, it might be an unnecessary layer.

When starting a new project, enabling strict mode from the beginning is practically mandatory. Enabling it later means dealing with hundreds of accumulated warnings; when it's on from the start, they never pile up.

Common mistakes

Wrong

const data: number = JSON.parse(response);
console.log(data + 1);

JSON.parse can return anything; because you declared the type, TypeScript believes it, but it does NOT perform validation AT RUNTIME. If the server returns text, the code will silently fail. External data must always be validated.

Correct

const parsed: unknown = JSON.parse(response);
if (typeof parsed === "number") {
  console.log(parsed + 1);
}

Wrong

function getUser(id) {
  return db.find(id);
}

Since the parameter's type is not declared, it implicitly becomes any and TypeScript cannot perform any checks. In strict mode, the compiler throws the error "Parameter 'id' implicitly has an 'any' type" — disabling this warning defeats the purpose of using TypeScript.

Correct

function getUser(id: string) {
  return db.find(id);
}

Wrong

const el = document.getElementById("app") as HTMLDivElement;
el.innerHTML = "hello";

Forcing a type with as SILENCES the compiler, it does not validate the data. If the element is not on the page, getElementById returns null and you get a "Cannot set properties of null" error at runtime. Instead of forcing, you need to check.

Correct

const el = document.getElementById("app");
if (el) {
  el.innerHTML = "hello";
}

Frequently asked questions

Does TypeScript protect me at runtime?

No. Type information is erased during compilation; there is no type checking whatsoever in the generated JavaScript. Therefore, external sources such as API responses, form data, and file contents must still be validated separately — declaring types is not validation.

How do I migrate an existing JavaScript project?

Step by step. Change the file extensions and start with strict mode disabled, then add types and tighten settings file by file. Trying to convert everything all at once halts work; TypeScript was specifically designed for gradual migration.

When is it acceptable to use any?

Almost never. any completely turns off type checking and disables TypeScript. Use unknown where you truly don't know the type: unknown also accepts anything, but FORCES you to check before using it, which is the behavior you want.

Summary

TypeScript adds a type system to JavaScript that lives at compile time; there is no type control at runtime. External data must still be validated, strict mode should be enabled from the start, and unknown should be preferred over any.

HexLibraryHexLibrary

Learn programming more clearly. Every command is explained line by line with colors, reinforced with runnable exercises, test your code instantly — all in one place, free.

Learn

  • Home
  • Languages & Lessons
  • HexCode

Company

  • About
  • Contact

Legal

  • Privacy Policy
  • Terms of Use

Other Projects

  • HexToolz
  • Free Service Manuals
© 2026 HexLibrary · The clearest way to learn code