Skip to content

TypeScript - Types

TypeScript’s core strength lies in its static type system. This system allows you to explicitly define the types of variables, function parameters, and return values. The TypeScript compiler then uses these type annotations to check your code for errors before it runs, catching potential issues like assigning a string to a number variable or calling a method that doesn’t exist on an object.

Using types improves code reliability, makes refactoring safer, enhances code readability and documentation, and enables powerful tooling like autocompletion and intelligent error reporting in code editors.

These are the fundamental building blocks for data:

TypeKeywordDescriptionExample
NumbernumberRepresents both integer and floating-point numbers (double-precision 64-bit).let age: number = 30; let price: number = 99.95;
StringstringRepresents sequences of characters (text). Enclosed in single quotes ('), double quotes ("), or backticks (`).let name: string = "Alice"; let message: string = Hello, ${name};
BooleanbooleanRepresents logical values: true or false.let isActive: boolean = true; let hasFinished: boolean = false;
NullnullRepresents the intentional absence of any object value. Has only one value: null.let data: string | null = null; (Often used in unions)
UndefinedundefinedRepresents the value automatically given to uninitialized variables. Has only one value: undefined.let notAssigned: number | undefined; (Often used implicitly or in unions)
Symbolsymbol(ES6+) Represents unique, immutable identifiers, often used as keys for object properties.const id: symbol = Symbol('uniqueId');
BigIntbigint(ES2020+) Represents whole numbers larger than the maximum safe integer representable by the number type.const largeNumber: bigint = 9007199254740991n; (Note the n suffix)

Note on null and undefined: By default, null and undefined are subtypes of all other types, meaning you could assign them to, say, a string variable. However, enabling the strictNullChecks compiler option (highly recommended) treats null and undefined as distinct types, forcing you to explicitly handle potential null/undefined values, often using union types (e.g., string | null).

TypeKeywordDescription
AnyanyThe dynamic type escape hatch. Using any opts out of type checking for that variable. Avoid using any whenever possible, as it defeats the purpose of TypeScript. Prefer more specific types or unknown.
UnknownunknownA type-safe counterpart to any. Represents a value whose type is not known. You must perform type checks (narrowing) or use type assertion before performing operations on an unknown value.
VoidvoidPrimarily used as the return type for functions that do not return a value.
NeverneverRepresents the type of values that never occur. Used for functions that always throw an error or never return (e.g., infinite loops). Also appears in type narrowing when no possibilities remain.
ObjectobjectRepresents any non-primitive type (i.e., anything that is not number, string, boolean, symbol, null, undefined, or bigint). Generally less useful than specific object types defined with interfaces or type aliases.

Beyond primitives, TypeScript allows you to define complex structures:

  • Arrays: Ordered lists of values of a single type. Syntax: type[] or Array<type>. (e.g., number[], Array<string>)
  • Tuples: Fixed-size arrays where the type of each element is known. Syntax: [type1, type2, ...]. (e.g., [string, number])
  • Enums: A way to define a set of named constants. Syntax: enum Color { Red, Green, Blue }.
  • Interfaces: Define the shape or contract of an object. Syntax: interface Person { name: string; age: number; }.
  • Type Aliases: Create custom names for types (primitive, union, intersection, object, etc.). Syntax: type UserID = string | number;.
  • Classes: Blueprints for creating objects with properties and methods.
  • Functions: Have their own types based on parameters and return values.

These more complex types are covered in detail in their respective sections. Understanding the basic types is the first step towards leveraging TypeScript’s powerful type system.