LearnAI ToolsCareerPractice BuildsPlayContact
Lesson 416 min read

Basic Types

Learn TypeScript's primitive types, arrays, tuples, the any and unknown types, void/null/undefined/never, and how type inference works.

Introduction

With TypeScript installed, it's time to learn the building blocks of its type system. This lesson covers the basic types you'll use constantly: primitives like string and number, arrays, tuples, the special any and unknown types, and the trio of "absence" types — void, null, undefined, and never.

What You Will Learn
  • TypeScript's primitive types and how to annotate variables with them.
  • How to type arrays and fixed-length tuples.
  • The difference between the dangerous any type and the safer unknown type.
  • What void, null, undefined, and never each mean and when they appear.
  • How TypeScript's type inference reduces how much you need to annotate by hand.

Primitive Types

TypeScript's primitive types mirror JavaScript's own primitives exactly — it does not invent new ones. A type annotation is written as a colon followed by the type name, right after a variable, parameter, or property name.

let username: string = "amol";
let age: number = 29;
let isActive: boolean = true;
let bigNumber: bigint = 100n;
let uniqueKey: symbol = Symbol("id");
TypeExampleNotes
string'hello', `template`Text values — single quotes, double quotes, and template literals all produce string.
number42, 3.14, -7All numbers share one type — there is no separate int or float type.
booleantrue, falseLogical values only.
bigint100nArbitrary-precision integers (ES2020+), written with a trailing n.
symbolSymbol('id')Unique, immutable identifiers, rarely needed in everyday code.

Arrays

Arrays are typed by writing the element type followed by square brackets, or equivalently using the generic `Array<T>` form. Both mean exactly the same thing; the bracket form is more common in everyday code.

let scores: number[] = [90, 85, 77];
let names: Array<string> = ["Amol", "Priya", "Dev"];

Tuples

A tuple is a fixed-length array where the type of each position is known in advance. Unlike a plain array, TypeScript enforces both the length and the exact type at every index.

let point: [number, number] = [10, 20];
let entry: [string, number] = ["age", 29];
// entry = [29, "age"]; // Error: types are in the wrong order

The any and unknown Types

`any` tells TypeScript to stop checking a value entirely — you can call any method, access any property, and assign it to anything, with zero compile-time safety. It exists mainly as an escape hatch for gradually migrating JavaScript code, and should be avoided in new code.

`unknown` is the type-safe counterpart. A value typed `unknown` could be anything, just like `any`, but TypeScript will not let you use it until you narrow it down to a more specific type first — for example with a `typeof` check.

function processValue(value: unknown) {
if (typeof value === "string") {
console.log(value.toUpperCase()); // OK — narrowed to string
}
}

void, null, undefined, and never

`void` describes a function that does not return a meaningful value — it is used almost exclusively as a function return type. `null` and `undefined` represent the deliberate and accidental absence of a value respectively, and TypeScript's `strictNullChecks` option (part of `strict` mode) prevents you from using a possibly-null value without checking it first. `never` describes something that can never happen — a function that always throws, an infinite loop, or an unreachable code branch.

Type Inference

You don't need to annotate every single variable. TypeScript infers a type automatically from a variable's initial value, so `let count = 5;` is already understood to be `number` without writing `: number` at all. Explicit annotations matter most at function boundaries — parameters and return types — where TypeScript has no initial value to infer from.

Common Mistakes

Avoid These Mistakes
  • Reaching for `any` as a shortcut whenever the compiler complains, instead of fixing the underlying type issue.
  • Confusing tuples with regular arrays — a tuple's length and per-position types are fixed, a plain array's are not.
  • Assuming `unknown` behaves like `any` — it does not; you must narrow an `unknown` value before using it.
  • Forgetting that with `strictNullChecks` enabled, `null` and `undefined` are not automatically assignable to other types.

Best Practices

  • Prefer `unknown` over `any` whenever a value's type genuinely isn't known ahead of time.
  • Let inference handle simple local variables; add explicit annotations at function parameters and exported function return types.
  • Use tuples when a fixed, ordered set of values is the actual shape of your data (like [x, y] coordinates).
  • Always enable `strict` mode so null/undefined handling is checked consistently across your codebase.

Frequently Asked Questions

No — TypeScript has a single number type for all numeric values, matching JavaScript, which stores all numbers as double-precision floats (with bigint available separately for large integers).

Rarely — mainly as a temporary measure while migrating existing JavaScript code to TypeScript. unknown is almost always the safer choice for genuinely unknown values.

Functions that always throw an error, functions with infinite loops, and exhaustiveness checks that ensure every case of a union type has been handled.

No — like all TypeScript types, a tuple is a compile-time-only concept. At runtime, a tuple is simply a regular JavaScript array.

Key Takeaways

  • TypeScript's primitives (string, number, boolean, bigint, symbol) mirror JavaScript's own primitives exactly.
  • Arrays are typed as `T[]` or `Array<T>`; tuples are fixed-length arrays with a known type per position.
  • `any` disables type checking entirely; `unknown` is the type-safe alternative that requires narrowing before use.
  • `void`, `null`, `undefined`, and `never` each describe a different flavor of "no meaningful value."
  • Type inference means you rarely need to annotate simple local variables by hand.

Summary

You now know the fundamental building blocks of TypeScript's type system. Next, you'll learn how to describe the shape of whole objects using interfaces — one of the most commonly used features in everyday TypeScript code.

Next Lesson →

Interfaces