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›Basic Types

Basic Types

number, string, boolean, array types, and why any is dangerous.

Simple enough for a 5-year-old to understand

Three basic types

Example code
1
let number: number = 42;
number — integer/floating-point number.
Variablenumber — integer/floating-point number.
2
let text: string = "hello";
string — text.
Variablestring — text.
3
let dogru: boolean = true;
boolean — true/false.
Variableboolean — true/false.
4
console.log(number, text, dogru);
42 hello true
Output / Input42 hello true
VariableOutput / Input

🎯 Exercise & Practice

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

1.Define an array of type number with the value [10, 20, 30] and print its length.

🎯 Expected output: 3

Loading editor…

About this topic

TypeScript's basic types mirror JavaScript's runtime types: string, number, boolean, null, undefined, symbol, bigint. On top of these, TypeScript adds its own types: any, unknown, never, and void.

In most cases, you do NOT need to write the type. TypeScript infers it from the right side, and this inference is usually more accurate than what you would write yourself. Type annotation is needed where inference falls short — function parameters, empty arrays, external data.

Three special type distinctions are worth knowing: any turns off type checking, unknown forces checking, never can take no values. The difference between unknown and any is the backbone of using TypeScript correctly.

What you will learn

  • Basic types and type inference
  • The critical difference between any and unknown
  • The void and never types
  • Type narrowing with typeof
  • Checking null and undefined
  • Why type assertion is risky

When to use?

Type narrowing will be the feature of TypeScript you use the most: when a value can be multiple types, you prove which one it is using typeof or a property check, and TypeScript knows the correct type inside that block.

With strictNullChecks enabled, null and undefined are separate types and you cannot use them without checking. This setting turns JavaScript's most common runtime error into a compile error.

Common mistakes

Wrong

let value: any = "text";
value.toFixed(2);

any allows anything; calling a number method on a string is valid at compile time but throws a "value.toFixed is not a function" error at runtime. Using any means turning off TypeScript.

Correct

let value: unknown = "text";
if (typeof value === "number") {
  value.toFixed(2);
}

Wrong

const items = [];
items.push("a");

For an empty array, TypeScript cannot infer anything and its type becomes an array of never; adding an element throws an "Argument of type 'string' is not assignable to parameter of type 'never'" error. The type of empty arrays must be explicitly written.

Correct

const items: string[] = [];
items.push("a");

Wrong

function find(id: string): string {
  const user = users[id];
  return user.name;
}

Reading from an array or object by index can return undefined, but TypeScript does not assume this by default. With noUncheckedIndexedAccess enabled, it throws an error; when disabled, the code crashes at runtime with "Cannot read properties of undefined".

Correct

function find(id: string): string {
  const user = users[id];
  if (!user) return "";
  return user.name;
}

Frequently asked questions

What is the difference between any and unknown?

Both accept any value, but you can DO anything on any, while you can do nothing on unknown — you must prove its type first. This enforcement is exactly what you want; always use unknown for external data.

Should I write the type everywhere?

No. Write them for function parameters and return types — these are contracts and need to be documented. Rely on inference for local variables; unnecessary type writing lengthens code and makes refactoring harder.

What is the difference between void and never?

void means "returns no value" — the function finishes normally but has no result. never means "never returns": the function either throws an error or enters an infinite loop. never is also used to mark impossible states and catch missing cases.

Summary

TypeScript infers most types; writing them is needed for parameters, return types, and empty arrays. any turns off checking, unknown forces checking — unknown should always be used for external data.

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