{"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":"generiske-elementer-i-typescript","status":"publish","type":"post","link":"https:\/\/thecodest.co\/da\/blog\/generics-in-typescript\/","title":{"rendered":"Generiske l\u00e6gemidler i TypeScript"},"content":{"rendered":"<p>Generics kan bruges i forbindelse med funktioner (oprettelse af generiske funktioner), klasser (oprettelse af generiske klasser) og gr\u00e6nseflader (oprettelse af generiske gr\u00e6nseflader).<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Grundl\u00e6ggende brug<\/h2>\n\n\n\n<p>Du har sikkert brugt generics tidligere uden at vide det - den mest almindelige brug af generics er at erkl\u00e6re et array:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"typescript\" class=\"language-typescript\">const myArray: string[];<\/code><\/pre>\n\n\n\n<p>Det er ikke s\u00e5 specielt ved f\u00f8rste \u00f8jekast, vi erkl\u00e6rer bare <code>myArray<\/code> som et array af strenge, men det er det samme som en generisk erkl\u00e6ring:<\/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\">At holde tingene eksplicitte<\/h2>\n\n\n\n<p>Lad os starte med et meget simpelt eksempel - hvordan kan vi overf\u00f8re denne vanilje? <a href=\"https:\/\/thecodest.co\/da\/blog\/hire-vue-js-developers\/\">JS<\/a> funktion til <a href=\"https:\/\/thecodest.co\/da\/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>Denne funktion returnerer et array fyldt med den givne m\u00e6ngde <code>Fyldstof<\/code>s\u00e5 <code>l\u00e6ngde<\/code> vil v\u00e6re <code>nummer<\/code> og hele funktionen vil returnere et array af <code>Fyldstof<\/code> - Men hvad er fyldstof? P\u00e5 dette tidspunkt kan det v\u00e6re hvad som helst, s\u00e5 en mulighed er at bruge <code>nogen<\/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>Brug af <code>nogen<\/code> er bestemt generisk - vi kan sende bogstaveligt talt hvad som helst, s\u00e5 \"arbejde med et antal typer i stedet for en enkelt type\" fra definitionen er fuldt ud d\u00e6kket, men vi mister forbindelsen mellem <code>Fyldstof<\/code> typen og returtypen. I dette tilf\u00e6lde \u00f8nsker vi at returnere en f\u00e6lles ting, og vi kan eksplicit definere denne f\u00e6lles ting som <strong>type parameter<\/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>og bruge det s\u00e5dan her:<\/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\">Generiske begr\u00e6nsninger<\/h2>\n\n\n\n<p>Lad os se p\u00e5 andre og nok mere almindelige tilf\u00e6lde. Hvorfor bruger vi egentlig typer i funktioner? For mig er det for at sikre, at de argumenter, der sendes til funktionen, har nogle egenskaber, som jeg \u00f8nsker at interagere med.<\/p>\n\n\n\n<p>Lad os endnu en gang pr\u00f8ve at overf\u00f8re en simpel vanilje-JS-funktion til TS.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"typescript\" class=\"language-typescript\">funktion getLength(ting) {\n    return thing.length;\n}<\/code><\/pre>\n\n\n\n<p>Vi har en ikke-triviel g\u00e5de - hvordan sikrer vi, at tingen har en <code>l\u00e6ngde<\/code> ejendom, og den f\u00f8rste tanke er m\u00e5ske at g\u00f8re noget lignende:<\/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>og afh\u00e6ngigt af konteksten kan det v\u00e6re korrekt, generelt er vi lidt generiske - det fungerer med arrays af flere typer, men hvad nu hvis vi ikke rigtig ved, om tingen altid skal v\u00e6re en array - m\u00e5ske er tingen en fodboldbane eller en bananskr\u00e6l? I s\u00e5 fald er vi n\u00f8dt til at samle de f\u00e6lles egenskaber for den ting i en konstruktion, der kan definere egenskaber for et objekt - et interface:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"typescript\" class=\"language-typescript\">interface IThingWithLength {\n  l\u00e6ngde: antal;\n}<\/code><\/pre>\n\n\n\n<p>Vi kan bruge <code>ITingWithLength<\/code> interface som type af <code>ting<\/code> parameter:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"typescript\" class=\"language-typescript\">function getLength(thing: IThingWithLength):number {\n    return thing.length;\n}<\/code><\/pre>\n\n\n\n<p>I dette enkle eksempel er det helt fint, men hvis vi \u00f8nsker at holde denne type generisk og ikke st\u00e5 over for problemet fra det f\u00f8rste eksempel, kan vi bruge <strong>Generiske begr\u00e6nsninger<\/strong>:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"typescript\" class=\"language-typescript\">function getLength(thing: T):number {\n    return thing.length;\n}<\/code><\/pre>\n\n\n\n<p>og bruge det:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"typescript\" class=\"language-typescript\">interface IBananaPeel {\n  tykkelse: number;\n  l\u00e6ngde: nummer;\n}\n\nconst bananaPeel: IBananaPeel = {tykkelse: 0,2, l\u00e6ngde: 3,14};\ngetLength(bananaPeel);<\/code><\/pre>\n\n\n\n<p>Brug af <code>str\u00e6kker sig<\/code> sikrer, at <code>T<\/code> vil indeholde egenskaber, der er defineret af <code>ITingWithLength<\/code>.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Generiske klasser<\/h2>\n\n\n\n<p>Indtil nu har vi arbejdet med generiske funktioner, men det er ikke det eneste sted, hvor generiske funktioner brillerer, s\u00e5 lad os se, hvordan vi kan indarbejde dem i klasser.<\/p>\n\n\n\n<p>Lad os f\u00f8rst pr\u00f8ve at opbevare en masse bananer i banankurven:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"typescript\" class=\"language-typescript\">klasse Banan {\n  konstrukt\u00f8r(\n    offentlig l\u00e6ngde: tal,\n    offentlig farve: streng,\n    offentlig ioniserende str\u00e5ling: tal\n  ) {}\n}\n\nklasse BananaBasket {\n  private bananas: 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>Lad os nu pr\u00f8ve at skabe en generel kurv til forskellige ting med samme type:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"typescript\" class=\"language-typescript\">klasse Kurv {\n  private ting: T[] = [];\n\n  add(ting: T): void {\n    this.stuff.push(ting);\n  }\n}\n\nconst bananaBasket = new Basket();<\/code><\/pre>\n\n\n\n<p>Og lad os til sidst antage, at vores kurv er en beholder til radioaktivt materiale, og at vi kun kan opbevare materiale, der har <code>ioniserende str\u00e5ling<\/code> ejendom:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"typescript\" class=\"language-typescript\">interface IRadioactive {\n  ionizingRadiation: number;\n}\n\nclass RadioactiveContainer {\n  private stuff: T[] = [];\n\n  add(thing: T): void {\n    this.stuff.push(ting);\n  }\n}<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Generisk gr\u00e6nseflade<\/h2>\n\n\n\n<p>Lad os til sidst pr\u00f8ve at samle al vores viden og bygge et radioaktivt imperium, der ogs\u00e5 bruger generiske interfaces:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"typescript\" class=\"language-typescript\">\/\/ Definer f\u00e6lles attributter for containere\ninterface IRadioactive {\n  ionizingRadiation: number;\n}\n\n\n\/\/ Definer noget, der er radioaktivt\ninterface IBanana extends IRadioactive {\n  length: number;\n  farve: streng;\n}\n\n\/\/ Definer noget, der ikke er radioaktivt\ninterface IDog {\n  v\u00e6gt: nummer;\n}\n\n\/\/ Definer gr\u00e6nseflade for beholder, der kun kan indeholde radioaktive ting\ninterface IRadioactiveContainer {\n  add(thing: T): void;\n  getRadioactiveness():number;\n}\n\n\/\/ Definer klasse, der implementerer interface for radioaktiv beholder\nclass RadioactiveContainer implementerer IRadioactiveContainer {\n  private stuff: T[] = [];\n\n  add(thing: T): void {\n    this.stuff.push(ting);\n  }\n\n  getRadioactiveness(): number {\n      return this.stuff.reduce((a, b) =&gt; a + b.ionizingRadiation, 0)\n  }\n}\n\n\/\/ FEJL! Typen 'IDog' opfylder ikke betingelsen 'IRadioactive'\n\/\/ Og det er ret brutalt at opbevare hunde i en radioaktiv container\nconst dogsContainer = new RadioactiveContainer();\n\n\/\/ Alt er godt fam!\nconst radioactiveContainer = new RadioactiveContainer();\n\n\/\/ Husk at sortere dit radioaktive affald - lav en separat beholder kun til bananer\nconst bananasContainer = new RadioactiveContainer();<\/code><\/pre>\n\n\n\n<p>Det var alt, folkens!<\/p>","protected":false},"excerpt":{"rendered":"<p>Generics giver genanvendelige kodestykker, der fungerer med et antal typer i stedet for en enkelt type. Generics giver mulighed for at behandle typen som en variabel og specificere den ved brug, ligesom funktionsparametre.<\/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\/da\/blog\/generiske-elementer-i-typescript\/\" \/>\n<meta property=\"og:locale\" content=\"da_DK\" \/>\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\/da\/blog\/generiske-elementer-i-typescript\/\" \/>\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 minutter\" \/>\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\":\"da-DK\",\"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\":\"da-DK\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/thecodest.co\\\/blog\\\/generics-in-typescript\\\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"da-DK\",\"@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\":\"da-DK\"},{\"@type\":\"Organization\",\"@id\":\"https:\\\/\\\/thecodest.co\\\/#organization\",\"name\":\"The Codest\",\"url\":\"https:\\\/\\\/thecodest.co\\\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"da-DK\",\"@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\":\"da-DK\",\"@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\\\/da\\\/author\\\/thecodest\\\/\"}]}<\/script>\n<!-- \/ Yoast SEO Premium plugin. -->","yoast_head_json":{"title":"Generiske l\u00e6gemidler i TypeScript - The Codest","description":"Generics giver genanvendelige kodestykker, der fungerer med et antal typer i stedet for en enkelt type. Generics giver mulighed for at behandle typen som en variabel og specificere den ved brug, ligesom funktionsparametre.","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\/da\/blog\/generiske-elementer-i-typescript\/","og_locale":"da_DK","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\/da\/blog\/generiske-elementer-i-typescript\/","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 minutter"},"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":"da-DK","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":"Generiske l\u00e6gemidler i 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 giver genanvendelige kodestykker, der fungerer med et antal typer i stedet for en enkelt type. Generics giver mulighed for at behandle typen som en variabel og specificere den ved brug, ligesom funktionsparametre.","breadcrumb":{"@id":"https:\/\/thecodest.co\/blog\/generics-in-typescript\/#breadcrumb"},"inLanguage":"da-DK","potentialAction":[{"@type":"ReadAction","target":["https:\/\/thecodest.co\/blog\/generics-in-typescript\/"]}]},{"@type":"ImageObject","inLanguage":"da-DK","@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":"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":"da-DK"},{"@type":"Organization","@id":"https:\/\/thecodest.co\/#organization","name":"Codest","url":"https:\/\/thecodest.co\/","logo":{"@type":"ImageObject","inLanguage":"da-DK","@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":"da-DK","@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\/da\/author\/thecodest\/"}]}},"_links":{"self":[{"href":"https:\/\/thecodest.co\/da\/wp-json\/wp\/v2\/posts\/3248","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/thecodest.co\/da\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/thecodest.co\/da\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/thecodest.co\/da\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/thecodest.co\/da\/wp-json\/wp\/v2\/comments?post=3248"}],"version-history":[{"count":9,"href":"https:\/\/thecodest.co\/da\/wp-json\/wp\/v2\/posts\/3248\/revisions"}],"predecessor-version":[{"id":7822,"href":"https:\/\/thecodest.co\/da\/wp-json\/wp\/v2\/posts\/3248\/revisions\/7822"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/thecodest.co\/da\/wp-json\/wp\/v2\/media\/3249"}],"wp:attachment":[{"href":"https:\/\/thecodest.co\/da\/wp-json\/wp\/v2\/media?parent=3248"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/thecodest.co\/da\/wp-json\/wp\/v2\/categories?post=3248"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/thecodest.co\/da\/wp-json\/wp\/v2\/tags?post=3248"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}