{"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":"almenn-forritun-i-typescript","status":"publish","type":"post","link":"https:\/\/thecodest.co\/is\/blog\/generics-in-typescript\/","title":{"rendered":"\u00d3merkt lyf \u00ed TypeScript"},"content":{"rendered":"<p>Almenn ger\u00f0ir m\u00e1 nota \u00ed tengslum vi\u00f0 fall (til a\u00f0 b\u00faa til almennt fall), flokka (til a\u00f0 b\u00faa til almennan flokk) og vi\u00f0m\u00f3t (til a\u00f0 b\u00faa til almennt vi\u00f0m\u00f3t).<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Grunnnotkun<\/h2>\n\n\n\n<p>\u00de\u00fa hefur l\u00edklega nota\u00f0 generics \u00e1\u00f0ur \u00e1n \u00feess a\u00f0 vita af \u00fev\u00ed \u2013 algengasta notkun generics er a\u00f0 l\u00fdsa fylki:<\/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>\u00dea\u00f0 er ekki mj\u00f6g s\u00e9rstakt vi\u00f0 fyrstu s\u00fdn, vi\u00f0 erum bara a\u00f0 l\u00fdsa. <code>m\u00ednara\u00f0ir<\/code> sem fylki strengja, en \u00fea\u00f0 er \u00fea\u00f0 sama og almenn yfirl\u00fdsing:<\/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\">Halda hlutunum sk\u00fdrum<\/h2>\n\n\n\n<p>Skulum byrja \u00e1 mj\u00f6g einf\u00f6ldu d\u00e6mi \u2013 hvernig g\u00e6tum vi\u00f0 flutt \u00feetta vanilla <a href=\"https:\/\/thecodest.co\/is\/blog\/hire-vue-js-developers\/\">JS<\/a> fall til <a href=\"https:\/\/thecodest.co\/is\/dictionary\/typescript-developer\/\">TypeScript<\/a>:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"typescript\" class=\"language-typescript\">getPrefiledArray(filler, length) {\n    return (new Array(length)).fill(filler);\n}<\/code><\/pre>\n\n\n\n<p>\u00deessi fall mun skila fylki fylltu me\u00f0 gefnu magni af <code>fylliefni<\/code>, svo <code>lengd<\/code> mun ver\u00f0a <code>tala<\/code> og heilli falli\u00f0 mun skila fylki af <code>fylliefni<\/code> \u2013 en hva\u00f0 er fylliefni? \u00c1 \u00feessum t\u00edmapunkti getur \u00fea\u00f0 veri\u00f0 hva\u00f0 sem er, svo einn kostur er a\u00f0 nota <code>hvert sem er<\/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>A\u00f0 nota <code>hvert sem er<\/code> er vissulega almenn \u2013 vi\u00f0 getum bori\u00f0 b\u00f3kstaflega hva\u00f0 sem er, svo \u201ca\u00f0 vinna me\u00f0 m\u00f6rgum ger\u00f0um \u00ed sta\u00f0 einnar ger\u00f0ar\u201d \u00far skilgreiningunni er fullkomlega teki\u00f0 me\u00f0, en vi\u00f0 missum tengslin milli <code>fylliefni<\/code> Ger\u00f0in og skilarger\u00f0in. \u00cd \u00feessu tilfelli viljum vi\u00f0 skila einhverju sameiginlegu og getum vi\u00f0 sk\u00fdrt skilgreint \u00feetta sameiginlega sem <strong>Ger\u00f0artilkynning<\/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 nota svona:<\/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\">Almenn takm\u00f6rk<\/h2>\n\n\n\n<p>Sko\u00f0um mismunandi, l\u00edklega algengari tilvik. Af hverju notum vi\u00f0 raunverulega ger\u00f0ir \u00ed fallum? Fyrir mig er \u00fea\u00f0 til a\u00f0 tryggja a\u00f0 \u00feau r\u00f6k sem send eru \u00ed falli\u00f0 hafi \u00e1kve\u00f0na eiginleika sem \u00e9g vil vinna me\u00f0.<\/p>\n\n\n\n<p>Aftur skulum vi\u00f0 reyna a\u00f0 flytja einfalt vanilla JS-fall yfir \u00ed TS.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"typescript\" class=\"language-typescript\">getLength(hlutur) {\n    skila lengd hlutursins;\n}<\/code><\/pre>\n\n\n\n<p>Vi\u00f0 st\u00f6ndum frammi fyrir verulegu vandi \u2013 hvernig tryggjum vi\u00f0 a\u00f0 hluturinn hafi <code>lengd<\/code> eign, og fyrsta hugmyndin g\u00e6ti veri\u00f0 a\u00f0 gera eitthva\u00f0 eins og:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"typescript\" class=\"language-typescript\">getLength(thing: typeof Array):number {\n    return thing.length;\n}<\/code><\/pre>\n\n\n\n<p>og allt eftir samhengi g\u00e6ti \u00fea\u00f0 veri\u00f0 r\u00e9tt, en almennt erum vi\u00f0 d\u00e1l\u00edti\u00f0 almenn \u2013 \u00fea\u00f0 virkar me\u00f0 fylkjum af m\u00f6rgum ger\u00f0um, en hva\u00f0 ef vi\u00f0 vitum ekki alveg hvort hluturinn eigi alltaf a\u00f0 vera fylki \u2013 kannski er hluturinn f\u00f3tboltav\u00f6llur e\u00f0a bananah\u00fd\u00f0i? \u00cd \u00feessu tilfelli ver\u00f0um vi\u00f0 a\u00f0 safna sameiginlegum eiginleikum hlutarins \u00ed uppbyggingu sem getur skilgreint eiginleika hlutskiptis \u2013 vi\u00f0m\u00f3t:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"typescript\" class=\"language-typescript\">interface IThingWithLength {\n  length: number;\n}<\/code><\/pre>\n\n\n\n<p>Vi\u00f0 getum nota\u00f0 <code>\u00c9g-hlutur-me\u00f0-lengd<\/code> vi\u00f0m\u00f3t sem tegund af the <code>hlutur<\/code> breyti:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"typescript\" class=\"language-typescript\">getLength(thing: IThingWithLength):number {\n    return thing.length;\n}<\/code><\/pre>\n\n\n\n<p>Alveg hreint sagt \u00ed \u00feessu einf\u00f6ldu d\u00e6mi ver\u00f0ur \u00feetta alveg \u00ed lagi, en ef vi\u00f0 viljum halda \u00feessari tegund almennri og for\u00f0ast vandam\u00e1li\u00f0 \u00far fyrsta d\u00e6minu getum vi\u00f0 nota\u00f0 <strong>Almenn takm\u00f6rk<\/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 nota \u00fea\u00f0:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"typescript\" class=\"language-typescript\">interface IBananaPeel {\n  thickness: number;\n  length: number;\n}\n\nconst bananaPeel: IBananaPeel = {thickness: 0.2, length: 3.14};\ngetLength(bananaPeel);<\/code><\/pre>\n\n\n\n<p>A\u00f0 nota <code>\u00fatv\u00edkkar<\/code> tryggir a\u00f0 <code>T<\/code> mun innihalda eiginleika sem eru skilgreindir af <code>\u00c9g-hlutur-me\u00f0-lengd<\/code>.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Almennar flokkar<\/h2>\n\n\n\n<p>Hinga\u00f0 til h\u00f6fum vi\u00f0 unni\u00f0 me\u00f0 gener\u00edskum fallum, en \u00feetta er ekki einasta svi\u00f0i\u00f0 \u00fear sem gener\u00edkar skara fram \u00far. Sko\u00f0um hvernig vi\u00f0 getum innleitt \u00fe\u00e6r \u00ed bekki.<\/p>\n\n\n\n<p>Fyrst skulum vi\u00f0 reyna a\u00f0 geyma knippi af banana \u00ed bananak\u00f6rfunni:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"typescript\" class=\"language-typescript\">class Banana {\n  constructor(\n    public length: number,\n    public color: string,\n    public ionizingRadiation: number\n  ) {}\n}\n\nclass 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, \u2018rau\u00f0ur\u2019, 10e-7));<\/code><\/pre>\n\n\n\n<p>N\u00fa skulum vi\u00f0 reyna a\u00f0 b\u00faa til alhli\u00f0a k\u00f6rfu fyrir mismunandi hluti af sama tagi:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"typescript\" class=\"language-typescript\">class Basket {\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>Og a\u00f0 lokum skulum vi\u00f0 gera r\u00e1\u00f0 fyrir a\u00f0 k\u00f6rfan okkar s\u00e9 geislavirk efni\u00edl\u00e1t og vi\u00f0 getum a\u00f0eins geymt efni sem hefur <code>j\u00f3nandi geislun<\/code> Eign:<\/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(thing);\n  }\n}<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Almenn vi\u00f0m\u00f3tsa\u00f0ger\u00f0<\/h2>\n\n\n\n<p>A\u00f0 lokum skulum vi\u00f0 reyna a\u00f0 safna allri okkar \u00feekkingu og byggja upp geislavirk heimsveldi me\u00f0 \u00fev\u00ed a\u00f0 nota Generic Interfaces:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"typescript\" class=\"language-typescript\">\/\/ Skilgreina sameiginlega eiginleika fyrir \u00edl\u00e1t\ninterface IRadioactive {\n  ionizingRadiation: number;\n}\n\n\n\/\/ Skilgreina eitthva\u00f0 sem er geislavirk\ninterface IBanana extends IRadioactive {\n  length: number;\n  color: string;\n}\n\n\/\/ Skilgreina eitthva\u00f0 sem er ekki geislavirk\ninterface IDog {\n  weight: number;\n}\n\n\/\/ Skilgreina vi\u00f0m\u00f3t fyrir \u00edl\u00e1t sem getur a\u00f0eins geymt geislavirk efni\ninterface IRadioactiveContainer {\n  add(thing: T): void;\n  getRadioactiveness():number;\n}\n\n\/\/ Skilgreina bekk sem innlei\u00f0ir vi\u00f0m\u00f3t fyrir geislavirk \u00edl\u00e1t\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\/\/ ERROR! Type 'IDog' does not satisfy the constraint 'IRadioactive'\n\/\/ Og \u00fea\u00f0 er frekar grimmilegt a\u00f0 geyma hunda \u00ed geislavirkum \u00edl\u00e1ti\nconst dogsContainer = new RadioactiveContainer();\n\n\/\/ Allt gott, fj\u00f6lskylda!\nconst radioactiveContainer = new RadioactiveContainer();\n\n\/\/ Mundu a\u00f0 flokka geislavirk \u00fargang - b\u00fa\u00f0u til s\u00e9rstakt tunnu fyrir bananar einir\nconst bananasContainer = new RadioactiveContainer();<\/code><\/pre>\n\n\n\n<p>\u00deetta er allt, f\u00f3lk!<\/p>","protected":false},"excerpt":{"rendered":"<p>Generics bj\u00f3\u00f0a upp \u00e1 endurn\u00fdtanlega k\u00f3\u00f0ahluta sem virka me\u00f0 m\u00f6rgum tegundum \u00ed sta\u00f0 einnar tegundar. Generics bj\u00f3\u00f0a upp \u00e1 lei\u00f0 til a\u00f0 me\u00f0h\u00f6ndla tegund sem breytu og tilgreina hana vi\u00f0 notkun, l\u00edkt og stiklu\u00f0u breytur \u00ed falli.<\/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=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/thecodest.co\/is\/blogg\/almenn-forritun-i-typescript\/\" \/>\n<meta property=\"og:locale\" content=\"is_IS\" \/>\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\/is\/blogg\/almenn-forritun-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 minutes\" \/>\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\":\"is\",\"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\",\"breadcrumb\":{\"@id\":\"https:\\\/\\\/thecodest.co\\\/blog\\\/generics-in-typescript\\\/#breadcrumb\"},\"inLanguage\":\"is\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/thecodest.co\\\/blog\\\/generics-in-typescript\\\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"is\",\"@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\":\"is\"},{\"@type\":\"Organization\",\"@id\":\"https:\\\/\\\/thecodest.co\\\/#organization\",\"name\":\"The Codest\",\"url\":\"https:\\\/\\\/thecodest.co\\\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"is\",\"@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\":\"is\",\"@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\\\/is\\\/author\\\/thecodest\\\/\"}]}<\/script>\n<!-- \/ Yoast SEO Premium plugin. -->","yoast_head_json":{"title":"\u00d3merkt lyf \u00ed TypeScript - The Codest","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\/is\/blogg\/almenn-forritun-i-typescript\/","og_locale":"is_IS","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\/is\/blogg\/almenn-forritun-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 minutes"},"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":"is","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":"\u00d3merkt lyf \u00ed 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","breadcrumb":{"@id":"https:\/\/thecodest.co\/blog\/generics-in-typescript\/#breadcrumb"},"inLanguage":"is","potentialAction":[{"@type":"ReadAction","target":["https:\/\/thecodest.co\/blog\/generics-in-typescript\/"]}]},{"@type":"ImageObject","inLanguage":"is","@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":"is"},{"@type":"Organization","@id":"https:\/\/thecodest.co\/#organization","name":"The Codest","url":"https:\/\/thecodest.co\/","logo":{"@type":"ImageObject","inLanguage":"is","@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":"is","@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\/is\/author\/thecodest\/"}]}},"_links":{"self":[{"href":"https:\/\/thecodest.co\/is\/wp-json\/wp\/v2\/posts\/3248","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/thecodest.co\/is\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/thecodest.co\/is\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/thecodest.co\/is\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/thecodest.co\/is\/wp-json\/wp\/v2\/comments?post=3248"}],"version-history":[{"count":9,"href":"https:\/\/thecodest.co\/is\/wp-json\/wp\/v2\/posts\/3248\/revisions"}],"predecessor-version":[{"id":7822,"href":"https:\/\/thecodest.co\/is\/wp-json\/wp\/v2\/posts\/3248\/revisions\/7822"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/thecodest.co\/is\/wp-json\/wp\/v2\/media\/3249"}],"wp:attachment":[{"href":"https:\/\/thecodest.co\/is\/wp-json\/wp\/v2\/media?parent=3248"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/thecodest.co\/is\/wp-json\/wp\/v2\/categories?post=3248"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/thecodest.co\/is\/wp-json\/wp\/v2\/tags?post=3248"}],"curies":[{"name":"vp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}