{"id":3248,"date":"2020-07-13T08:52:00","date_gmt":"2020-07-13T08:52:00","guid":{"rendered":"http:\/\/the-codest.localhost\/blog\/generics-in-typescript\/"},"modified":"2026-04-24T11:39:03","modified_gmt":"2026-04-24T11:39:03","slug":"generici-in-dattiloscritto","status":"publish","type":"post","link":"https:\/\/thecodest.co\/it\/blog\/generics-in-typescript\/","title":{"rendered":"Generici in TypeScript"},"content":{"rendered":"<p>I generici possono essere usati insieme alle funzioni (creando funzioni generiche), alle classi (creando classi generiche) e alle interfacce (creando interfacce generiche).<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Utilizzo di base<\/h2>\n\n\n\n<p>Probabilmente avete usato i generici in passato anche senza saperlo: l'uso pi\u00f9 comune dei generici \u00e8 la dichiarazione di un array:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"typescript\" class=\"language-typescript\">const myArray: stringa[];<\/code><\/pre>\n\n\n\n<p>A prima vista non \u00e8 nulla di speciale, ci limitiamo a dichiarare <code>myArray<\/code> come array di stringhe, ma \u00e8 uguale a una dichiarazione generica:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"typescript\" class=\"language-typescript\">const myArray: Array;<\/code><\/pre>\n\n\n\n<p><\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Mantenere le cose esplicite<\/h2>\n\n\n\n<p>Cominciamo con un esempio molto semplice: come potremmo portare questo prodotto vaniglia <a href=\"https:\/\/thecodest.co\/it\/blog\/hire-vue-js-developers\/\">JS<\/a> funzione per <a href=\"https:\/\/thecodest.co\/it\/dictionary\/typescript-developer\/\">TypeScript<\/a>:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"typescript\" class=\"language-typescript\">function getPrefiledArray(filler, length) {\n    return (new Array(length)).fill(filler);\n}<\/code><\/pre>\n\n\n\n<p>Questa funzione restituisce un array riempito con la quantit\u00e0 data di <code>riempimento<\/code>, quindi <code>lunghezza<\/code> sar\u00e0 <code>numero<\/code> e l'intera funzione restituir\u00e0 un array di <code>riempimento<\/code> - ma cos'\u00e8 il riempimento? A questo punto pu\u00f2 essere qualsiasi cosa, quindi un'opzione \u00e8 quella di utilizzare <code>qualsiasi<\/code>:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"typescript\" class=\"language-typescript\">function getPrefiledArray(filler: any, length: number): any[] {\n    return (new Array(length)).fill(filler);\n}<\/code><\/pre>\n\n\n\n<p>Utilizzo <code>qualsiasi<\/code> \u00e8 certamente generico - possiamo passare letteralmente qualsiasi cosa, quindi \"lavorare con un certo numero di tipi invece che con un singolo tipo\" dalla definizione \u00e8 pienamente coperto, ma perdiamo la connessione tra <code>riempimento<\/code> e il tipo di ritorno. In questo caso, vogliamo restituire una cosa comune e possiamo definire esplicitamente questa cosa comune come <strong>parametro tipo<\/strong>:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"typescript\" class=\"language-typescript\">function getPrefiledArray(filler: T, length: number): T[] {\n    return (new Array(length)).fill(filler);\n}<\/code><\/pre>\n\n\n\n<p>e utilizzare in questo modo:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"typescript\" class=\"language-typescript\">const prefilledArray = getPrefiledArray(0, 10);<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Vincoli generici<\/h2>\n\n\n\n<p>Esaminiamo casi diversi, probabilmente pi\u00f9 comuni. Perch\u00e9 usiamo i tipi nelle funzioni? Per me, \u00e8 per garantire che gli argomenti passati alla funzione abbiano alcune propriet\u00e0 con cui voglio interagire.<\/p>\n\n\n\n<p>Ancora una volta proviamo a portare una semplice funzione JS vanilla in TS.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"typescript\" class=\"language-typescript\">funzione getLength(thing) {\n    restituisce thing.length;\n}<\/code><\/pre>\n\n\n\n<p>Abbiamo un enigma non banale: come garantire che la cosa abbia un <code>lunghezza<\/code> e il primo pensiero potrebbe essere quello di fare qualcosa di simile:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"typescript\" class=\"language-typescript\">function getLength(thing: typeof Array):number {\n    return thing.length;\n}<\/code><\/pre>\n\n\n\n<p>e a seconda del contesto potrebbe essere corretto, nel complesso siamo un po' generici - funzioner\u00e0 con array di pi\u00f9 tipi, ma cosa succede se non sappiamo se l'oggetto deve essere sempre un array - forse l'oggetto \u00e8 un campo da calcio o una buccia di banana? In questo caso, dobbiamo raccogliere le propriet\u00e0 comuni di quell'oggetto in un costrutto che possa definire le propriet\u00e0 di un oggetto - un'interfaccia:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"typescript\" class=\"language-typescript\">interfaccia IThingWithLength {\n  lunghezza: numero;\n}<\/code><\/pre>\n\n\n\n<p>Possiamo utilizzare <code>IThingConLunghezza<\/code> come tipo dell'interfaccia <code>cosa<\/code> parametro:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"typescript\" class=\"language-typescript\">function getLength(thing: IThingWithLength):number {\n    restituisce thing.length;\n}<\/code><\/pre>\n\n\n\n<p>francamente in questo semplice esempio andr\u00e0 benissimo, ma se vogliamo mantenere questo tipo generico e non affrontare il problema del primo esempio possiamo usare <strong>Vincoli generici<\/strong>:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"typescript\" class=\"language-typescript\">function getLength(thing: T):number {\n    restituisce thing.length;\n}<\/code><\/pre>\n\n\n\n<p>e utilizzarlo:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"typescript\" class=\"language-typescript\">interfaccia IBananaPeel {\n  spessore: numero;\n  lunghezza: numero;\n}\n\nconst bananaPeel: IBananaPeel = {spessore: 0,2, lunghezza: 3,14};\ngetLength(bananaPeel);<\/code><\/pre>\n\n\n\n<p>Utilizzo <code>si estende<\/code> assicura che <code>T<\/code> conterr\u00e0 propriet\u00e0 definite da <code>IThingConLunghezza<\/code>.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Classi generiche<\/h2>\n\n\n\n<p>Fino a questo punto, abbiamo lavorato con funzioni generiche, ma non \u00e8 l'unico posto in cui i generici brillano: vediamo come incorporarli nelle classi.<\/p>\n\n\n\n<p>Prima di tutto, proviamo a conservare un mucchio di banane nel cestino delle banane:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"typescript\" class=\"language-typescript\">classe Banana {\n  costruttore(\n    public lunghezza: numero,\n    public colore: stringa,\n    public radiazione ionizzante: numero\n  ) {}\n}\n\nclass BananaBasket {\n  private banane: Banana[] = [];\n\n  add(banana: Banana): void {\n    this.bananas.push(banana);\n  }\n}\n\nconst bananaBasket = new BananaBasket();\nbananaBasket.add(new Banana(3.14, 'red', 10e-7));<\/code><\/pre>\n\n\n\n<p>Ora proviamo a creare un cestino generico, per diverse cose con lo stesso tipo:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"typescript\" class=\"language-typescript\">classe Cestino {\n  private stuff: T[] = [];\n\n  add(thing: T): void {\n    this.stuff.push(thing);\n  }\n}\n\nconst bananaBasket = new Basket();<\/code><\/pre>\n\n\n\n<p>Infine, supponiamo che il nostro cestino sia un contenitore di materiale radioattivo e che si possa immagazzinare solo materia che abbia <code>radiazioni ionizzanti<\/code> propriet\u00e0:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"typescript\" class=\"language-typescript\">interfaccia IRadioactive {\n  ionizingRadiation: numero;\n}\n\nclass RadioactiveContainer {\n  private stuff: T[] = [];\n\n  add(thing: T): void {\n    this.stuff.push(thing);\n  }\n}<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Interfaccia generica<\/h2>\n\n\n\n<p>Infine, cerchiamo di raccogliere tutte le nostre conoscenze e di costruire un impero radioattivo utilizzando anche le interfacce generiche:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"typescript\" class=\"language-typescript\">\/\/ Definire gli attributi comuni per i contenitori\ninterfaccia IRadioactive {\n  ionizingRadiation: number;\n}\n\n\n\/\/ Definisce qualcosa che \u00e8 radioattivo\ninterfaccia IBanana estende IRadioactive {\n  lunghezza: numero;\n  colore: stringa;\n}\n\n\/\/ Definisce qualcosa che non \u00e8 radioattivo\ninterfaccia IDog {\n  peso: numero;\n}\n\n\/\/ Definisce un'interfaccia per un contenitore che pu\u00f2 contenere solo materiale radioattivo\ninterface IRadioactiveContainer {\n  add(thing: T): void;\n  getRadioactiveness():number;\n}\n\n\/\/ Definire la classe che implementa l'interfaccia del contenitore radioattivo\nclass RadioactiveContainer implements IRadioactiveContainer {\n  private stuff: T[] = [];\n\n  add(thing: T): void {\n    this.stuff.push(thing);\n  }\n\n  getRadioactiveness(): number {\n      return this.stuff.reduce((a, b) =&gt; a + b.ionizingRadiation, 0)\n  }\n}\n\n\/\/ ERRORE! Il tipo 'IDog' non soddisfa il vincolo 'IRadioactive'.\n\/\/ Ed \u00e8 piuttosto brutale memorizzare i cani all'interno del contenitore radioattivo\nconst dogsContainer = new RadioactiveContainer();\n\n\/\/ Tutto bene!\nconst radioactiveContainer = new RadioactiveContainer();\n\n\/\/ Ricordarsi di differenziare i rifiuti radioattivi - creare un contenitore separato solo per le banane\nconst bananasContainer = new RadioactiveContainer();<\/code><\/pre>\n\n\n\n<p>Questo \u00e8 tutto, gente!<\/p>","protected":false},"excerpt":{"rendered":"<p>I generici forniscono pezzi di codice riutilizzabili che funzionano con una serie di tipi invece che con un singolo tipo. I generici forniscono un modo per trattare il tipo come una variabile e specificarlo al momento dell'uso, in modo simile ai parametri delle funzioni.<\/p>","protected":false},"author":2,"featured_media":3249,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"inline_featured_image":false,"footnotes":""},"categories":[8],"tags":[],"class_list":["post-3248","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-software-development"],"acf":[],"yoast_head":"<!-- This site is optimized with the Yoast SEO Premium plugin v27.3 (Yoast SEO v27.3) - https:\/\/yoast.com\/product\/yoast-seo-premium-wordpress\/ -->\n<title>Generics in TypeScript - The Codest<\/title>\n<meta name=\"description\" content=\"Generics provide reusable bits of code that work with a number of types instead of a single type. Generics provide a way to treat type as a variable and specify it on usage, similar to function parameters.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/thecodest.co\/it\/blog\/generici-in-dattiloscritto\/\" \/>\n<meta property=\"og:locale\" content=\"it_IT\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Generics in TypeScript\" \/>\n<meta property=\"og:description\" content=\"Generics provide reusable bits of code that work with a number of types instead of a single type. Generics provide a way to treat type as a variable and specify it on usage, similar to function parameters.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/thecodest.co\/it\/blog\/generici-in-dattiloscritto\/\" \/>\n<meta property=\"og:site_name\" content=\"The Codest\" \/>\n<meta property=\"article:published_time\" content=\"2020-07-13T08:52:00+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2026-04-24T11:39:03+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/thecodest.co\/app\/uploads\/2024\/05\/cover-image-75.jpg\" \/>\n\t<meta property=\"og:image:width\" content=\"1200\" \/>\n\t<meta property=\"og:image:height\" content=\"600\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/jpeg\" \/>\n<meta name=\"author\" content=\"thecodest\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"thecodest\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"3 minuti\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\\\/\\\/thecodest.co\\\/blog\\\/generics-in-typescript\\\/#article\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/thecodest.co\\\/blog\\\/generics-in-typescript\\\/\"},\"author\":{\"name\":\"thecodest\",\"@id\":\"https:\\\/\\\/thecodest.co\\\/#\\\/schema\\\/person\\\/7e3fe41dfa4f4e41a7baad4c6e0d4f76\"},\"headline\":\"Generics in TypeScript\",\"datePublished\":\"2020-07-13T08:52:00+00:00\",\"dateModified\":\"2026-04-24T11:39:03+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/thecodest.co\\\/blog\\\/generics-in-typescript\\\/\"},\"wordCount\":516,\"commentCount\":0,\"publisher\":{\"@id\":\"https:\\\/\\\/thecodest.co\\\/#organization\"},\"image\":{\"@id\":\"https:\\\/\\\/thecodest.co\\\/blog\\\/generics-in-typescript\\\/#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/thecodest.co\\\/app\\\/uploads\\\/2024\\\/05\\\/cover-image-75.jpg\",\"articleSection\":[\"Software Development\"],\"inLanguage\":\"it-IT\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\\\/\\\/thecodest.co\\\/blog\\\/generics-in-typescript\\\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/thecodest.co\\\/blog\\\/generics-in-typescript\\\/\",\"url\":\"https:\\\/\\\/thecodest.co\\\/blog\\\/generics-in-typescript\\\/\",\"name\":\"Generics in TypeScript - The Codest\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/thecodest.co\\\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\\\/\\\/thecodest.co\\\/blog\\\/generics-in-typescript\\\/#primaryimage\"},\"image\":{\"@id\":\"https:\\\/\\\/thecodest.co\\\/blog\\\/generics-in-typescript\\\/#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/thecodest.co\\\/app\\\/uploads\\\/2024\\\/05\\\/cover-image-75.jpg\",\"datePublished\":\"2020-07-13T08:52:00+00:00\",\"dateModified\":\"2026-04-24T11:39:03+00:00\",\"description\":\"Generics provide reusable bits of code that work with a number of types instead of a single type. Generics provide a way to treat type as a variable and specify it on usage, similar to function parameters.\",\"breadcrumb\":{\"@id\":\"https:\\\/\\\/thecodest.co\\\/blog\\\/generics-in-typescript\\\/#breadcrumb\"},\"inLanguage\":\"it-IT\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/thecodest.co\\\/blog\\\/generics-in-typescript\\\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"it-IT\",\"@id\":\"https:\\\/\\\/thecodest.co\\\/blog\\\/generics-in-typescript\\\/#primaryimage\",\"url\":\"https:\\\/\\\/thecodest.co\\\/app\\\/uploads\\\/2024\\\/05\\\/cover-image-75.jpg\",\"contentUrl\":\"https:\\\/\\\/thecodest.co\\\/app\\\/uploads\\\/2024\\\/05\\\/cover-image-75.jpg\",\"width\":1200,\"height\":600},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/thecodest.co\\\/blog\\\/generics-in-typescript\\\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\\\/\\\/thecodest.co\\\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Generics in TypeScript\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/thecodest.co\\\/#website\",\"url\":\"https:\\\/\\\/thecodest.co\\\/\",\"name\":\"The Codest\",\"description\":\"\",\"publisher\":{\"@id\":\"https:\\\/\\\/thecodest.co\\\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/thecodest.co\\\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"it-IT\"},{\"@type\":\"Organization\",\"@id\":\"https:\\\/\\\/thecodest.co\\\/#organization\",\"name\":\"The Codest\",\"url\":\"https:\\\/\\\/thecodest.co\\\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"it-IT\",\"@id\":\"https:\\\/\\\/thecodest.co\\\/#\\\/schema\\\/logo\\\/image\\\/\",\"url\":\"https:\\\/\\\/thecodest.co\\\/app\\\/uploads\\\/2024\\\/03\\\/thecodest-logo.svg\",\"contentUrl\":\"https:\\\/\\\/thecodest.co\\\/app\\\/uploads\\\/2024\\\/03\\\/thecodest-logo.svg\",\"width\":144,\"height\":36,\"caption\":\"The Codest\"},\"image\":{\"@id\":\"https:\\\/\\\/thecodest.co\\\/#\\\/schema\\\/logo\\\/image\\\/\"},\"sameAs\":[\"https:\\\/\\\/pl.linkedin.com\\\/company\\\/codest\",\"https:\\\/\\\/clutch.co\\\/profile\\\/codest\"]},{\"@type\":\"Person\",\"@id\":\"https:\\\/\\\/thecodest.co\\\/#\\\/schema\\\/person\\\/7e3fe41dfa4f4e41a7baad4c6e0d4f76\",\"name\":\"thecodest\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"it-IT\",\"@id\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/5dbfe6a1e8c86e432e8812759e34e6fe82ebac75119ae3237a6c1311fa19caf4?s=96&d=mm&r=g\",\"url\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/5dbfe6a1e8c86e432e8812759e34e6fe82ebac75119ae3237a6c1311fa19caf4?s=96&d=mm&r=g\",\"contentUrl\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/5dbfe6a1e8c86e432e8812759e34e6fe82ebac75119ae3237a6c1311fa19caf4?s=96&d=mm&r=g\",\"caption\":\"thecodest\"},\"url\":\"https:\\\/\\\/thecodest.co\\\/it\\\/author\\\/thecodest\\\/\"}]}<\/script>\n<!-- \/ Yoast SEO Premium plugin. -->","yoast_head_json":{"title":"Generici in TypeScript - The Codest","description":"I generici forniscono pezzi di codice riutilizzabili che funzionano con una serie di tipi invece che con un singolo tipo. I generici forniscono un modo per trattare il tipo come una variabile e specificarlo al momento dell'uso, in modo simile ai parametri delle funzioni.","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/thecodest.co\/it\/blog\/generici-in-dattiloscritto\/","og_locale":"it_IT","og_type":"article","og_title":"Generics in TypeScript","og_description":"Generics provide reusable bits of code that work with a number of types instead of a single type. Generics provide a way to treat type as a variable and specify it on usage, similar to function parameters.","og_url":"https:\/\/thecodest.co\/it\/blog\/generici-in-dattiloscritto\/","og_site_name":"The Codest","article_published_time":"2020-07-13T08:52:00+00:00","article_modified_time":"2026-04-24T11:39:03+00:00","og_image":[{"width":1200,"height":600,"url":"https:\/\/thecodest.co\/app\/uploads\/2024\/05\/cover-image-75.jpg","type":"image\/jpeg"}],"author":"thecodest","twitter_card":"summary_large_image","twitter_misc":{"Written by":"thecodest","Est. reading time":"3 minuti"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/thecodest.co\/blog\/generics-in-typescript\/#article","isPartOf":{"@id":"https:\/\/thecodest.co\/blog\/generics-in-typescript\/"},"author":{"name":"thecodest","@id":"https:\/\/thecodest.co\/#\/schema\/person\/7e3fe41dfa4f4e41a7baad4c6e0d4f76"},"headline":"Generics in TypeScript","datePublished":"2020-07-13T08:52:00+00:00","dateModified":"2026-04-24T11:39:03+00:00","mainEntityOfPage":{"@id":"https:\/\/thecodest.co\/blog\/generics-in-typescript\/"},"wordCount":516,"commentCount":0,"publisher":{"@id":"https:\/\/thecodest.co\/#organization"},"image":{"@id":"https:\/\/thecodest.co\/blog\/generics-in-typescript\/#primaryimage"},"thumbnailUrl":"https:\/\/thecodest.co\/app\/uploads\/2024\/05\/cover-image-75.jpg","articleSection":["Software Development"],"inLanguage":"it-IT","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/thecodest.co\/blog\/generics-in-typescript\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/thecodest.co\/blog\/generics-in-typescript\/","url":"https:\/\/thecodest.co\/blog\/generics-in-typescript\/","name":"Generici in TypeScript - The Codest","isPartOf":{"@id":"https:\/\/thecodest.co\/#website"},"primaryImageOfPage":{"@id":"https:\/\/thecodest.co\/blog\/generics-in-typescript\/#primaryimage"},"image":{"@id":"https:\/\/thecodest.co\/blog\/generics-in-typescript\/#primaryimage"},"thumbnailUrl":"https:\/\/thecodest.co\/app\/uploads\/2024\/05\/cover-image-75.jpg","datePublished":"2020-07-13T08:52:00+00:00","dateModified":"2026-04-24T11:39:03+00:00","description":"I generici forniscono pezzi di codice riutilizzabili che funzionano con una serie di tipi invece che con un singolo tipo. I generici forniscono un modo per trattare il tipo come una variabile e specificarlo al momento dell'uso, in modo simile ai parametri delle funzioni.","breadcrumb":{"@id":"https:\/\/thecodest.co\/blog\/generics-in-typescript\/#breadcrumb"},"inLanguage":"it-IT","potentialAction":[{"@type":"ReadAction","target":["https:\/\/thecodest.co\/blog\/generics-in-typescript\/"]}]},{"@type":"ImageObject","inLanguage":"it-IT","@id":"https:\/\/thecodest.co\/blog\/generics-in-typescript\/#primaryimage","url":"https:\/\/thecodest.co\/app\/uploads\/2024\/05\/cover-image-75.jpg","contentUrl":"https:\/\/thecodest.co\/app\/uploads\/2024\/05\/cover-image-75.jpg","width":1200,"height":600},{"@type":"BreadcrumbList","@id":"https:\/\/thecodest.co\/blog\/generics-in-typescript\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/thecodest.co\/"},{"@type":"ListItem","position":2,"name":"Generics in TypeScript"}]},{"@type":"WebSite","@id":"https:\/\/thecodest.co\/#website","url":"https:\/\/thecodest.co\/","name":"The Codest","description":"","publisher":{"@id":"https:\/\/thecodest.co\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/thecodest.co\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"it-IT"},{"@type":"Organization","@id":"https:\/\/thecodest.co\/#organization","name":"The Codest","url":"https:\/\/thecodest.co\/","logo":{"@type":"ImageObject","inLanguage":"it-IT","@id":"https:\/\/thecodest.co\/#\/schema\/logo\/image\/","url":"https:\/\/thecodest.co\/app\/uploads\/2024\/03\/thecodest-logo.svg","contentUrl":"https:\/\/thecodest.co\/app\/uploads\/2024\/03\/thecodest-logo.svg","width":144,"height":36,"caption":"The Codest"},"image":{"@id":"https:\/\/thecodest.co\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/pl.linkedin.com\/company\/codest","https:\/\/clutch.co\/profile\/codest"]},{"@type":"Person","@id":"https:\/\/thecodest.co\/#\/schema\/person\/7e3fe41dfa4f4e41a7baad4c6e0d4f76","name":"thecodest","image":{"@type":"ImageObject","inLanguage":"it-IT","@id":"https:\/\/secure.gravatar.com\/avatar\/5dbfe6a1e8c86e432e8812759e34e6fe82ebac75119ae3237a6c1311fa19caf4?s=96&d=mm&r=g","url":"https:\/\/secure.gravatar.com\/avatar\/5dbfe6a1e8c86e432e8812759e34e6fe82ebac75119ae3237a6c1311fa19caf4?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/5dbfe6a1e8c86e432e8812759e34e6fe82ebac75119ae3237a6c1311fa19caf4?s=96&d=mm&r=g","caption":"thecodest"},"url":"https:\/\/thecodest.co\/it\/author\/thecodest\/"}]}},"_links":{"self":[{"href":"https:\/\/thecodest.co\/it\/wp-json\/wp\/v2\/posts\/3248","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/thecodest.co\/it\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/thecodest.co\/it\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/thecodest.co\/it\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/thecodest.co\/it\/wp-json\/wp\/v2\/comments?post=3248"}],"version-history":[{"count":9,"href":"https:\/\/thecodest.co\/it\/wp-json\/wp\/v2\/posts\/3248\/revisions"}],"predecessor-version":[{"id":7822,"href":"https:\/\/thecodest.co\/it\/wp-json\/wp\/v2\/posts\/3248\/revisions\/7822"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/thecodest.co\/it\/wp-json\/wp\/v2\/media\/3249"}],"wp:attachment":[{"href":"https:\/\/thecodest.co\/it\/wp-json\/wp\/v2\/media?parent=3248"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/thecodest.co\/it\/wp-json\/wp\/v2\/categories?post=3248"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/thecodest.co\/it\/wp-json\/wp\/v2\/tags?post=3248"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}