lezione 02

types e interface

interface, union e type narrowing.

livello: base durata: 22 min output: tipi solidi badge: typescript essentials

interface

interface Project { id: number; name: string; }

union types

type Id = string | number;

tipi base

let year: number = 2026;
let name: string = "Samir";

type narrowing

function isString(v: unknown): v is string {
  return typeof v === "string";
}

esercizi

definisci un type e mappa un array.

type Item = { id: number; name: string };
const items: Item[] = [{ id: 1, name: "A" }];
const names = items.map(i => i.name);
console.log(names);

panoramica

In questo capitolo su types e interface lavoriamo su interface, union types, tipi base, type narrowing per creare micro-pattern riutilizzabili.

lavora in sequenza: leggi, prova, modifica gli snippet e annota i trade-off principali.

badge: typescript essentials

obiettivi

  • capire e applicare interface in uno scenario reale
  • capire e applicare union types in uno scenario reale
  • capire e applicare tipi base in uno scenario reale
  • capire e applicare type narrowing in uno scenario reale

scheda rapida

interface Project { id: number; name: string; }

adatta questo scheletro agli esempi della lezione.

tips

  • attiva strict
  • evita any
  • usa type alias chiari
  • separa layer e dto
  • mantieni funzioni pure
  • mantieni interface leggibile

tip: documenta ogni scelta architetturale.

mini progetto

crea un array tipizzato e trasformalo.

  • definisci type
  • crea array
  • map

output atteso: output tipizzato.

checklist

  • ho eseguito gli snippet e controllato l'output
  • ho annotato almeno 2 trade-off
  • ho completato il mini progetto
  • ho salvato un riepilogo personale
start a brief