{"componentChunkName":"component---src-templates-handbook-tsx","path":"/docs/handbook/generics.html","result":{"data":{"allSitePage":{"nodes":[{"path":"/dev-404-page/"},{"path":"/docs/handbook/advanced-types.html"},{"path":"/docs/handbook/basic-types.html"},{"path":"/docs/handbook/classes.html"},{"path":"/docs/handbook/compiler-options-in-msbuild.html"},{"path":"/docs/handbook/compiler-options.html"},{"path":"/docs/handbook/configuring-watch.html"},{"path":"/docs/handbook/declaration-merging.html"},{"path":"/docs/handbook/decorators.html"},{"path":"/docs/handbook/enums.html"},{"path":"/docs/handbook/functions.html"},{"path":"/docs/handbook/generics.html"},{"path":"/docs/handbook/integrating-with-build-tools.html"},{"path":"/docs/handbook/interfaces.html"},{"path":"/docs/handbook/iterators-and-generators.html"},{"path":"/docs/handbook/jsdoc-supported-types.html"},{"path":"/docs/handbook/jsx.html"},{"path":"/docs/handbook/literal-types.html"},{"path":"/docs/handbook/mixins.html"},{"path":"/docs/handbook/module-resolution.html"},{"path":"/docs/handbook/modules.html"},{"path":"/docs/handbook/namespaces-and-modules.html"},{"path":"/docs/handbook/namespaces.html"},{"path":"/docs/handbook/nightly-builds.html"},{"path":"/docs/handbook/project-references.html"},{"path":"/docs/handbook/symbols.html"},{"path":"/docs/handbook/"},{"path":"/docs/handbook/triple-slash-directives.html"},{"path":"/docs/handbook/type-checking-javascript-files.html"},{"path":"/docs/handbook/type-compatibility.html"},{"path":"/docs/handbook/type-inference.html"},{"path":"/docs/handbook/unions-and-intersections.html"},{"path":"/docs/handbook/utility-types.html"},{"path":"/docs/handbook/variable-declarations.html"},{"path":"/docs/handbook/writing-declaration-files.html"},{"path":"/docs/handbook/declaration-files/by-example.html"},{"path":"/docs/handbook/declaration-files/consumption.html"},{"path":"/docs/handbook/declaration-files/deep-dive.html"},{"path":"/docs/handbook/declaration-files/do-s-and-don-ts.html"},{"path":"/docs/handbook/declaration-files/introduction.html"},{"path":"/docs/handbook/declaration-files/library-structures.html"},{"path":"/docs/handbook/declaration-files/publishing.html"},{"path":"/docs/handbook/declaration-files/templates.html"},{"path":"/docs/handbook/release-notes/typescript-1-1.html"},{"path":"/docs/handbook/release-notes/typescript-1-3.html"},{"path":"/docs/handbook/release-notes/typescript-1-4.html"},{"path":"/docs/handbook/release-notes/typescript-1-5.html"},{"path":"/docs/handbook/release-notes/typescript-1-6.html"},{"path":"/docs/handbook/release-notes/typescript-1-7.html"},{"path":"/docs/handbook/release-notes/typescript-1-8.html"},{"path":"/docs/handbook/release-notes/typescript-2-0.html"},{"path":"/docs/handbook/release-notes/typescript-2-2.html"},{"path":"/docs/handbook/release-notes/typescript-2-1.html"},{"path":"/docs/handbook/release-notes/typescript-2-3.html"},{"path":"/docs/handbook/release-notes/typescript-2-4.html"},{"path":"/docs/handbook/release-notes/typescript-2-5.html"},{"path":"/docs/handbook/release-notes/typescript-2-6.html"},{"path":"/docs/handbook/release-notes/typescript-2-7.html"},{"path":"/docs/handbook/release-notes/typescript-2-8.html"},{"path":"/docs/handbook/release-notes/typescript-2-9.html"},{"path":"/docs/handbook/release-notes/typescript-3-0.html"},{"path":"/docs/handbook/release-notes/typescript-3-1.html"},{"path":"/docs/handbook/release-notes/typescript-3-2.html"},{"path":"/docs/handbook/release-notes/typescript-3-3.html"},{"path":"/docs/handbook/release-notes/typescript-3-4.html"},{"path":"/docs/handbook/release-notes/typescript-3-5.html"},{"path":"/docs/handbook/release-notes/typescript-3-6.html"},{"path":"/docs/handbook/release-notes/typescript-3-7.html"},{"path":"/docs/handbook/release-notes/typescript-3-8.html"},{"path":"/docs/handbook/asp-net-core.html"},{"path":"/docs/handbook/angular.html"},{"path":"/docs/handbook/dom-manipulation.html"},{"path":"/docs/handbook/gulp.html"},{"path":"/docs/handbook/migrating-from-javascript.html"},{"path":"/docs/handbook/react-&-webpack.html"},{"path":"/docs/handbook/react.html"},{"path":"/docs/handbook/typescript-in-5-minutes-func.html"},{"path":"/docs/handbook/typescript-in-5-minutes.html"},{"path":"/docs/handbook/typescript-in-5-minutes-oop.html"},{"path":"/docs/handbook/typescript-from-scratch.html"},{"path":"/docs/handbook/typescript-tooling-in-5-minutes.html"},{"path":"/docs/handbook/tsconfig-json.html"},{"path":"/docs/handbook/declaration-files/templates/global-modifying-module-d-ts.html"},{"path":"/docs/handbook/declaration-files/templates/global-plugin-d-ts.html"},{"path":"/docs/handbook/declaration-files/templates/global-d-ts.html"},{"path":"/docs/handbook/declaration-files/templates/module-class-d-ts.html"},{"path":"/docs/handbook/declaration-files/templates/module-function-d-ts.html"},{"path":"/docs/handbook/declaration-files/templates/module-plugin-d-ts.html"},{"path":"/docs/handbook/declaration-files/templates/module-d-ts.html"},{"path":"/docs/handbook/release-notes/overview.html"},{"path":"/tsconfig"},{"path":"/en/tsconfig"},{"path":"/ja/tsconfig"},{"path":"/vo/tsconfig"},{"path":"/play"},{"path":"/en/play"},{"path":"/es/play"},{"path":"/vo/play"},{"path":"/zh/play"},{"path":"/ja/play"},{"path":"/play/3-7/fixits/big-number-literals.ts"},{"path":"/play/3-7/fixits/const-to-let.ts"},{"path":"/play/3-7/fixits/infer-from-usage-changes.ts"},{"path":"/play/3-7/syntax-and-messaging/flattened-error-reporting.ts"},{"path":"/play/3-7/syntax-and-messaging/nullish-coalescing.ts"},{"path":"/play/3-7/syntax-and-messaging/optional-chaining.ts"},{"path":"/play/3-7/types-and-code-flow/assertion-functions.ts"},{"path":"/play/3-7/types-and-code-flow/recursive-type-references.ts"},{"path":"/play/3-7/types-and-code-flow/uncalled-function-checks.ts"},{"path":"/play/3-8/breaking-changes/checking-unions-with-index-signatures.ts"},{"path":"/play/3-8/jsdoc-improvements/accessibility-modifiers.js"},{"path":"/play/3-8/syntax-and-messaging/export-modules-from.ts"},{"path":"/play/3-8/syntax-and-messaging/private-class-fields.ts"},{"path":"/play/javascript/external-apis/typescript-with-deno.ts"},{"path":"/play/javascript/external-apis/typescript-with-node.js"},{"path":"/play/javascript/external-apis/typescript-with-web.js"},{"path":"/play/javascript/external-apis/typescript-with-webgl.js"},{"path":"/play/javascript/functions-with-javascript/function-chaining.ts"},{"path":"/play/javascript/functions-with-javascript/generic-functions.ts"},{"path":"/play/javascript/functions-with-javascript/typing-functions.ts"},{"path":"/play/javascript/helping-with-javascript/errors.ts"},{"path":"/play/javascript/helping-with-javascript/quick-fixes.ts"},{"path":"/play/javascript/javascript-essentials/code-flow.ts"},{"path":"/play/javascript/javascript-essentials/functions.ts"},{"path":"/play/javascript/javascript-essentials/hello-world.ts"},{"path":"/play/javascript/javascript-essentials/objects-and-arrays.ts"},{"path":"/play/javascript/modern-javascript/async-await.ts"},{"path":"/play/javascript/modern-javascript/immutability.ts"},{"path":"/play/javascript/modern-javascript/import-export.ts"},{"path":"/play/javascript/modern-javascript/jsdoc-support.js"},{"path":"/play/javascript/working-with-classes/classes-101.ts"},{"path":"/play/javascript/working-with-classes/generic-classes.ts"},{"path":"/play/javascript/working-with-classes/mixins.ts"},{"path":"/play/javascript/working-with-classes/this.ts"},{"path":"/play/playground/config/javascript-playgrounds.js"},{"path":"/play/playground/config/new-compiler-defaults.ts"},{"path":"/play/playground/language/automatic-type-acquisition.ts"},{"path":"/play/playground/language/fixits.ts"},{"path":"/play/playground/tooling/mobile-support.ts"},{"path":"/play/playground/tooling/sharable-urls.ts"},{"path":"/play/playground/tooling/typescript-versions.ts"},{"path":"/play/typescript/language/soundness.ts"},{"path":"/play/typescript/language/structural-typing.ts"},{"path":"/play/typescript/language/type-guards.ts"},{"path":"/play/typescript/language/type-widening-and-narrowing.ts"},{"path":"/play/typescript/language-extensions/enums.ts"},{"path":"/play/typescript/language-extensions/nominal-typing.ts"},{"path":"/play/typescript/language-extensions/types-vs-interfaces.ts"},{"path":"/play/typescript/meta-types/conditional-types.ts"},{"path":"/play/typescript/meta-types/discriminate-types.ts"},{"path":"/play/typescript/meta-types/indexed-types.ts"},{"path":"/play/typescript/meta-types/mapped-types.ts"},{"path":"/play/typescript/primitives/any.ts"},{"path":"/play/typescript/primitives/literals.ts"},{"path":"/play/typescript/primitives/union-and-intersection-types.ts"},{"path":"/play/typescript/primitives/unknown-and-never.ts"},{"path":"/play/typescript/type-primitives/built-in-utility-types.ts"},{"path":"/play/typescript/type-primitives/nullable-types.ts"},{"path":"/play/typescript/type-primitives/tuples.ts"},{"path":"/es/play/javascript/external-apis/typescript-with-deno.ts"},{"path":"/es/play/javascript/external-apis/typescript-with-node.js"},{"path":"/es/play/javascript/external-apis/typescript-with-web.js"},{"path":"/es/play/javascript/functions-with-javascript/function-chaining.ts"},{"path":"/es/play/javascript/functions-with-javascript/generic-functions.ts"},{"path":"/es/play/javascript/javascript-essentials/hello-world.ts"},{"path":"/es/play/javascript/javascript-essentials/objects-and-arrays.ts"},{"path":"/es/play/typescript/type-primitives/built-in-utility-types.ts"},{"path":"/es/play/typescript/type-primitives/nullable-types.ts"},{"path":"/es/play/typescript/type-primitives/tuples.ts"},{"path":"/ja/play/typescript/language-extensions/enums.ts"},{"path":"/ja/play/typescript/language-extensions/nominal-typing.ts"},{"path":"/ja/play/typescript/language-extensions/types-vs-interfaces.ts"},{"path":"/ja/play/typescript/primitives/any.ts"},{"path":"/ja/play/typescript/primitives/literals.ts"},{"path":"/ja/play/typescript/primitives/union-and-intersection-types.ts"},{"path":"/ja/play/typescript/type-primitives/built-in-utility-types.ts"},{"path":"/vo/play/javascript/javascript-essentials/code-flow.ts"},{"path":"/vo/play/javascript/javascript-essentials/functions.ts"},{"path":"/vo/play/javascript/javascript-essentials/hello-world.ts"},{"path":"/vo/play/javascript/javascript-essentials/objects-and-arrays.ts"},{"path":"/zh/play/javascript/external-apis/typescript-with-deno.ts"},{"path":"/zh/play/javascript/external-apis/typescript-with-node.js"},{"path":"/zh/play/javascript/external-apis/typescript-with-web.js"},{"path":"/zh/play/javascript/external-apis/typescript-with-webgl.js"},{"path":"/zh/play/javascript/functions-with-javascript/function-chaining.ts"},{"path":"/zh/play/javascript/functions-with-javascript/generic-functions.ts"},{"path":"/zh/play/javascript/functions-with-javascript/typing-functions.ts"},{"path":"/zh/play/javascript/helping-with-javascript/errors.ts"},{"path":"/zh/play/javascript/helping-with-javascript/quick-fixes.ts"},{"path":"/zh/play/javascript/javascript-essentials/code-flow.ts"},{"path":"/zh/play/javascript/javascript-essentials/functions.ts"},{"path":"/zh/play/javascript/javascript-essentials/hello-world.ts"},{"path":"/zh/play/javascript/modern-javascript/import-export.ts"},{"path":"/zh/play/javascript/modern-javascript/jsdoc-support.js"},{"path":"/zh/play/javascript/working-with-classes/classes-101.ts"},{"path":"/zh/play/javascript/working-with-classes/generic-classes.ts"},{"path":"/zh/play/javascript/working-with-classes/mixins.ts"},{"path":"/zh/play/javascript/working-with-classes/this.ts"},{"path":"/zh/play/playground/config/javascript-playgrounds.js"},{"path":"/zh/play/playground/config/new-compiler-defaults.ts"},{"path":"/zh/play/playground/language/automatic-type-acquisition.ts"},{"path":"/zh/play/playground/language/fixits.ts"},{"path":"/zh/play/playground/tooling/mobile-support.ts"},{"path":"/zh/play/playground/tooling/sharable-urls.ts"},{"path":"/zh/play/playground/tooling/typescript-versions.ts"},{"path":"/zh/play/typescript/language/soundness.ts"},{"path":"/zh/play/typescript/language/structural-typing.ts"},{"path":"/zh/play/typescript/language/type-guards.ts"},{"path":"/zh/play/typescript/language/type-widening-and-narrowing.ts"},{"path":"/zh/play/typescript/language-extensions/enums.ts"},{"path":"/zh/play/typescript/language-extensions/nominal-typing.ts"},{"path":"/zh/play/typescript/language-extensions/types-vs-interfaces.ts"},{"path":"/zh/play/typescript/meta-types/conditional-types.ts"},{"path":"/zh/play/typescript/meta-types/discriminate-types.ts"},{"path":"/zh/play/typescript/meta-types/indexed-types.ts"},{"path":"/zh/play/typescript/meta-types/mapped-types.ts"},{"path":"/zh/play/typescript/primitives/any.ts"},{"path":"/zh/play/typescript/primitives/literals.ts"},{"path":"/zh/play/typescript/primitives/union-and-intersection-types.ts"},{"path":"/zh/play/typescript/primitives/unknown-and-never.ts"},{"path":"/zh/play/typescript/type-primitives/built-in-utility-types.ts"},{"path":"/zh/play/typescript/type-primitives/nullable-types.ts"},{"path":"/zh/play/typescript/type-primitives/tuples.ts"},{"path":"/zh/play/javascript/modern-javascript/immutability.ts"},{"path":"/zh/play/javascript/modern-javascript/async-await.ts"},{"path":"/zh/play/javascript/javascript-essentials/objects-and-arrays.ts"},{"path":"/community"},{"path":"/es/community"},{"path":"/ja/community"},{"path":"/vo/community"},{"path":"/zh/community"},{"path":"/download"},{"path":"/es/download"},{"path":"/ja/download"},{"path":"/vo/download"},{"path":"/zh/download"},{"path":"/empty"},{"path":"/es/empty"},{"path":"/ja/empty"},{"path":"/vo/empty"},{"path":"/zh/empty"},{"path":"/"},{"path":"/es/"},{"path":"/ja/"},{"path":"/vo/"},{"path":"/zh/"},{"path":"/tools"},{"path":"/es/tools"},{"path":"/ja/tools"},{"path":"/vo/tools"},{"path":"/zh/tools"},{"path":"/why-create-typescript"},{"path":"/es/why-create-typescript"},{"path":"/ja/why-create-typescript"},{"path":"/vo/why-create-typescript"},{"path":"/zh/why-create-typescript"},{"path":"/docs/home"},{"path":"/es/docs/home"},{"path":"/ja/docs/home"},{"path":"/vo/docs/home"},{"path":"/zh/docs/home"},{"path":"/dev/playground-plugins/"},{"path":"/dev/sandbox/"},{"path":"/dev/twoslash/"},{"path":"/dev/typescript-vfs/"}]},"markdownRemark":{"id":"49b9485a-5779-59b4-abbf-cec7146e9c15","excerpt":"Introduction A major part of software engineering is building components that not only have well-defined and consistent APIs, but are also reusable.\nComponents…","html":"<h1 id=\"introduction\" style=\"position:relative;\"><a href=\"#introduction\" aria-label=\"introduction permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>Introduction</h1>\n<p>A major part of software engineering is building components that not only have well-defined and consistent APIs, but are also reusable.\nComponents that are capable of working on the data of today as well as the data of tomorrow will give you the most flexible capabilities for building up large software systems.</p>\n<p>In languages like C# and Java, one of the main tools in the toolbox for creating reusable components is <em>generics</em>, that is, being able to create a component that can work over a variety of types rather than a single one.\nThis allows users to consume these components and use their own types.</p>\n<h1 id=\"hello-world-of-generics\" style=\"position:relative;\"><a href=\"#hello-world-of-generics\" aria-label=\"hello world of generics permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>Hello World of Generics</h1>\n<p>To start off, let’s do the “hello world” of generics: the identity function.\nThe identity function is a function that will return back whatever is passed in.\nYou can think of this in a similar way to the <code>echo</code> command.</p>\n<p>Without generics, we would either have to give the identity function a specific type:</p>\n<pre class=\"shiki\"><div class=\"language-id\">ts</div><div class='code-container'><code><span style=\"color: #0000FF\">function</span><span style=\"color: #000000\"> identity(arg: number): number {</span>\n<span style=\"color: #000000\">  </span><span style=\"color: #0000FF\">return</span><span style=\"color: #000000\"> arg;</span>\n<span style=\"color: #000000\">}</span></code></div></pre>\n<p>Or, we could describe the identity function using the <code>any</code> type:</p>\n<pre class=\"shiki\"><div class=\"language-id\">ts</div><div class='code-container'><code><span style=\"color: #0000FF\">function</span><span style=\"color: #000000\"> identity(arg: any): any {</span>\n<span style=\"color: #000000\">  </span><span style=\"color: #0000FF\">return</span><span style=\"color: #000000\"> arg;</span>\n<span style=\"color: #000000\">}</span></code></div></pre>\n<p>While using <code>any</code> is certainly generic in that it will cause the function to accept any and all types for the type of <code>arg</code>, we actually are losing the information about what that type was when the function returns.\nIf we passed in a number, the only information we have is that any type could be returned.</p>\n<p>Instead, we need a way of capturing the type of the argument in such a way that we can also use it to denote what is being returned.\nHere, we will use a <em>type variable</em>, a special kind of variable that works on types rather than values.</p>\n<pre class=\"shiki\"><div class=\"language-id\">ts</div><div class='code-container'><code><span style=\"color: #0000FF\">function</span><span style=\"color: #000000\"> identity&lt;T&gt;(arg: T): T {</span>\n<span style=\"color: #000000\">  </span><span style=\"color: #0000FF\">return</span><span style=\"color: #000000\"> arg;</span>\n<span style=\"color: #000000\">}</span></code></div></pre>\n<p>We’ve now added a type variable <code>T</code> to the identity function.\nThis <code>T</code> allows us to capture the type the user provides (e.g. <code>number</code>), so that we can use that information later.\nHere, we use <code>T</code> again as the return type. On inspection, we can now see the same type is used for the argument and the return type.\nThis allows us to traffic that type information in one side of the function and out the other.</p>\n<p>We say that this version of the <code>identity</code> function is generic, as it works over a range of types.\nUnlike using <code>any</code>, it’s also just as precise (ie, it doesn’t lose any information) as the first <code>identity</code> function that used numbers for the argument and return type.</p>\n<p>Once we’ve written the generic identity function, we can call it in one of two ways.\nThe first way is to pass all of the arguments, including the type argument, to the function:</p>\n<pre class=\"shiki\"><div class=\"language-id\">ts</div><div class='code-container'><code><span style=\"color: #0000FF\">let</span><span style=\"color: #000000\"> output = identity&lt;string&gt;(</span><span style=\"color: #A31515\">\"myString\"</span><span style=\"color: #000000\">); </span><span style=\"color: #008000\">// type of output will be 'string'</span></code></div></pre>\n<p>Here we explicitly set <code>T</code> to be <code>string</code> as one of the arguments to the function call, denoted using the <code>&#x3C;></code> around the arguments rather than <code>()</code>.</p>\n<p>The second way is also perhaps the most common. Here we use <em>type argument inference</em> — that is, we want the compiler to set the value of <code>T</code> for us automatically based on the type of the argument we pass in:</p>\n<pre class=\"shiki\"><div class=\"language-id\">ts</div><div class='code-container'><code><span style=\"color: #0000FF\">let</span><span style=\"color: #000000\"> output = identity(</span><span style=\"color: #A31515\">\"myString\"</span><span style=\"color: #000000\">); </span><span style=\"color: #008000\">// type of output will be 'string'</span></code></div></pre>\n<p>Notice that we didn’t have to explicitly pass the type in the angle brackets (<code>&#x3C;></code>); the compiler just looked at the value <code>\"myString\"</code>, and set <code>T</code> to its type.\nWhile type argument inference can be a helpful tool to keep code shorter and more readable, you may need to explicitly pass in the type arguments as we did in the previous example when the compiler fails to infer the type, as may happen in more complex examples.</p>\n<h1 id=\"working-with-generic-type-variables\" style=\"position:relative;\"><a href=\"#working-with-generic-type-variables\" aria-label=\"working with generic type variables permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>Working with Generic Type Variables</h1>\n<p>When you begin to use generics, you’ll notice that when you create generic functions like <code>identity</code>, the compiler will enforce that you use any generically typed parameters in the body of the function correctly.\nThat is, that you actually treat these parameters as if they could be any and all types.</p>\n<p>Let’s take our <code>identity</code> function from earlier:</p>\n<pre class=\"shiki\"><div class=\"language-id\">ts</div><div class='code-container'><code><span style=\"color: #0000FF\">function</span><span style=\"color: #000000\"> identity&lt;T&gt;(arg: T): T {</span>\n<span style=\"color: #000000\">  </span><span style=\"color: #0000FF\">return</span><span style=\"color: #000000\"> arg;</span>\n<span style=\"color: #000000\">}</span></code></div></pre>\n<p>What if we want to also log the length of the argument <code>arg</code> to the console with each call?\nWe might be tempted to write this:</p>\n<pre class=\"shiki\"><div class=\"language-id\">ts</div><div class='code-container'><code><span style=\"color: #0000FF\">function</span><span style=\"color: #000000\"> loggingIdentity&lt;T&gt;(arg: T): T {</span>\n<span style=\"color: #000000\">  console.log(arg.length); </span><span style=\"color: #008000\">// Error: T doesn't have .length</span>\n<span style=\"color: #000000\">  </span><span style=\"color: #0000FF\">return</span><span style=\"color: #000000\"> arg;</span>\n<span style=\"color: #000000\">}</span></code></div></pre>\n<p>When we do, the compiler will give us an error that we’re using the <code>.length</code> member of <code>arg</code>, but nowhere have we said that <code>arg</code> has this member.\nRemember, we said earlier that these type variables stand in for any and all types, so someone using this function could have passed in a <code>number</code> instead, which does not have a <code>.length</code> member.</p>\n<p>Let’s say that we’ve actually intended this function to work on arrays of <code>T</code> rather than <code>T</code> directly. Since we’re working with arrays, the <code>.length</code> member should be available.\nWe can describe this just like we would create arrays of other types:</p>\n<pre class=\"shiki\"><div class=\"language-id\">ts</div><div class='code-container'><code><span style=\"color: #0000FF\">function</span><span style=\"color: #000000\"> loggingIdentity&lt;T&gt;(arg: T[]): T[] {</span>\n<span style=\"color: #000000\">  console.log(arg.length); </span><span style=\"color: #008000\">// Array has a .length, so no more error</span>\n<span style=\"color: #000000\">  </span><span style=\"color: #0000FF\">return</span><span style=\"color: #000000\"> arg;</span>\n<span style=\"color: #000000\">}</span></code></div></pre>\n<p>You can read the type of <code>loggingIdentity</code> as “the generic function <code>loggingIdentity</code> takes a type parameter <code>T</code>, and an argument <code>arg</code> which is an array of <code>T</code>s, and returns an array of <code>T</code>s.”\nIf we passed in an array of numbers, we’d get an array of numbers back out, as <code>T</code> would bind to <code>number</code>.\nThis allows us to use our generic type variable <code>T</code> as part of the types we’re working with, rather than the whole type, giving us greater flexibility.</p>\n<p>We can alternatively write the sample example this way:</p>\n<pre class=\"shiki\"><div class=\"language-id\">ts</div><div class='code-container'><code><span style=\"color: #0000FF\">function</span><span style=\"color: #000000\"> loggingIdentity&lt;T&gt;(arg: Array&lt;T&gt;): Array&lt;T&gt; {</span>\n<span style=\"color: #000000\">  console.log(arg.length); </span><span style=\"color: #008000\">// Array has a .length, so no more error</span>\n<span style=\"color: #000000\">  </span><span style=\"color: #0000FF\">return</span><span style=\"color: #000000\"> arg;</span>\n<span style=\"color: #000000\">}</span></code></div></pre>\n<p>You may already be familiar with this style of type from other languages.\nIn the next section, we’ll cover how you can create your own generic types like <code>Array&#x3C;T></code>.</p>\n<h1 id=\"generic-types\" style=\"position:relative;\"><a href=\"#generic-types\" aria-label=\"generic types permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>Generic Types</h1>\n<p>In previous sections, we created generic identity functions that worked over a range of types.\nIn this section, we’ll explore the type of the functions themselves and how to create generic interfaces.</p>\n<p>The type of generic functions is just like those of non-generic functions, with the type parameters listed first, similarly to function declarations:</p>\n<pre class=\"shiki\"><div class=\"language-id\">ts</div><div class='code-container'><code><span style=\"color: #0000FF\">function</span><span style=\"color: #000000\"> identity&lt;T&gt;(arg: T): T {</span>\n<span style=\"color: #000000\">  </span><span style=\"color: #0000FF\">return</span><span style=\"color: #000000\"> arg;</span>\n<span style=\"color: #000000\">}</span>\n\n<span style=\"color: #0000FF\">let</span><span style=\"color: #000000\"> myIdentity: &lt;T&gt;(arg: T) </span><span style=\"color: #0000FF\">=&gt;</span><span style=\"color: #000000\"> T = identity;</span></code></div></pre>\n<p>We could also have used a different name for the generic type parameter in the type, so long as the number of type variables and how the type variables are used line up.</p>\n<pre class=\"shiki\"><div class=\"language-id\">ts</div><div class='code-container'><code><span style=\"color: #0000FF\">function</span><span style=\"color: #000000\"> identity&lt;T&gt;(arg: T): T {</span>\n<span style=\"color: #000000\">  </span><span style=\"color: #0000FF\">return</span><span style=\"color: #000000\"> arg;</span>\n<span style=\"color: #000000\">}</span>\n\n<span style=\"color: #0000FF\">let</span><span style=\"color: #000000\"> myIdentity: &lt;U&gt;(arg: U) </span><span style=\"color: #0000FF\">=&gt;</span><span style=\"color: #000000\"> U = identity;</span></code></div></pre>\n<p>We can also write the generic type as a call signature of an object literal type:</p>\n<pre class=\"shiki\"><div class=\"language-id\">ts</div><div class='code-container'><code><span style=\"color: #0000FF\">function</span><span style=\"color: #000000\"> identity&lt;T&gt;(arg: T): T {</span>\n<span style=\"color: #000000\">  </span><span style=\"color: #0000FF\">return</span><span style=\"color: #000000\"> arg;</span>\n<span style=\"color: #000000\">}</span>\n\n<span style=\"color: #0000FF\">let</span><span style=\"color: #000000\"> myIdentity: { &lt;T&gt;(arg: T): T } = identity;</span></code></div></pre>\n<p>Which leads us to writing our first generic interface.\nLet’s take the object literal from the previous example and move it to an interface:</p>\n<pre class=\"shiki\"><div class=\"language-id\">ts</div><div class='code-container'><code><span style=\"color: #0000FF\">interface</span><span style=\"color: #000000\"> GenericIdentityFn {</span>\n<span style=\"color: #000000\">  &lt;T&gt;(arg: T): T;</span>\n<span style=\"color: #000000\">}</span>\n\n<span style=\"color: #0000FF\">function</span><span style=\"color: #000000\"> identity&lt;T&gt;(arg: T): T {</span>\n<span style=\"color: #000000\">  </span><span style=\"color: #0000FF\">return</span><span style=\"color: #000000\"> arg;</span>\n<span style=\"color: #000000\">}</span>\n\n<span style=\"color: #0000FF\">let</span><span style=\"color: #000000\"> myIdentity: GenericIdentityFn = identity;</span></code></div></pre>\n<p>In a similar example, we may want to move the generic parameter to be a parameter of the whole interface.\nThis lets us see what type(s) we’re generic over (e.g. <code>Dictionary&#x3C;string></code> rather than just <code>Dictionary</code>).\nThis makes the type parameter visible to all the other members of the interface.</p>\n<pre class=\"shiki\"><div class=\"language-id\">ts</div><div class='code-container'><code><span style=\"color: #0000FF\">interface</span><span style=\"color: #000000\"> GenericIdentityFn&lt;T&gt; {</span>\n<span style=\"color: #000000\">  (arg: T): T;</span>\n<span style=\"color: #000000\">}</span>\n\n<span style=\"color: #0000FF\">function</span><span style=\"color: #000000\"> identity&lt;T&gt;(arg: T): T {</span>\n<span style=\"color: #000000\">  </span><span style=\"color: #0000FF\">return</span><span style=\"color: #000000\"> arg;</span>\n<span style=\"color: #000000\">}</span>\n\n<span style=\"color: #0000FF\">let</span><span style=\"color: #000000\"> myIdentity: GenericIdentityFn&lt;number&gt; = identity;</span></code></div></pre>\n<p>Notice that our example has changed to be something slightly different.\nInstead of describing a generic function, we now have a non-generic function signature that is a part of a generic type.\nWhen we use <code>GenericIdentityFn</code>, we now will also need to specify the corresponding type argument (here: <code>number</code>), effectively locking in what the underlying call signature will use.\nUnderstanding when to put the type parameter directly on the call signature and when to put it on the interface itself will be helpful in describing what aspects of a type are generic.</p>\n<p>In addition to generic interfaces, we can also create generic classes.\nNote that it is not possible to create generic enums and namespaces.</p>\n<h1 id=\"generic-classes\" style=\"position:relative;\"><a href=\"#generic-classes\" aria-label=\"generic classes permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>Generic Classes</h1>\n<p>A generic class has a similar shape to a generic interface.\nGeneric classes have a generic type parameter list in angle brackets (<code>&#x3C;></code>) following the name of the class.</p>\n<pre class=\"shiki\"><div class=\"language-id\">ts</div><div class='code-container'><code><span style=\"color: #0000FF\">class</span><span style=\"color: #000000\"> GenericNumber&lt;T&gt; {</span>\n<span style=\"color: #000000\">  zeroValue: T;</span>\n<span style=\"color: #000000\">  add: (x: T, y: T) </span><span style=\"color: #0000FF\">=&gt;</span><span style=\"color: #000000\"> T;</span>\n<span style=\"color: #000000\">}</span>\n\n<span style=\"color: #0000FF\">let</span><span style=\"color: #000000\"> myGenericNumber = </span><span style=\"color: #0000FF\">new</span><span style=\"color: #000000\"> GenericNumber&lt;number&gt;();</span>\n<span style=\"color: #000000\">myGenericNumber.zeroValue = </span><span style=\"color: #09835A\">0</span><span style=\"color: #000000\">;</span>\n<span style=\"color: #000000\">myGenericNumber.add = </span><span style=\"color: #0000FF\">function</span><span style=\"color: #000000\">(x, y) {</span>\n<span style=\"color: #000000\">  </span><span style=\"color: #0000FF\">return</span><span style=\"color: #000000\"> x + y;</span>\n<span style=\"color: #000000\">};</span></code></div></pre>\n<p>This is a pretty literal use of the <code>GenericNumber</code> class, but you may have noticed that nothing is restricting it to only use the <code>number</code> type.\nWe could have instead used <code>string</code> or even more complex objects.</p>\n<pre class=\"shiki\"><div class=\"language-id\">ts</div><div class='code-container'><code><span style=\"color: #0000FF\">let</span><span style=\"color: #000000\"> stringNumeric = </span><span style=\"color: #0000FF\">new</span><span style=\"color: #000000\"> GenericNumber&lt;string&gt;();</span>\n<span style=\"color: #000000\">stringNumeric.zeroValue = </span><span style=\"color: #A31515\">\"\"</span><span style=\"color: #000000\">;</span>\n<span style=\"color: #000000\">stringNumeric.add = </span><span style=\"color: #0000FF\">function</span><span style=\"color: #000000\">(x, y) {</span>\n<span style=\"color: #000000\">  </span><span style=\"color: #0000FF\">return</span><span style=\"color: #000000\"> x + y;</span>\n<span style=\"color: #000000\">};</span>\n\n<span style=\"color: #000000\">console.log(stringNumeric.add(stringNumeric.zeroValue, </span><span style=\"color: #A31515\">\"test\"</span><span style=\"color: #000000\">));</span></code></div></pre>\n<p>Just as with interface, putting the type parameter on the class itself lets us make sure all of the properties of the class are working with the same type.</p>\n<p>As we covered in <a href=\"/4d6a169eee0e52b47b0fe0d97cde5e96/Classes.md\">our section on classes</a>, a class has two sides to its type: the static side and the instance side.\nGeneric classes are only generic over their instance side rather than their static side, so when working with classes, static members can not use the class’s type parameter.</p>\n<h1 id=\"generic-constraints\" style=\"position:relative;\"><a href=\"#generic-constraints\" aria-label=\"generic constraints permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>Generic Constraints</h1>\n<p>If you remember from an earlier example, you may sometimes want to write a generic function that works on a set of types where you have some knowledge about what capabilities that set of types will have.\nIn our <code>loggingIdentity</code> example, we wanted to be able to access the <code>.length</code> property of <code>arg</code>, but the compiler could not prove that every type had a <code>.length</code> property, so it warns us that we can’t make this assumption.</p>\n<pre class=\"shiki\"><div class=\"language-id\">ts</div><div class='code-container'><code><span style=\"color: #0000FF\">function</span><span style=\"color: #000000\"> loggingIdentity&lt;T&gt;(arg: T): T {</span>\n<span style=\"color: #000000\">  console.log(arg.length); </span><span style=\"color: #008000\">// Error: T doesn't have .length</span>\n<span style=\"color: #000000\">  </span><span style=\"color: #0000FF\">return</span><span style=\"color: #000000\"> arg;</span>\n<span style=\"color: #000000\">}</span></code></div></pre>\n<p>Instead of working with any and all types, we’d like to constrain this function to work with any and all types that also have the <code>.length</code> property.\nAs long as the type has this member, we’ll allow it, but it’s required to have at least this member.\nTo do so, we must list our requirement as a constraint on what T can be.</p>\n<p>To do so, we’ll create an interface that describes our constraint.\nHere, we’ll create an interface that has a single <code>.length</code> property and then we’ll use this interface and the <code>extends</code> keyword to denote our constraint:</p>\n<pre class=\"shiki\"><div class=\"language-id\">ts</div><div class='code-container'><code><span style=\"color: #0000FF\">interface</span><span style=\"color: #000000\"> Lengthwise {</span>\n<span style=\"color: #000000\">  length: number;</span>\n<span style=\"color: #000000\">}</span>\n\n<span style=\"color: #0000FF\">function</span><span style=\"color: #000000\"> loggingIdentity&lt;T </span><span style=\"color: #0000FF\">extends</span><span style=\"color: #000000\"> Lengthwise&gt;(arg: T): T {</span>\n<span style=\"color: #000000\">  console.log(arg.length); </span><span style=\"color: #008000\">// Now we know it has a .length property, so no more error</span>\n<span style=\"color: #000000\">  </span><span style=\"color: #0000FF\">return</span><span style=\"color: #000000\"> arg;</span>\n<span style=\"color: #000000\">}</span></code></div></pre>\n<p>Because the generic function is now constrained, it will no longer work over any and all types:</p>\n<pre class=\"shiki\"><div class=\"language-id\">ts</div><div class='code-container'><code><span style=\"color: #000000\">loggingIdentity(</span><span style=\"color: #09835A\">3</span><span style=\"color: #000000\">); </span><span style=\"color: #008000\">// Error, number doesn't have a .length property</span></code></div></pre>\n<p>Instead, we need to pass in values whose type has all the required properties:</p>\n<pre class=\"shiki\"><div class=\"language-id\">ts</div><div class='code-container'><code><span style=\"color: #000000\">loggingIdentity({ length: </span><span style=\"color: #09835A\">10</span><span style=\"color: #000000\">, value: </span><span style=\"color: #09835A\">3</span><span style=\"color: #000000\"> });</span></code></div></pre>\n<h2 id=\"using-type-parameters-in-generic-constraints\" style=\"position:relative;\"><a href=\"#using-type-parameters-in-generic-constraints\" aria-label=\"using type parameters in generic constraints permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>Using Type Parameters in Generic Constraints</h2>\n<p>You can declare a type parameter that is constrained by another type parameter.\nFor example, here we’d like to get a property from an object given its name.\nWe’d like to ensure that we’re not accidentally grabbing a property that does not exist on the <code>obj</code>, so we’ll place a constraint between the two types:</p>\n<pre class=\"shiki\"><div class=\"language-id\">ts</div><div class='code-container'><code><span style=\"color: #0000FF\">function</span><span style=\"color: #000000\"> getProperty&lt;T, K </span><span style=\"color: #0000FF\">extends</span><span style=\"color: #000000\"> </span><span style=\"color: #0000FF\">keyof</span><span style=\"color: #000000\"> T&gt;(obj: T, key: K) {</span>\n<span style=\"color: #000000\">  </span><span style=\"color: #0000FF\">return</span><span style=\"color: #000000\"> obj[key];</span>\n<span style=\"color: #000000\">}</span>\n\n<span style=\"color: #0000FF\">let</span><span style=\"color: #000000\"> x = { a: </span><span style=\"color: #09835A\">1</span><span style=\"color: #000000\">, b: </span><span style=\"color: #09835A\">2</span><span style=\"color: #000000\">, c: </span><span style=\"color: #09835A\">3</span><span style=\"color: #000000\">, d: </span><span style=\"color: #09835A\">4</span><span style=\"color: #000000\"> };</span>\n\n<span style=\"color: #000000\">getProperty(x, </span><span style=\"color: #A31515\">\"a\"</span><span style=\"color: #000000\">); </span><span style=\"color: #008000\">// okay</span>\n<span style=\"color: #000000\">getProperty(x, </span><span style=\"color: #A31515\">\"m\"</span><span style=\"color: #000000\">); </span><span style=\"color: #008000\">// error: Argument of type 'm' isn't assignable to 'a' | 'b' | 'c' | 'd'.</span></code></div></pre>\n<h2 id=\"using-class-types-in-generics\" style=\"position:relative;\"><a href=\"#using-class-types-in-generics\" aria-label=\"using class types in generics permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>Using Class Types in Generics</h2>\n<p>When creating factories in TypeScript using generics, it is necessary to refer to class types by their constructor functions. For example,</p>\n<pre class=\"shiki\"><div class=\"language-id\">ts</div><div class='code-container'><code><span style=\"color: #0000FF\">function</span><span style=\"color: #000000\"> create&lt;T&gt;(c: { </span><span style=\"color: #0000FF\">new</span><span style=\"color: #000000\"> (): T }): T {</span>\n<span style=\"color: #000000\">  </span><span style=\"color: #0000FF\">return</span><span style=\"color: #000000\"> </span><span style=\"color: #0000FF\">new</span><span style=\"color: #000000\"> c();</span>\n<span style=\"color: #000000\">}</span></code></div></pre>\n<p>A more advanced example uses the prototype property to infer and constrain relationships between the constructor function and the instance side of class types.</p>\n<pre class=\"shiki\"><div class=\"language-id\">ts</div><div class='code-container'><code><span style=\"color: #0000FF\">class</span><span style=\"color: #000000\"> BeeKeeper {</span>\n<span style=\"color: #000000\">  hasMask: boolean;</span>\n<span style=\"color: #000000\">}</span>\n\n<span style=\"color: #0000FF\">class</span><span style=\"color: #000000\"> ZooKeeper {</span>\n<span style=\"color: #000000\">  nametag: string;</span>\n<span style=\"color: #000000\">}</span>\n\n<span style=\"color: #0000FF\">class</span><span style=\"color: #000000\"> Animal {</span>\n<span style=\"color: #000000\">  numLegs: number;</span>\n<span style=\"color: #000000\">}</span>\n\n<span style=\"color: #0000FF\">class</span><span style=\"color: #000000\"> Bee </span><span style=\"color: #0000FF\">extends</span><span style=\"color: #000000\"> Animal {</span>\n<span style=\"color: #000000\">  keeper: BeeKeeper;</span>\n<span style=\"color: #000000\">}</span>\n\n<span style=\"color: #0000FF\">class</span><span style=\"color: #000000\"> Lion </span><span style=\"color: #0000FF\">extends</span><span style=\"color: #000000\"> Animal {</span>\n<span style=\"color: #000000\">  keeper: ZooKeeper;</span>\n<span style=\"color: #000000\">}</span>\n\n<span style=\"color: #0000FF\">function</span><span style=\"color: #000000\"> createInstance&lt;A </span><span style=\"color: #0000FF\">extends</span><span style=\"color: #000000\"> Animal&gt;(c: </span><span style=\"color: #0000FF\">new</span><span style=\"color: #000000\"> () </span><span style=\"color: #0000FF\">=&gt;</span><span style=\"color: #000000\"> A): A {</span>\n<span style=\"color: #000000\">  </span><span style=\"color: #0000FF\">return</span><span style=\"color: #000000\"> </span><span style=\"color: #0000FF\">new</span><span style=\"color: #000000\"> c();</span>\n<span style=\"color: #000000\">}</span>\n\n<span style=\"color: #000000\">createInstance(Lion).keeper.nametag; </span><span style=\"color: #008000\">// typechecks!</span>\n<span style=\"color: #000000\">createInstance(Bee).keeper.hasMask; </span><span style=\"color: #008000\">// typechecks!</span></code></div></pre>","headings":[{"value":"Introduction","depth":1},{"value":"Hello World of Generics","depth":1},{"value":"Working with Generic Type Variables","depth":1},{"value":"Generic Types","depth":1},{"value":"Generic Classes","depth":1},{"value":"Generic Constraints","depth":1},{"value":"Using Type Parameters in Generic Constraints","depth":2},{"value":"Using Class Types in Generics","depth":2}],"frontmatter":{"permalink":"/docs/handbook/generics.html","title":"Generics"}}},"pageContext":{"slug":"/docs/handbook/generics.html","isOldHandbook":true}}}