{"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":"generika-v-typovem-skriptu","status":"publish","type":"post","link":"https:\/\/thecodest.co\/cs\/blog\/generics-in-typescript\/","title":{"rendered":"Generika v TypeScript"},"content":{"rendered":"<p>Generika lze pou\u017e\u00edt ve spojen\u00ed s funkcemi (vytvo\u0159en\u00ed generick\u00e9 funkce), t\u0159\u00eddami (vytvo\u0159en\u00ed generick\u00e9 t\u0159\u00eddy) a rozhran\u00edmi (vytvo\u0159en\u00ed generick\u00e9ho rozhran\u00ed).<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Z\u00e1kladn\u00ed pou\u017eit\u00ed<\/h2>\n\n\n\n<p>Pravd\u011bpodobn\u011b jste v minulosti pou\u017eili generika, ani\u017e byste o tom v\u011bd\u011bli - nej\u010dast\u011bj\u0161\u00edm pou\u017eit\u00edm generik je deklarace pole:<\/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>Na prvn\u00ed pohled to nen\u00ed nic zvl\u00e1\u0161tn\u00edho, jen prohla\u0161ujeme. <code>myArray<\/code> jako pole \u0159et\u011bzc\u016f, ale je to stejn\u00e9 jako obecn\u00e1 deklarace:<\/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\">Zachov\u00e1n\u00ed explicitnosti<\/h2>\n\n\n\n<p>Za\u010dn\u011bme velmi jednoduch\u00fdm p\u0159\u00edkladem - jak bychom mohli p\u0159en\u00e9st tuto vanilkovou verzi. <a href=\"https:\/\/thecodest.co\/cs\/blog\/hire-vue-js-developers\/\">JS<\/a> funkce na <a href=\"https:\/\/thecodest.co\/cs\/dictionary\/typescript-developer\/\">TypeScript<\/a>:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"typescript\" class=\"language-typescript\">funkce getPrefiledArray(filler, length) {\n    return (new Array(length)).fill(filler);\n}<\/code><\/pre>\n\n\n\n<p>Tato funkce vr\u00e1t\u00ed pole napln\u011bn\u00e9 dan\u00fdm mno\u017estv\u00edm <code>v\u00fdpl\u0148<\/code>, tak\u017ee <code>d\u00e9lka<\/code> bude <code>\u010d\u00edslo<\/code> a cel\u00e1 funkce vr\u00e1t\u00ed pole <code>v\u00fdpl\u0148<\/code> - ale co je to v\u00fdpl\u0148? V tuto chv\u00edli to m\u016f\u017ee b\u00fdt cokoli, tak\u017ee jednou z mo\u017enost\u00ed je pou\u017e\u00edt <code>jak\u00fdkoli<\/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>Pou\u017e\u00edv\u00e1n\u00ed str\u00e1nek <code>jak\u00fdkoli<\/code> je jist\u011b obecn\u00fd - m\u016f\u017eeme mu p\u0159edat doslova cokoli, tak\u017ee \"pr\u00e1ce s v\u00edce typy m\u00edsto s jedn\u00edm typem\" z definice je pln\u011b pokryta, ale ztr\u00e1c\u00edme vazbu mezi <code>v\u00fdpl\u0148<\/code> a n\u00e1vratov\u00fd typ. V tomto p\u0159\u00edpad\u011b chceme vr\u00e1tit n\u011bjakou spole\u010dnou v\u011bc a tuto spole\u010dnou v\u011bc m\u016f\u017eeme explicitn\u011b definovat jako <strong>parametr typu<\/strong>:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"typescript\" class=\"language-typescript\">funkce getPrefiledArray(filler: T, length: number): T[] {\n    return (new Array(length)).fill(filler);\n}<\/code><\/pre>\n\n\n\n<p>a pou\u017eijte je takto:<\/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\">Obecn\u00e1 omezen\u00ed<\/h2>\n\n\n\n<p>Pod\u00edvejme se na jin\u00e9, pravd\u011bpodobn\u011b \u010dast\u011bj\u0161\u00ed p\u0159\u00edpady. Pro\u010d vlastn\u011b pou\u017e\u00edv\u00e1me typy ve funkc\u00edch? Pro m\u011b je to proto, abych zajistil, \u017ee argumenty p\u0159ed\u00e1van\u00e9 funkci budou m\u00edt n\u011bjak\u00e9 vlastnosti, se kter\u00fdmi chci interagovat.<\/p>\n\n\n\n<p>Je\u0161t\u011b jednou se pokus\u00edme p\u0159en\u00e9st jednoduchou funkci vanilla JS do TS.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"typescript\" class=\"language-typescript\">funkce getLength(v\u011bc) {\n    return thing.length;\n}<\/code><\/pre>\n\n\n\n<p>M\u00e1me netrivi\u00e1ln\u00ed h\u00e1danku - jak zajistit, aby v\u011bc m\u011bla. <code>d\u00e9lka<\/code> a prvn\u00ed my\u0161lenkou m\u016f\u017ee b\u00fdt n\u011bco jako:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"typescript\" class=\"language-typescript\">funkce getLength(thing: typeof Array):number {\n    return thing.length;\n}<\/code><\/pre>\n\n\n\n<p>a v z\u00e1vislosti na kontextu to m\u016f\u017ee b\u00fdt spr\u00e1vn\u011b, celkov\u011b jsme trochu obecn\u00ed - bude to fungovat s poli v\u00edce typ\u016f, ale co kdy\u017e opravdu nev\u00edme, jestli m\u00e1 b\u00fdt v\u011bc v\u017edy pole - t\u0159eba je v\u011bc fotbalov\u00e9 h\u0159i\u0161t\u011b nebo slupka od ban\u00e1nu? V takov\u00e9m p\u0159\u00edpad\u011b mus\u00edme shrom\u00e1\u017edit spole\u010dn\u00e9 vlastnosti dan\u00e9 v\u011bci do konstrukce, kter\u00e1 m\u016f\u017ee definovat vlastnosti objektu - rozhran\u00ed:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"typescript\" class=\"language-typescript\">rozhran\u00ed IThingWithLength {\n  length: \u010d\u00edslo;\n}<\/code><\/pre>\n\n\n\n<p>M\u016f\u017eeme pou\u017e\u00edt <code>IThingWithLength<\/code> jako typ rozhran\u00ed <code>v\u011bc<\/code> parametr:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"typescript\" class=\"language-typescript\">funkce getLength(thing: IThingWithLength):number {\n    return thing.length;\n}<\/code><\/pre>\n\n\n\n<p>up\u0159\u00edmn\u011b \u0159e\u010deno, v tomto jednoduch\u00e9m p\u0159\u00edkladu to bude naprosto v po\u0159\u00e1dku, ale pokud chceme, aby tento typ byl obecn\u00fd a ne\u0159e\u0161il se probl\u00e9m z prvn\u00edho p\u0159\u00edkladu, m\u016f\u017eeme pou\u017e\u00edt typ <strong>Obecn\u00e1 omezen\u00ed<\/strong>:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"typescript\" class=\"language-typescript\">funkce getLength(thing: T):number {\n    return thing.length;\n}<\/code><\/pre>\n\n\n\n<p>a pou\u017e\u00edvat ji:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"typescript\" class=\"language-typescript\">rozhran\u00ed IBananaPeel {\n  thickness: \u010d\u00edslo;\n  d\u00e9lka: \u010d\u00edslo;\n}\n\nconst bananaPeel: IBananaPeel = {thickness: 0.2, length: 3.14};\ngetLength(bananaPeel);<\/code><\/pre>\n\n\n\n<p>Pou\u017e\u00edv\u00e1n\u00ed str\u00e1nek <code>roz\u0161i\u0159uje<\/code> zaji\u0161\u0165uje, \u017ee <code>T<\/code> bude obsahovat vlastnosti, kter\u00e9 jsou definov\u00e1ny pomoc\u00ed <code>IThingWithLength<\/code>.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Obecn\u00e9 t\u0159\u00eddy<\/h2>\n\n\n\n<p>A\u017e do t\u00e9to chv\u00edle jsme pracovali s generick\u00fdmi funkcemi, ale to nen\u00ed jedin\u00e9 m\u00edsto, kde generika z\u00e1\u0159\u00ed, pod\u00edvejme se, jak je m\u016f\u017eeme za\u010dlenit do t\u0159\u00edd.<\/p>\n\n\n\n<p>Nejd\u0159\u00edve si vyzkou\u0161ejte ulo\u017eit trs ban\u00e1n\u016f do ko\u0161\u00edku na ban\u00e1ny:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"typescript\" class=\"language-typescript\">t\u0159\u00edda Banana {\n  constructor(\n    public length: \u010d\u00edslo,\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, 'red', 10e-7));<\/code><\/pre>\n\n\n\n<p>Nyn\u00ed se pokus\u00edme vytvo\u0159it ko\u0161 pro v\u0161eobecn\u00e9 pou\u017eit\u00ed, pro r\u016fzn\u00e9 v\u011bci stejn\u00e9ho typu:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"typescript\" class=\"language-typescript\">t\u0159\u00edda Basket {\n  private stuff: T[] = [];\n\n  add(v\u011bc: T): void {\n    this.stuff.push(thing);\n  }\n}\n\nconst bananaBasket = new Basket();<\/code><\/pre>\n\n\n\n<p>A kone\u010dn\u011b, p\u0159edpokl\u00e1dejme, \u017ee n\u00e1\u0161 ko\u0161 je kontejnerem na radioaktivn\u00ed materi\u00e1l a \u017ee do n\u011bj m\u016f\u017eeme ukl\u00e1dat pouze hmotu, kter\u00e1 m\u00e1 <code>ionizuj\u00edc\u00ed z\u00e1\u0159en\u00ed<\/code> nemovitosti:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"typescript\" class=\"language-typescript\">rozhran\u00ed 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\">Obecn\u00e9 rozhran\u00ed<\/h2>\n\n\n\n<p>Nakonec se pokus\u00edme shrom\u00e1\u017edit v\u0161echny na\u0161e znalosti a vybudovat radioaktivn\u00ed \u0159\u00ed\u0161i tak\u00e9 pomoc\u00ed generick\u00fdch rozhran\u00ed:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"typescript\" class=\"language-typescript\">\/\/ Definice spole\u010dn\u00fdch atribut\u016f pro kontejnery\ninterface IRadioactive {\n  ionizingRadiation: number;\n}\n\n\n\/\/ Definujte n\u011bco, co je radioaktivn\u00ed\ninterface IBanana extends IRadioactive {\n  length: number;\n  color: string;\n}\n\n\/\/ Definujte n\u011bco, co nen\u00ed radioaktivn\u00ed\nrozhran\u00ed IDog {\n  weight: number;\n}\n\n\/\/ Definujte rozhran\u00ed pro kontejner, kter\u00fd m\u016f\u017ee obsahovat pouze radioaktivn\u00ed v\u011bci.\ninterface IRadioactiveContainer {\n  add(thing: T): void;\n  getRadioactiveness():number;\n}\n\n\/\/ Definujte t\u0159\u00eddu implementuj\u00edc\u00ed rozhran\u00ed radioaktivn\u00edho kontejneru\nt\u0159\u00edda 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\/\/ CHYBA! Typ 'IDog' nespl\u0148uje omezen\u00ed 'IRadioactive'\n\/\/ A je docela brut\u00e1ln\u00ed ukl\u00e1dat psy uvnit\u0159 radioaktivn\u00edho kontejneru\nconst dogsContainer = new RadioactiveContainer();\n\n\/\/ V\u0161echno v po\u0159\u00e1dku, rodino!\nconst radioactiveContainer = new RadioactiveContainer();\n\n\/\/ Nezapome\u0148te t\u0159\u00eddit radioaktivn\u00ed odpad - vytvo\u0159te samostatn\u00fd ko\u0161 pouze pro ban\u00e1ny.\nconst bananasContainer = new RadioactiveContainer();<\/code><\/pre>\n\n\n\n<p>To je v\u0161e, p\u0159\u00e1tel\u00e9!<\/p>","protected":false},"excerpt":{"rendered":"<p>Generika poskytuj\u00ed opakovan\u011b pou\u017eiteln\u00e9 \u010d\u00e1sti k\u00f3du, kter\u00e9 pracuj\u00ed s \u0159adou typ\u016f nam\u00edsto jednoho typu. Generika poskytuj\u00ed zp\u016fsob, jak zach\u00e1zet s typem jako s prom\u011bnnou a specifikovat jej p\u0159i pou\u017eit\u00ed, podobn\u011b jako parametry funkc\u00ed.<\/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\/cs\/blog\/generika-v-typovem-skriptu\/\" \/>\n<meta property=\"og:locale\" content=\"cs_CZ\" \/>\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\/cs\/blog\/generika-v-typovem-skriptu\/\" \/>\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 minuty\" \/>\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\":\"cs\",\"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\":\"cs\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/thecodest.co\\\/blog\\\/generics-in-typescript\\\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"cs\",\"@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\":\"cs\"},{\"@type\":\"Organization\",\"@id\":\"https:\\\/\\\/thecodest.co\\\/#organization\",\"name\":\"The Codest\",\"url\":\"https:\\\/\\\/thecodest.co\\\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"cs\",\"@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\":\"cs\",\"@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\\\/cs\\\/author\\\/thecodest\\\/\"}]}<\/script>\n<!-- \/ Yoast SEO Premium plugin. -->","yoast_head_json":{"title":"Generick\u00e9 p\u0159\u00edpravky v TypeScript - The Codest","description":"Generika poskytuj\u00ed opakovan\u011b pou\u017eiteln\u00e9 \u010d\u00e1sti k\u00f3du, kter\u00e9 pracuj\u00ed s \u0159adou typ\u016f nam\u00edsto jednoho typu. Generika poskytuj\u00ed zp\u016fsob, jak zach\u00e1zet s typem jako s prom\u011bnnou a specifikovat jej p\u0159i pou\u017eit\u00ed, podobn\u011b jako parametry funkc\u00ed.","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\/cs\/blog\/generika-v-typovem-skriptu\/","og_locale":"cs_CZ","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\/cs\/blog\/generika-v-typovem-skriptu\/","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 minuty"},"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":"cs","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":"Generick\u00e9 p\u0159\u00edpravky v 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":"Generika poskytuj\u00ed opakovan\u011b pou\u017eiteln\u00e9 \u010d\u00e1sti k\u00f3du, kter\u00e9 pracuj\u00ed s \u0159adou typ\u016f nam\u00edsto jednoho typu. Generika poskytuj\u00ed zp\u016fsob, jak zach\u00e1zet s typem jako s prom\u011bnnou a specifikovat jej p\u0159i pou\u017eit\u00ed, podobn\u011b jako parametry funkc\u00ed.","breadcrumb":{"@id":"https:\/\/thecodest.co\/blog\/generics-in-typescript\/#breadcrumb"},"inLanguage":"cs","potentialAction":[{"@type":"ReadAction","target":["https:\/\/thecodest.co\/blog\/generics-in-typescript\/"]}]},{"@type":"ImageObject","inLanguage":"cs","@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":"cs"},{"@type":"Organization","@id":"https:\/\/thecodest.co\/#organization","name":"The Codest","url":"https:\/\/thecodest.co\/","logo":{"@type":"ImageObject","inLanguage":"cs","@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":"cs","@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\/cs\/author\/thecodest\/"}]}},"_links":{"self":[{"href":"https:\/\/thecodest.co\/cs\/wp-json\/wp\/v2\/posts\/3248","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/thecodest.co\/cs\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/thecodest.co\/cs\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/thecodest.co\/cs\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/thecodest.co\/cs\/wp-json\/wp\/v2\/comments?post=3248"}],"version-history":[{"count":9,"href":"https:\/\/thecodest.co\/cs\/wp-json\/wp\/v2\/posts\/3248\/revisions"}],"predecessor-version":[{"id":7822,"href":"https:\/\/thecodest.co\/cs\/wp-json\/wp\/v2\/posts\/3248\/revisions\/7822"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/thecodest.co\/cs\/wp-json\/wp\/v2\/media\/3249"}],"wp:attachment":[{"href":"https:\/\/thecodest.co\/cs\/wp-json\/wp\/v2\/media?parent=3248"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/thecodest.co\/cs\/wp-json\/wp\/v2\/categories?post=3248"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/thecodest.co\/cs\/wp-json\/wp\/v2\/tags?post=3248"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}