Skip to content

Basic Types in TypeScript

The primitive types in TypeScript: string, number, boolean, array, tuple, enum, and more.

What you'll learn

  • The primitive types: string, number, and boolean
  • How to type arrays and tuples
  • What the special types any, unknown, and void do
  • When to use never and object types

Concept

Primitive Types

TypeScript has three main primitive types that mirror JavaScript's primitives: string for text, number for all numeric values (integers and floats are both number), and boolean for true/false. You annotate a variable with a colon followed by the type name: let count: number = 10. These are the building blocks of every TypeScript program.

Arrays and Tuples

An array type is written as Type[] or Array<Type> — for example, number[] is an array of numbers. A tuple is a fixed-length array where each position has a specific type: [string, number] describes a pair where the first element is a string and the second is a number. Tuples are useful for things like [statusCode, message] pairs or CSV row data.

Special Types

Beyond the primitives, TypeScript has several special types. any disables type checking entirely — avoid it. unknown is a type-safe alternative to any: it accepts any value but forces you to check the type before using it. void means a function returns nothing. never represents a value that never occurs (like a function that always throws). object is the type for any non-primitive value.

Example

              // Primitives
let name: string = "Alice";
let score: number = 95.5;
let passed: boolean = true;

// Arrays
let scores: number[] = [90, 85, 92];
let names: Array<string> = ["Alice", "Bob"];

// Tuple — fixed length, typed positions
let response: [number, string] = [200, "OK"];

// Special types
let data: unknown = JSON.parse('{"x":1}');
if (typeof data === "object" && data !== null) {
  console.log("Got an object");
}

function logMessage(msg: string): void {
  console.log(msg);
}
            

Primitives cover most variables. Arrays use Type[] syntax. Tuples fix the length and per-position type. unknown is the safe alternative to any.

Try it

  • JSON to TypeScript

    Generate typed interfaces from JSON to see basic types in use.

  • JSON Formatter

    Format JSON data and understand the shapes TypeScript describes.

Common mistakes

The mistake

Using any to silence type errors

The fix

Use unknown instead. It forces you to verify the type before use, keeping type safety while accepting arbitrary data.

The mistake

Confusing number with separate int and float types

The fix

TypeScript has only one number type. Both 42 and 3.14 are number. There is no int or float distinction.

Practice Basic Types in TypeScript

5 exercises

Practice Basic Types in TypeScript

Look up unfamiliar terms

A beginner-friendly glossary explains jargon in plain English — variables, functions, DOM, Promise, and more.

View glossary

Build real projects

Apply what you learned by building guided projects with starter code and solutions.

View projects

Decode error messages

Plain-English explanations of common errors — what they mean, why they happen, and how to fix them.

View errors