Basic Types in TypeScript
Basic Types
~16 Min. Lesezeit Zuletzt aktualisiert am August 8, 2026
Now for the actual building blocks: the basic types TypeScript provides. We'll start modeling real data for our library – the first books.
The primitive types
let title: string = 'The Lord of the Rings';
let pageCount: number = 1216;
let isAvailable: boolean = true;
let rating: number = 4.5; // TypeScript has NO separate "float" type - it's all number
let note: string | null = null; // more on this in chapter 9 (union types)A small but important difference from many other languages: TypeScript has ONLY number for ALL numeric values – no separate int/float/double like Java or C#, because JavaScript itself doesn't make that distinction either.
Arrays
let genres: string[] = ['Fantasy', 'Adventure'];
let pageCounts: Array<number> = [1216, 423, 310]; // alternative syntax, identical meaning
genres.push('Classic'); // allowed - string fits
genres.push(42); // error - number doesn't fit in string[]string[] and Array<number> are two ways of writing THE SAME thing – the square-bracket syntax is more common in everyday use, the angle-bracket ("generic", more on this in chapter 17) syntax is sometimes more readable for more complex, nested array types.
Tuples: arrays with a fixed length and per-position types
let coordinate: [number, number] = [52.5, 13.4]; // latitude, longitude
let shelfPosition: [string, number] = ['Shelf A', 3]; // shelf name, slot number
coordinate = [13.4, 52.5, 100]; // error - the tuple allows EXACTLY two elementsA tuple is an array with a FIXED length, where EVERY position has its OWN type – unlike number[] (any number of numbers), [number, number] forces EXACTLY two numbers at FIXED positions.
any, unknown, never, void: the four special cases
any– turns off type checking ENTIRELY. TypeScript treats ananyvalue like plain JavaScript: anything is allowed, even obviously wrong operations. Should be used SPARINGLY – everyanyis a "blind spot" in an otherwise consistent type system.unknown– the SAFE counterpart toany: a value of unknown type that you're only allowed to actually use AFTER an explicit type check (see chapter 19, type narrowing).never– the type for values that NEVER occur (a function that always throws, or an infinite loop) – we'll encounter this again with exhaustiveness checks in chapter 19.void– the return type for functions that return NOTHING (only have side effects likeconsole.log) – covered in more detail in chapter 6.
let unsafeValue: any = 'text';
unsafeValue.foo.bar.baz(); // NO compile-time error, though guaranteed to crash at runtime
let externalData: unknown = JSON.parse('{}');
externalData.foo; // error - only allowed after a type check (see chapter 19)Achtung: any is tempting when you want a type error to "just go away" – resist that. any disables type checking not just for THIS value, but for EVERYTHING derived from it – a single any can silently "eat through" large parts of a project. Use unknown instead when the type is GENUINELY not yet known.
In practice: modeling the first book
Let's extend src/index.ts with our first, real library data:
let title: string = 'The Lord of the Rings';
let author: string = 'J.R.R. Tolkien';
let publicationYear: number = 1954;
let pageCount: number = 1216;
let isBorrowed: boolean = false;
let genres: string[] = ['Fantasy', 'Adventure'];
console.log(`${title} by ${author} (${publicationYear}), ${pageCount} pages`);
console.log(`Genres: ${genres.join(', ')}`);
console.log(`Borrowed: ${isBorrowed ? 'yes' : 'no'}`);Six separate variables for ONE book already feels unwieldy – in the next chapter we'll learn let vs. const in the TypeScript context, and starting in chapter 7 (interfaces) we'll combine these loose variables into a single, clean Book type.
Tipp: Run npm start after EVERY code change in this tutorial to see the actual behavior – TypeScript errors stop execution before anything even runs, so a successful program run is also a confirmation that the current code is type-correct.