Learning
TypeScript

Objektumok és interface

Objektumtípusok: inline típusok, interface, opcionális/readonly mezők, extends, index signature.

Objektumok és interface

Inline objektumtípus

function printUser(user: { name: string; age: number }): void {
  console.log(`${user.name}, ${user.age} éves`)
}

Interface

interface User {
  id: number
  name: string
  email: string
}

const user: User = {
  id: 1,
  name: "Adam",
  email: "adam@example.com",
}

Opcionális és readonly mezők

interface Product {
  id: number
  name: string
  description?: string
  readonly sku: string
}

Interface kiterjesztése

interface Animal {
  name: string
}

interface Dog extends Animal {
  breed: string
}

Nested objektumok

interface Address {
  street: string
  city: string
  country: string
}

interface UserWithAddress {
  id: number
  name: string
  address: Address
}

Index signature

interface StringMap {
  [key: string]: string
}

const translations: StringMap = {
  hello: "szia",
  goodbye: "viszlát",
}

On this page