Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Lernen Basic Types in TypeScript | Section
TypeScript for Backend Development

bookBasic Types in TypeScript

Swipe um das Menü anzuzeigen

TypeScript allows you to define the type of data your variables will store. This helps make your code more predictable and easier to understand.

Primitive Types

The most common types are:

  • string: text values;
  • number: numeric values;
  • boolean: true or false.
let name: string = "John";
let age: number = 25;
let isActive: boolean = true;

Arrays

You can also define arrays with a specific type:

let numbers: number[] = [1, 2, 3];
let users: string[] = ["Alice", "Bob"];

This ensures that all elements in the array follow the same type.

Type Inference

In many cases, TypeScript can automatically detect the type based on the assigned value.

let city = "New York";

TypeScript understands that city is a string, even without explicitly defining the type.

Why Types Matter

Types help prevent mistakes.

let price: number = 100;
price = "cheap"; // Error

Without TypeScript, this kind of issue might only appear at runtime. With TypeScript, it is caught immediately.

question mark

Which of the following correctly defines an array of strings in TypeScript?

Wählen Sie die richtige Antwort aus

War alles klar?

Wie können wir es verbessern?

Danke für Ihr Feedback!

Abschnitt 1. Kapitel 3

Fragen Sie AI

expand

Fragen Sie AI

ChatGPT

Fragen Sie alles oder probieren Sie eine der vorgeschlagenen Fragen, um unser Gespräch zu beginnen

Abschnitt 1. Kapitel 3
some-alt