{"componentChunkName":"component---src-templates-handbook-tsx","path":"/docs/handbook/functions.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":"1d4fc0d1-9094-5a7d-a4f8-50c305d2ca3c","excerpt":"Introduction Functions are the fundamental building block of any application in JavaScript.\nThey’re how you build up layers of abstraction, mimicking classes…","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>Functions are the fundamental building block of any application in JavaScript.\nThey’re how you build up layers of abstraction, mimicking classes, information hiding, and modules.\nIn TypeScript, while there are classes, namespaces, and modules, functions still play the key role in describing how to <em>do</em> things.\nTypeScript also adds some new capabilities to the standard JavaScript functions to make them easier to work with.</p>\n<h1 id=\"functions\" style=\"position:relative;\"><a href=\"#functions\" aria-label=\"functions 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>Functions</h1>\n<p>To begin, just as in JavaScript, TypeScript functions can be created both as a named function or as an anonymous function.\nThis allows you to choose the most appropriate approach for your application, whether you’re building a list of functions in an API or a one-off function to hand off to another function.</p>\n<p>To quickly recap what these two approaches look like in JavaScript:</p>\n<pre class=\"shiki\"><div class=\"language-id\">ts</div><div class='code-container'><code><span style=\"color: #008000\">// Named function</span>\n<span style=\"color: #0000FF\">function</span><span style=\"color: #000000\"> add(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: #008000\">// Anonymous function</span>\n<span style=\"color: #0000FF\">let</span><span style=\"color: #000000\"> myAdd = </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>Just as in JavaScript, functions can refer to variables outside of the function body.\nWhen they do so, they’re said to <em>capture</em> these variables.\nWhile understanding how this works (and the trade-offs when using this technique) is outside of the scope of this article, having a firm understanding how this mechanic works is an important piece of working with JavaScript and TypeScript.</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\"> z = </span><span style=\"color: #09835A\">100</span><span style=\"color: #000000\">;</span>\n\n<span style=\"color: #0000FF\">function</span><span style=\"color: #000000\"> addToZ(x, y) {</span>\n<span style=\"color: #000000\">  </span><span style=\"color: #0000FF\">return</span><span style=\"color: #000000\"> x + y + z;</span>\n<span style=\"color: #000000\">}</span></code></div></pre>\n<h1 id=\"function-types\" style=\"position:relative;\"><a href=\"#function-types\" aria-label=\"function 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>Function Types</h1>\n<h2 id=\"typing-the-function\" style=\"position:relative;\"><a href=\"#typing-the-function\" aria-label=\"typing the function 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>Typing the function</h2>\n<p>Let’s add types to our simple examples 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\"> add(x: number, y: number): number {</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: #0000FF\">let</span><span style=\"color: #000000\"> myAdd = </span><span style=\"color: #0000FF\">function</span><span style=\"color: #000000\">(x: number, y: number): number {</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>We can add types to each of the parameters and then to the function itself to add a return type.\nTypeScript can figure the return type out by looking at the return statements, so we can also optionally leave this off in many cases.</p>\n<h2 id=\"writing-the-function-type\" style=\"position:relative;\"><a href=\"#writing-the-function-type\" aria-label=\"writing the function type 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>Writing the function type</h2>\n<p>Now that we’ve typed the function, let’s write the full type of the function out by looking at each piece of the function type.</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\"> myAdd: (x: number, y: number) </span><span style=\"color: #0000FF\">=&gt;</span><span style=\"color: #000000\"> number = </span><span style=\"color: #0000FF\">function</span><span style=\"color: #000000\">(</span>\n<span style=\"color: #000000\">  x: number,</span>\n<span style=\"color: #000000\">  y: number</span>\n<span style=\"color: #000000\">): number {</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>A function’s type has the same two parts: the type of the arguments and the return type.\nWhen writing out the whole function type, both parts are required.\nWe write out the parameter types just like a parameter list, giving each parameter a name and a type.\nThis name is just to help with readability.\nWe could have instead written:</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\"> myAdd: (baseValue: number, increment: number) </span><span style=\"color: #0000FF\">=&gt;</span><span style=\"color: #000000\"> number = </span><span style=\"color: #0000FF\">function</span><span style=\"color: #000000\">(</span>\n<span style=\"color: #000000\">  x: number,</span>\n<span style=\"color: #000000\">  y: number</span>\n<span style=\"color: #000000\">): number {</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>As long as the parameter types line up, it’s considered a valid type for the function, regardless of the names you give the parameters in the function type.</p>\n<p>The second part is the return type.\nWe make it clear which is the return type by using a fat arrow (<code>=></code>) between the parameters and the return type.\nAs mentioned before, this is a required part of the function type, so if the function doesn’t return a value, you would use <code>void</code> instead of leaving it off.</p>\n<p>Of note, only the parameters and the return type make up the function type.\nCaptured variables are not reflected in the type.\nIn effect, captured variables are part of the “hidden state” of any function and do not make up its API.</p>\n<h2 id=\"inferring-the-types\" style=\"position:relative;\"><a href=\"#inferring-the-types\" aria-label=\"inferring the 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>Inferring the types</h2>\n<p>In playing with the example, you may notice that the TypeScript compiler can figure out the type even if you only have types on one side of the equation:</p>\n<pre class=\"shiki\"><div class=\"language-id\">ts</div><div class='code-container'><code><span style=\"color: #008000\">// myAdd has the full function type</span>\n<span style=\"color: #0000FF\">let</span><span style=\"color: #000000\"> myAdd = </span><span style=\"color: #0000FF\">function</span><span style=\"color: #000000\">(x: number, y: number): number {</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: #008000\">// The parameters 'x' and 'y' have the type number</span>\n<span style=\"color: #0000FF\">let</span><span style=\"color: #000000\"> myAdd: (baseValue: number, increment: number) </span><span style=\"color: #0000FF\">=&gt;</span><span style=\"color: #000000\"> number = </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 called “contextual typing”, a form of type inference.\nThis helps cut down on the amount of effort to keep your program typed.</p>\n<h1 id=\"optional-and-default-parameters\" style=\"position:relative;\"><a href=\"#optional-and-default-parameters\" aria-label=\"optional and default parameters 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>Optional and Default Parameters</h1>\n<p>In TypeScript, every parameter is assumed to be required by the function.\nThis doesn’t mean that it can’t be given <code>null</code> or <code>undefined</code>, but rather, when the function is called, the compiler will check that the user has provided a value for each parameter.\nThe compiler also assumes that these parameters are the only parameters that will be passed to the function.\nIn short, the number of arguments given to a function has to match the number of parameters the function expects.</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\"> buildName(firstName: string, lastName: string) {</span>\n<span style=\"color: #000000\">  </span><span style=\"color: #0000FF\">return</span><span style=\"color: #000000\"> firstName + </span><span style=\"color: #A31515\">\" \"</span><span style=\"color: #000000\"> + lastName;</span>\n<span style=\"color: #000000\">}</span>\n\n<span style=\"color: #0000FF\">let</span><span style=\"color: #000000\"> result1 = buildName(</span><span style=\"color: #A31515\">\"Bob\"</span><span style=\"color: #000000\">); </span><span style=\"color: #008000\">// error, too few parameters</span>\n<span style=\"color: #0000FF\">let</span><span style=\"color: #000000\"> result2 = buildName(</span><span style=\"color: #A31515\">\"Bob\"</span><span style=\"color: #000000\">, </span><span style=\"color: #A31515\">\"Adams\"</span><span style=\"color: #000000\">, </span><span style=\"color: #A31515\">\"Sr.\"</span><span style=\"color: #000000\">); </span><span style=\"color: #008000\">// error, too many parameters</span>\n<span style=\"color: #0000FF\">let</span><span style=\"color: #000000\"> result3 = buildName(</span><span style=\"color: #A31515\">\"Bob\"</span><span style=\"color: #000000\">, </span><span style=\"color: #A31515\">\"Adams\"</span><span style=\"color: #000000\">); </span><span style=\"color: #008000\">// ah, just right</span></code></div></pre>\n<p>In JavaScript, every parameter is optional, and users may leave them off as they see fit.\nWhen they do, their value is <code>undefined</code>.\nWe can get this functionality in TypeScript by adding a <code>?</code> to the end of parameters we want to be optional.\nFor example, let’s say we want the last name parameter from above to be optional:</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\"> buildName(firstName: string, lastName?: string) {</span>\n<span style=\"color: #000000\">  </span><span style=\"color: #0000FF\">if</span><span style=\"color: #000000\"> (lastName) </span><span style=\"color: #0000FF\">return</span><span style=\"color: #000000\"> firstName + </span><span style=\"color: #A31515\">\" \"</span><span style=\"color: #000000\"> + lastName;</span>\n<span style=\"color: #000000\">  </span><span style=\"color: #0000FF\">else</span><span style=\"color: #000000\"> </span><span style=\"color: #0000FF\">return</span><span style=\"color: #000000\"> firstName;</span>\n<span style=\"color: #000000\">}</span>\n\n<span style=\"color: #0000FF\">let</span><span style=\"color: #000000\"> result1 = buildName(</span><span style=\"color: #A31515\">\"Bob\"</span><span style=\"color: #000000\">); </span><span style=\"color: #008000\">// works correctly now</span>\n<span style=\"color: #0000FF\">let</span><span style=\"color: #000000\"> result2 = buildName(</span><span style=\"color: #A31515\">\"Bob\"</span><span style=\"color: #000000\">, </span><span style=\"color: #A31515\">\"Adams\"</span><span style=\"color: #000000\">, </span><span style=\"color: #A31515\">\"Sr.\"</span><span style=\"color: #000000\">); </span><span style=\"color: #008000\">// error, too many parameters</span>\n<span style=\"color: #0000FF\">let</span><span style=\"color: #000000\"> result3 = buildName(</span><span style=\"color: #A31515\">\"Bob\"</span><span style=\"color: #000000\">, </span><span style=\"color: #A31515\">\"Adams\"</span><span style=\"color: #000000\">); </span><span style=\"color: #008000\">// ah, just right</span></code></div></pre>\n<p>Any optional parameters must follow required parameters.\nHad we wanted to make the first name optional, rather than the last name, we would need to change the order of parameters in the function, putting the first name last in the list.</p>\n<p>In TypeScript, we can also set a value that a parameter will be assigned if the user does not provide one, or if the user passes <code>undefined</code> in its place.\nThese are called default-initialized parameters.\nLet’s take the previous example and default the last name to <code>\"Smith\"</code>.</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\"> buildName(firstName: string, lastName = </span><span style=\"color: #A31515\">\"Smith\"</span><span style=\"color: #000000\">) {</span>\n<span style=\"color: #000000\">  </span><span style=\"color: #0000FF\">return</span><span style=\"color: #000000\"> firstName + </span><span style=\"color: #A31515\">\" \"</span><span style=\"color: #000000\"> + lastName;</span>\n<span style=\"color: #000000\">}</span>\n\n<span style=\"color: #0000FF\">let</span><span style=\"color: #000000\"> result1 = buildName(</span><span style=\"color: #A31515\">\"Bob\"</span><span style=\"color: #000000\">); </span><span style=\"color: #008000\">// works correctly now, returns \"Bob Smith\"</span>\n<span style=\"color: #0000FF\">let</span><span style=\"color: #000000\"> result2 = buildName(</span><span style=\"color: #A31515\">\"Bob\"</span><span style=\"color: #000000\">, </span><span style=\"color: #0000FF\">undefined</span><span style=\"color: #000000\">); </span><span style=\"color: #008000\">// still works, also returns \"Bob Smith\"</span>\n<span style=\"color: #0000FF\">let</span><span style=\"color: #000000\"> result3 = buildName(</span><span style=\"color: #A31515\">\"Bob\"</span><span style=\"color: #000000\">, </span><span style=\"color: #A31515\">\"Adams\"</span><span style=\"color: #000000\">, </span><span style=\"color: #A31515\">\"Sr.\"</span><span style=\"color: #000000\">); </span><span style=\"color: #008000\">// error, too many parameters</span>\n<span style=\"color: #0000FF\">let</span><span style=\"color: #000000\"> result4 = buildName(</span><span style=\"color: #A31515\">\"Bob\"</span><span style=\"color: #000000\">, </span><span style=\"color: #A31515\">\"Adams\"</span><span style=\"color: #000000\">); </span><span style=\"color: #008000\">// ah, just right</span></code></div></pre>\n<p>Default-initialized parameters that come after all required parameters are treated as optional, and just like optional parameters, can be omitted when calling their respective function.\nThis means optional parameters and trailing default parameters will share commonality in their types, so both</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\"> buildName(firstName: string, lastName?: string) {</span>\n<span style=\"color: #000000\">  </span><span style=\"color: #008000\">// ...</span>\n<span style=\"color: #000000\">}</span></code></div></pre>\n<p>and</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\"> buildName(firstName: string, lastName = </span><span style=\"color: #A31515\">\"Smith\"</span><span style=\"color: #000000\">) {</span>\n<span style=\"color: #000000\">  </span><span style=\"color: #008000\">// ...</span>\n<span style=\"color: #000000\">}</span></code></div></pre>\n<p>share the same type <code>(firstName: string, lastName?: string) => string</code>.\nThe default value of <code>lastName</code> disappears in the type, only leaving behind the fact that the parameter is optional.</p>\n<p>Unlike plain optional parameters, default-initialized parameters don’t <em>need</em> to occur after required parameters.\nIf a default-initialized parameter comes before a required parameter, users need to explicitly pass <code>undefined</code> to get the default initialized value.\nFor example, we could write our last example with only a default initializer on <code>firstName</code>:</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\"> buildName(firstName = </span><span style=\"color: #A31515\">\"Will\"</span><span style=\"color: #000000\">, lastName: string) {</span>\n<span style=\"color: #000000\">  </span><span style=\"color: #0000FF\">return</span><span style=\"color: #000000\"> firstName + </span><span style=\"color: #A31515\">\" \"</span><span style=\"color: #000000\"> + lastName;</span>\n<span style=\"color: #000000\">}</span>\n\n<span style=\"color: #0000FF\">let</span><span style=\"color: #000000\"> result1 = buildName(</span><span style=\"color: #A31515\">\"Bob\"</span><span style=\"color: #000000\">); </span><span style=\"color: #008000\">// error, too few parameters</span>\n<span style=\"color: #0000FF\">let</span><span style=\"color: #000000\"> result2 = buildName(</span><span style=\"color: #A31515\">\"Bob\"</span><span style=\"color: #000000\">, </span><span style=\"color: #A31515\">\"Adams\"</span><span style=\"color: #000000\">, </span><span style=\"color: #A31515\">\"Sr.\"</span><span style=\"color: #000000\">); </span><span style=\"color: #008000\">// error, too many parameters</span>\n<span style=\"color: #0000FF\">let</span><span style=\"color: #000000\"> result3 = buildName(</span><span style=\"color: #A31515\">\"Bob\"</span><span style=\"color: #000000\">, </span><span style=\"color: #A31515\">\"Adams\"</span><span style=\"color: #000000\">); </span><span style=\"color: #008000\">// okay and returns \"Bob Adams\"</span>\n<span style=\"color: #0000FF\">let</span><span style=\"color: #000000\"> result4 = buildName(</span><span style=\"color: #0000FF\">undefined</span><span style=\"color: #000000\">, </span><span style=\"color: #A31515\">\"Adams\"</span><span style=\"color: #000000\">); </span><span style=\"color: #008000\">// okay and returns \"Will Adams\"</span></code></div></pre>\n<h1 id=\"rest-parameters\" style=\"position:relative;\"><a href=\"#rest-parameters\" aria-label=\"rest parameters 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>Rest Parameters</h1>\n<p>Required, optional, and default parameters all have one thing in common: they talk about one parameter at a time.\nSometimes, you want to work with multiple parameters as a group, or you may not know how many parameters a function will ultimately take.\nIn JavaScript, you can work with the arguments directly using the <code>arguments</code> variable that is visible inside every function body.</p>\n<p>In TypeScript, you can gather these arguments together into a variable:</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\"> buildName(firstName: string, ...restOfName: string[]) {</span>\n<span style=\"color: #000000\">  </span><span style=\"color: #0000FF\">return</span><span style=\"color: #000000\"> firstName + </span><span style=\"color: #A31515\">\" \"</span><span style=\"color: #000000\"> + restOfName.join(</span><span style=\"color: #A31515\">\" \"</span><span style=\"color: #000000\">);</span>\n<span style=\"color: #000000\">}</span>\n\n<span style=\"color: #008000\">// employeeName will be \"Joseph Samuel Lucas MacKinzie\"</span>\n<span style=\"color: #0000FF\">let</span><span style=\"color: #000000\"> employeeName = buildName(</span><span style=\"color: #A31515\">\"Joseph\"</span><span style=\"color: #000000\">, </span><span style=\"color: #A31515\">\"Samuel\"</span><span style=\"color: #000000\">, </span><span style=\"color: #A31515\">\"Lucas\"</span><span style=\"color: #000000\">, </span><span style=\"color: #A31515\">\"MacKinzie\"</span><span style=\"color: #000000\">);</span></code></div></pre>\n<p><em>Rest parameters</em> are treated as a boundless number of optional parameters.\nWhen passing arguments for a rest parameter, you can use as many as you want; you can even pass none.\nThe compiler will build an array of the arguments passed in with the name given after the ellipsis (<code>...</code>), allowing you to use it in your function.</p>\n<p>The ellipsis is also used in the type of the function with rest parameters:</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\"> buildName(firstName: string, ...restOfName: string[]) {</span>\n<span style=\"color: #000000\">  </span><span style=\"color: #0000FF\">return</span><span style=\"color: #000000\"> firstName + </span><span style=\"color: #A31515\">\" \"</span><span style=\"color: #000000\"> + restOfName.join(</span><span style=\"color: #A31515\">\" \"</span><span style=\"color: #000000\">);</span>\n<span style=\"color: #000000\">}</span>\n\n<span style=\"color: #0000FF\">let</span><span style=\"color: #000000\"> buildNameFun: (fname: string, ...rest: string[]) </span><span style=\"color: #0000FF\">=&gt;</span><span style=\"color: #000000\"> string = buildName;</span></code></div></pre>\n<h1 id=\"this\" style=\"position:relative;\"><a href=\"#this\" aria-label=\"this 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><code>this</code></h1>\n<p>Learning how to use <code>this</code> in JavaScript is something of a rite of passage.\nSince TypeScript is a superset of JavaScript, TypeScript developers also need to learn how to use <code>this</code> and how to spot when it’s not being used correctly.\nFortunately, TypeScript lets you catch incorrect uses of <code>this</code> with a couple of techniques.\nIf you need to learn how <code>this</code> works in JavaScript, though, first read Yehuda Katz’s <a href=\"http://yehudakatz.com/2011/08/11/understanding-javascript-function-invocation-and-this/\">Understanding JavaScript Function Invocation and “this”</a>.\nYehuda’s article explains the inner workings of <code>this</code> very well, so we’ll just cover the basics here.</p>\n<h2 id=\"this-and-arrow-functions\" style=\"position:relative;\"><a href=\"#this-and-arrow-functions\" aria-label=\"this and arrow functions 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><code>this</code> and arrow functions</h2>\n<p>In JavaScript, <code>this</code> is a variable that’s set when a function is called.\nThis makes it a very powerful and flexible feature, but it comes at the cost of always having to know about the context that a function is executing in.\nThis is notoriously confusing, especially when returning a function or passing a function as an argument.</p>\n<p>Let’s look at an example:</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\"> deck = {</span>\n<span style=\"color: #000000\">  suits: [</span><span style=\"color: #A31515\">\"hearts\"</span><span style=\"color: #000000\">, </span><span style=\"color: #A31515\">\"spades\"</span><span style=\"color: #000000\">, </span><span style=\"color: #A31515\">\"clubs\"</span><span style=\"color: #000000\">, </span><span style=\"color: #A31515\">\"diamonds\"</span><span style=\"color: #000000\">],</span>\n<span style=\"color: #000000\">  cards: Array(</span><span style=\"color: #09835A\">52</span><span style=\"color: #000000\">),</span>\n<span style=\"color: #000000\">  createCardPicker: </span><span style=\"color: #0000FF\">function</span><span style=\"color: #000000\">() {</span>\n<span style=\"color: #000000\">    </span><span style=\"color: #0000FF\">return</span><span style=\"color: #000000\"> </span><span style=\"color: #0000FF\">function</span><span style=\"color: #000000\">() {</span>\n<span style=\"color: #000000\">      </span><span style=\"color: #0000FF\">let</span><span style=\"color: #000000\"> pickedCard = Math.floor(Math.random() * </span><span style=\"color: #09835A\">52</span><span style=\"color: #000000\">);</span>\n<span style=\"color: #000000\">      </span><span style=\"color: #0000FF\">let</span><span style=\"color: #000000\"> pickedSuit = Math.floor(pickedCard / </span><span style=\"color: #09835A\">13</span><span style=\"color: #000000\">);</span>\n\n<span style=\"color: #000000\">      </span><span style=\"color: #0000FF\">return</span><span style=\"color: #000000\"> { suit: </span><span style=\"color: #0000FF\">this</span><span style=\"color: #000000\">.suits[pickedSuit], card: pickedCard % </span><span style=\"color: #09835A\">13</span><span style=\"color: #000000\"> };</span>\n<span style=\"color: #000000\">    };</span>\n<span style=\"color: #000000\">  }</span>\n<span style=\"color: #000000\">};</span>\n\n<span style=\"color: #0000FF\">let</span><span style=\"color: #000000\"> cardPicker = deck.createCardPicker();</span>\n<span style=\"color: #0000FF\">let</span><span style=\"color: #000000\"> pickedCard = cardPicker();</span>\n\n<span style=\"color: #000000\">alert(</span><span style=\"color: #A31515\">\"card: \"</span><span style=\"color: #000000\"> + pickedCard.card + </span><span style=\"color: #A31515\">\" of \"</span><span style=\"color: #000000\"> + pickedCard.suit);</span></code></div></pre>\n<p>Notice that <code>createCardPicker</code> is a function that itself returns a function.\nIf we tried to run the example, we would get an error instead of the expected alert box.\nThis is because the <code>this</code> being used in the function created by <code>createCardPicker</code> will be set to <code>window</code> instead of our <code>deck</code> object.\nThat’s because we call <code>cardPicker()</code> on its own.\nA top-level non-method syntax call like this will use <code>window</code> for <code>this</code>.\n(Note: under strict mode, <code>this</code> will be <code>undefined</code> rather than <code>window</code>).</p>\n<p>We can fix this by making sure the function is bound to the correct <code>this</code> before we return the function to be used later.\nThis way, regardless of how it’s later used, it will still be able to see the original <code>deck</code> object.\nTo do this, we change the function expression to use the ECMAScript 6 arrow syntax.\nArrow functions capture the <code>this</code> where the function is created rather than where it is invoked:</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\"> deck = {</span>\n<span style=\"color: #000000\">  suits: [</span><span style=\"color: #A31515\">\"hearts\"</span><span style=\"color: #000000\">, </span><span style=\"color: #A31515\">\"spades\"</span><span style=\"color: #000000\">, </span><span style=\"color: #A31515\">\"clubs\"</span><span style=\"color: #000000\">, </span><span style=\"color: #A31515\">\"diamonds\"</span><span style=\"color: #000000\">],</span>\n<span style=\"color: #000000\">  cards: Array(</span><span style=\"color: #09835A\">52</span><span style=\"color: #000000\">),</span>\n<span style=\"color: #000000\">  createCardPicker: </span><span style=\"color: #0000FF\">function</span><span style=\"color: #000000\">() {</span>\n<span style=\"color: #000000\">    </span><span style=\"color: #008000\">// NOTE: the line below is now an arrow function, allowing us to capture 'this' right here</span>\n<span style=\"color: #000000\">    </span><span style=\"color: #0000FF\">return</span><span style=\"color: #000000\"> () </span><span style=\"color: #0000FF\">=&gt;</span><span style=\"color: #000000\"> {</span>\n<span style=\"color: #000000\">      </span><span style=\"color: #0000FF\">let</span><span style=\"color: #000000\"> pickedCard = Math.floor(Math.random() * </span><span style=\"color: #09835A\">52</span><span style=\"color: #000000\">);</span>\n<span style=\"color: #000000\">      </span><span style=\"color: #0000FF\">let</span><span style=\"color: #000000\"> pickedSuit = Math.floor(pickedCard / </span><span style=\"color: #09835A\">13</span><span style=\"color: #000000\">);</span>\n\n<span style=\"color: #000000\">      </span><span style=\"color: #0000FF\">return</span><span style=\"color: #000000\"> { suit: </span><span style=\"color: #0000FF\">this</span><span style=\"color: #000000\">.suits[pickedSuit], card: pickedCard % </span><span style=\"color: #09835A\">13</span><span style=\"color: #000000\"> };</span>\n<span style=\"color: #000000\">    };</span>\n<span style=\"color: #000000\">  }</span>\n<span style=\"color: #000000\">};</span>\n\n<span style=\"color: #0000FF\">let</span><span style=\"color: #000000\"> cardPicker = deck.createCardPicker();</span>\n<span style=\"color: #0000FF\">let</span><span style=\"color: #000000\"> pickedCard = cardPicker();</span>\n\n<span style=\"color: #000000\">alert(</span><span style=\"color: #A31515\">\"card: \"</span><span style=\"color: #000000\"> + pickedCard.card + </span><span style=\"color: #A31515\">\" of \"</span><span style=\"color: #000000\"> + pickedCard.suit);</span></code></div></pre>\n<p>Even better, TypeScript will warn you when you make this mistake if you pass the <code>--noImplicitThis</code> flag to the compiler.\nIt will point out that <code>this</code> in <code>this.suits[pickedSuit]</code> is of type <code>any</code>.</p>\n<h2 id=\"this-parameters\" style=\"position:relative;\"><a href=\"#this-parameters\" aria-label=\"this parameters 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><code>this</code> parameters</h2>\n<p>Unfortunately, the type of <code>this.suits[pickedSuit]</code> is still <code>any</code>.\nThat’s because <code>this</code> comes from the function expression inside the object literal.\nTo fix this, you can provide an explicit <code>this</code> parameter.\n<code>this</code> parameters are fake parameters that come first in the parameter list of a function:</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\"> f(this: void) {</span>\n<span style=\"color: #000000\">  </span><span style=\"color: #008000\">// make sure `this` is unusable in this standalone function</span>\n<span style=\"color: #000000\">}</span></code></div></pre>\n<p>Let’s add a couple of interfaces to our example above, <code>Card</code> and <code>Deck</code>, to make the types clearer and easier to reuse:</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\"> Card {</span>\n<span style=\"color: #000000\">  suit: string;</span>\n<span style=\"color: #000000\">  card: number;</span>\n<span style=\"color: #000000\">}</span>\n<span style=\"color: #0000FF\">interface</span><span style=\"color: #000000\"> Deck {</span>\n<span style=\"color: #000000\">  suits: string[];</span>\n<span style=\"color: #000000\">  cards: number[];</span>\n<span style=\"color: #000000\">  createCardPicker(this: Deck): () </span><span style=\"color: #0000FF\">=&gt;</span><span style=\"color: #000000\"> Card;</span>\n<span style=\"color: #000000\">}</span>\n<span style=\"color: #0000FF\">let</span><span style=\"color: #000000\"> deck: Deck = {</span>\n<span style=\"color: #000000\">  suits: [</span><span style=\"color: #A31515\">\"hearts\"</span><span style=\"color: #000000\">, </span><span style=\"color: #A31515\">\"spades\"</span><span style=\"color: #000000\">, </span><span style=\"color: #A31515\">\"clubs\"</span><span style=\"color: #000000\">, </span><span style=\"color: #A31515\">\"diamonds\"</span><span style=\"color: #000000\">],</span>\n<span style=\"color: #000000\">  cards: Array(</span><span style=\"color: #09835A\">52</span><span style=\"color: #000000\">),</span>\n<span style=\"color: #000000\">  </span><span style=\"color: #008000\">// NOTE: The function now explicitly specifies that its callee must be of type Deck</span>\n<span style=\"color: #000000\">  createCardPicker: </span><span style=\"color: #0000FF\">function</span><span style=\"color: #000000\">(this: Deck) {</span>\n<span style=\"color: #000000\">    </span><span style=\"color: #0000FF\">return</span><span style=\"color: #000000\"> () </span><span style=\"color: #0000FF\">=&gt;</span><span style=\"color: #000000\"> {</span>\n<span style=\"color: #000000\">      </span><span style=\"color: #0000FF\">let</span><span style=\"color: #000000\"> pickedCard = Math.floor(Math.random() * </span><span style=\"color: #09835A\">52</span><span style=\"color: #000000\">);</span>\n<span style=\"color: #000000\">      </span><span style=\"color: #0000FF\">let</span><span style=\"color: #000000\"> pickedSuit = Math.floor(pickedCard / </span><span style=\"color: #09835A\">13</span><span style=\"color: #000000\">);</span>\n\n<span style=\"color: #000000\">      </span><span style=\"color: #0000FF\">return</span><span style=\"color: #000000\"> { suit: </span><span style=\"color: #0000FF\">this</span><span style=\"color: #000000\">.suits[pickedSuit], card: pickedCard % </span><span style=\"color: #09835A\">13</span><span style=\"color: #000000\"> };</span>\n<span style=\"color: #000000\">    };</span>\n<span style=\"color: #000000\">  }</span>\n<span style=\"color: #000000\">};</span>\n\n<span style=\"color: #0000FF\">let</span><span style=\"color: #000000\"> cardPicker = deck.createCardPicker();</span>\n<span style=\"color: #0000FF\">let</span><span style=\"color: #000000\"> pickedCard = cardPicker();</span>\n\n<span style=\"color: #000000\">alert(</span><span style=\"color: #A31515\">\"card: \"</span><span style=\"color: #000000\"> + pickedCard.card + </span><span style=\"color: #A31515\">\" of \"</span><span style=\"color: #000000\"> + pickedCard.suit);</span></code></div></pre>\n<p>Now TypeScript knows that <code>createCardPicker</code> expects to be called on a <code>Deck</code> object.\nThat means that <code>this</code> is of type <code>Deck</code> now, not <code>any</code>, so <code>--noImplicitThis</code> will not cause any errors.</p>\n<h3 id=\"this-parameters-in-callbacks\" style=\"position:relative;\"><a href=\"#this-parameters-in-callbacks\" aria-label=\"this parameters in callbacks 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><code>this</code> parameters in callbacks</h3>\n<p>You can also run into errors with <code>this</code> in callbacks, when you pass functions to a library that will later call them.\nBecause the library that calls your callback will call it like a normal function, <code>this</code> will be <code>undefined</code>.\nWith some work you can use <code>this</code> parameters to prevent errors with callbacks too.\nFirst, the library author needs to annotate the callback type with <code>this</code>:</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\"> UIElement {</span>\n<span style=\"color: #000000\">  addClickListener(onclick: (this: void, e: Event) </span><span style=\"color: #0000FF\">=&gt;</span><span style=\"color: #000000\"> void): void;</span>\n<span style=\"color: #000000\">}</span></code></div></pre>\n<p><code>this: void</code> means that <code>addClickListener</code> expects <code>onclick</code> to be a function that does not require a <code>this</code> type.\nSecond, annotate your calling code with <code>this</code>:</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\"> Handler {</span>\n<span style=\"color: #000000\">  info: string;</span>\n<span style=\"color: #000000\">  onClickBad(this: Handler, e: Event) {</span>\n<span style=\"color: #000000\">    </span><span style=\"color: #008000\">// oops, used `this` here. using this callback would crash at runtime</span>\n<span style=\"color: #000000\">    </span><span style=\"color: #0000FF\">this</span><span style=\"color: #000000\">.info = e.message;</span>\n<span style=\"color: #000000\">  }</span>\n<span style=\"color: #000000\">}</span>\n<span style=\"color: #0000FF\">let</span><span style=\"color: #000000\"> h = </span><span style=\"color: #0000FF\">new</span><span style=\"color: #000000\"> Handler();</span>\n<span style=\"color: #000000\">uiElement.addClickListener(h.onClickBad); </span><span style=\"color: #008000\">// error!</span></code></div></pre>\n<p>With <code>this</code> annotated, you make it explicit that <code>onClickBad</code> must be called on an instance of <code>Handler</code>.\nThen TypeScript will detect that <code>addClickListener</code> requires a function that has <code>this: void</code>.\nTo fix the error, change the type of <code>this</code>:</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\"> Handler {</span>\n<span style=\"color: #000000\">  info: string;</span>\n<span style=\"color: #000000\">  onClickGood(this: void, e: Event) {</span>\n<span style=\"color: #000000\">    </span><span style=\"color: #008000\">// can't use `this` here because it's of type void!</span>\n<span style=\"color: #000000\">    console.log(</span><span style=\"color: #A31515\">\"clicked!\"</span><span style=\"color: #000000\">);</span>\n<span style=\"color: #000000\">  }</span>\n<span style=\"color: #000000\">}</span>\n<span style=\"color: #0000FF\">let</span><span style=\"color: #000000\"> h = </span><span style=\"color: #0000FF\">new</span><span style=\"color: #000000\"> Handler();</span>\n<span style=\"color: #000000\">uiElement.addClickListener(h.onClickGood);</span></code></div></pre>\n<p>Because <code>onClickGood</code> specifies its <code>this</code> type as <code>void</code>, it is legal to pass to <code>addClickListener</code>.\nOf course, this also means that it can’t use <code>this.info</code>.\nIf you want both then you’ll have to use an arrow function:</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\"> Handler {</span>\n<span style=\"color: #000000\">  info: string;</span>\n<span style=\"color: #000000\">  onClickGood = (e: Event) </span><span style=\"color: #0000FF\">=&gt;</span><span style=\"color: #000000\"> {</span>\n<span style=\"color: #000000\">    </span><span style=\"color: #0000FF\">this</span><span style=\"color: #000000\">.info = e.message;</span>\n<span style=\"color: #000000\">  };</span>\n<span style=\"color: #000000\">}</span></code></div></pre>\n<p>This works because arrow functions use the outer <code>this</code>, so you can always pass them to something that expects <code>this: void</code>.\nThe downside is that one arrow function is created per object of type Handler.\nMethods, on the other hand, are only created once and attached to Handler’s prototype.\nThey are shared between all objects of type Handler.</p>\n<h1 id=\"overloads\" style=\"position:relative;\"><a href=\"#overloads\" aria-label=\"overloads 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>Overloads</h1>\n<p>JavaScript is inherently a very dynamic language.\nIt’s not uncommon for a single JavaScript function to return different types of objects based on the shape of the arguments passed 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\"> suits = [</span><span style=\"color: #A31515\">\"hearts\"</span><span style=\"color: #000000\">, </span><span style=\"color: #A31515\">\"spades\"</span><span style=\"color: #000000\">, </span><span style=\"color: #A31515\">\"clubs\"</span><span style=\"color: #000000\">, </span><span style=\"color: #A31515\">\"diamonds\"</span><span style=\"color: #000000\">];</span>\n\n<span style=\"color: #0000FF\">function</span><span style=\"color: #000000\"> pickCard(x): any {</span>\n<span style=\"color: #000000\">  </span><span style=\"color: #008000\">// Check to see if we're working with an object/array</span>\n<span style=\"color: #000000\">  </span><span style=\"color: #008000\">// if so, they gave us the deck and we'll pick the card</span>\n<span style=\"color: #000000\">  </span><span style=\"color: #0000FF\">if</span><span style=\"color: #000000\"> (</span><span style=\"color: #0000FF\">typeof</span><span style=\"color: #000000\"> x == </span><span style=\"color: #A31515\">\"object\"</span><span style=\"color: #000000\">) {</span>\n<span style=\"color: #000000\">    </span><span style=\"color: #0000FF\">let</span><span style=\"color: #000000\"> pickedCard = Math.floor(Math.random() * x.length);</span>\n<span style=\"color: #000000\">    </span><span style=\"color: #0000FF\">return</span><span style=\"color: #000000\"> pickedCard;</span>\n<span style=\"color: #000000\">  }</span>\n<span style=\"color: #000000\">  </span><span style=\"color: #008000\">// Otherwise just let them pick the card</span>\n<span style=\"color: #000000\">  </span><span style=\"color: #0000FF\">else</span><span style=\"color: #000000\"> </span><span style=\"color: #0000FF\">if</span><span style=\"color: #000000\"> (</span><span style=\"color: #0000FF\">typeof</span><span style=\"color: #000000\"> x == </span><span style=\"color: #A31515\">\"number\"</span><span style=\"color: #000000\">) {</span>\n<span style=\"color: #000000\">    </span><span style=\"color: #0000FF\">let</span><span style=\"color: #000000\"> pickedSuit = Math.floor(x / </span><span style=\"color: #09835A\">13</span><span style=\"color: #000000\">);</span>\n<span style=\"color: #000000\">    </span><span style=\"color: #0000FF\">return</span><span style=\"color: #000000\"> { suit: suits[pickedSuit], card: x % </span><span style=\"color: #09835A\">13</span><span style=\"color: #000000\"> };</span>\n<span style=\"color: #000000\">  }</span>\n<span style=\"color: #000000\">}</span>\n\n<span style=\"color: #0000FF\">let</span><span style=\"color: #000000\"> myDeck = [</span>\n<span style=\"color: #000000\">  { suit: </span><span style=\"color: #A31515\">\"diamonds\"</span><span style=\"color: #000000\">, card: </span><span style=\"color: #09835A\">2</span><span style=\"color: #000000\"> },</span>\n<span style=\"color: #000000\">  { suit: </span><span style=\"color: #A31515\">\"spades\"</span><span style=\"color: #000000\">, card: </span><span style=\"color: #09835A\">10</span><span style=\"color: #000000\"> },</span>\n<span style=\"color: #000000\">  { suit: </span><span style=\"color: #A31515\">\"hearts\"</span><span style=\"color: #000000\">, card: </span><span style=\"color: #09835A\">4</span><span style=\"color: #000000\"> }</span>\n<span style=\"color: #000000\">];</span>\n<span style=\"color: #0000FF\">let</span><span style=\"color: #000000\"> pickedCard1 = myDeck[pickCard(myDeck)];</span>\n<span style=\"color: #000000\">alert(</span><span style=\"color: #A31515\">\"card: \"</span><span style=\"color: #000000\"> + pickedCard1.card + </span><span style=\"color: #A31515\">\" of \"</span><span style=\"color: #000000\"> + pickedCard1.suit);</span>\n\n<span style=\"color: #0000FF\">let</span><span style=\"color: #000000\"> pickedCard2 = pickCard(</span><span style=\"color: #09835A\">15</span><span style=\"color: #000000\">);</span>\n<span style=\"color: #000000\">alert(</span><span style=\"color: #A31515\">\"card: \"</span><span style=\"color: #000000\"> + pickedCard2.card + </span><span style=\"color: #A31515\">\" of \"</span><span style=\"color: #000000\"> + pickedCard2.suit);</span></code></div></pre>\n<p>Here, the <code>pickCard</code> function will return two different things based on what the user has passed in.\nIf the users passes in an object that represents the deck, the function will pick the card.\nIf the user picks the card, we tell them which card they’ve picked.\nBut how do we describe this to the type system?</p>\n<p>The answer is to supply multiple function types for the same function as a list of overloads.\nThis list is what the compiler will use to resolve function calls.\nLet’s create a list of overloads that describe what our <code>pickCard</code> accepts and what it returns.</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\"> suits = [</span><span style=\"color: #A31515\">\"hearts\"</span><span style=\"color: #000000\">, </span><span style=\"color: #A31515\">\"spades\"</span><span style=\"color: #000000\">, </span><span style=\"color: #A31515\">\"clubs\"</span><span style=\"color: #000000\">, </span><span style=\"color: #A31515\">\"diamonds\"</span><span style=\"color: #000000\">];</span>\n\n<span style=\"color: #0000FF\">function</span><span style=\"color: #000000\"> pickCard(x: { suit: string; card: number }[]): number;</span>\n<span style=\"color: #0000FF\">function</span><span style=\"color: #000000\"> pickCard(x: number): { suit: string; card: number };</span>\n<span style=\"color: #0000FF\">function</span><span style=\"color: #000000\"> pickCard(x): any {</span>\n<span style=\"color: #000000\">  </span><span style=\"color: #008000\">// Check to see if we're working with an object/array</span>\n<span style=\"color: #000000\">  </span><span style=\"color: #008000\">// if so, they gave us the deck and we'll pick the card</span>\n<span style=\"color: #000000\">  </span><span style=\"color: #0000FF\">if</span><span style=\"color: #000000\"> (</span><span style=\"color: #0000FF\">typeof</span><span style=\"color: #000000\"> x == </span><span style=\"color: #A31515\">\"object\"</span><span style=\"color: #000000\">) {</span>\n<span style=\"color: #000000\">    </span><span style=\"color: #0000FF\">let</span><span style=\"color: #000000\"> pickedCard = Math.floor(Math.random() * x.length);</span>\n<span style=\"color: #000000\">    </span><span style=\"color: #0000FF\">return</span><span style=\"color: #000000\"> pickedCard;</span>\n<span style=\"color: #000000\">  }</span>\n<span style=\"color: #000000\">  </span><span style=\"color: #008000\">// Otherwise just let them pick the card</span>\n<span style=\"color: #000000\">  </span><span style=\"color: #0000FF\">else</span><span style=\"color: #000000\"> </span><span style=\"color: #0000FF\">if</span><span style=\"color: #000000\"> (</span><span style=\"color: #0000FF\">typeof</span><span style=\"color: #000000\"> x == </span><span style=\"color: #A31515\">\"number\"</span><span style=\"color: #000000\">) {</span>\n<span style=\"color: #000000\">    </span><span style=\"color: #0000FF\">let</span><span style=\"color: #000000\"> pickedSuit = Math.floor(x / </span><span style=\"color: #09835A\">13</span><span style=\"color: #000000\">);</span>\n<span style=\"color: #000000\">    </span><span style=\"color: #0000FF\">return</span><span style=\"color: #000000\"> { suit: suits[pickedSuit], card: x % </span><span style=\"color: #09835A\">13</span><span style=\"color: #000000\"> };</span>\n<span style=\"color: #000000\">  }</span>\n<span style=\"color: #000000\">}</span>\n\n<span style=\"color: #0000FF\">let</span><span style=\"color: #000000\"> myDeck = [</span>\n<span style=\"color: #000000\">  { suit: </span><span style=\"color: #A31515\">\"diamonds\"</span><span style=\"color: #000000\">, card: </span><span style=\"color: #09835A\">2</span><span style=\"color: #000000\"> },</span>\n<span style=\"color: #000000\">  { suit: </span><span style=\"color: #A31515\">\"spades\"</span><span style=\"color: #000000\">, card: </span><span style=\"color: #09835A\">10</span><span style=\"color: #000000\"> },</span>\n<span style=\"color: #000000\">  { suit: </span><span style=\"color: #A31515\">\"hearts\"</span><span style=\"color: #000000\">, card: </span><span style=\"color: #09835A\">4</span><span style=\"color: #000000\"> }</span>\n<span style=\"color: #000000\">];</span>\n<span style=\"color: #0000FF\">let</span><span style=\"color: #000000\"> pickedCard1 = myDeck[pickCard(myDeck)];</span>\n<span style=\"color: #000000\">alert(</span><span style=\"color: #A31515\">\"card: \"</span><span style=\"color: #000000\"> + pickedCard1.card + </span><span style=\"color: #A31515\">\" of \"</span><span style=\"color: #000000\"> + pickedCard1.suit);</span>\n\n<span style=\"color: #0000FF\">let</span><span style=\"color: #000000\"> pickedCard2 = pickCard(</span><span style=\"color: #09835A\">15</span><span style=\"color: #000000\">);</span>\n<span style=\"color: #000000\">alert(</span><span style=\"color: #A31515\">\"card: \"</span><span style=\"color: #000000\"> + pickedCard2.card + </span><span style=\"color: #A31515\">\" of \"</span><span style=\"color: #000000\"> + pickedCard2.suit);</span></code></div></pre>\n<p>With this change, the overloads now give us type checked calls to the <code>pickCard</code> function.</p>\n<p>In order for the compiler to pick the correct type check, it follows a similar process to the underlying JavaScript.\nIt looks at the overload list and, proceeding with the first overload, attempts to call the function with the provided parameters.\nIf it finds a match, it picks this overload as the correct overload.\nFor this reason, it’s customary to order overloads from most specific to least specific.</p>\n<p>Note that the <code>function pickCard(x): any</code> piece is not part of the overload list, so it only has two overloads: one that takes an object and one that takes a number.\nCalling <code>pickCard</code> with any other parameter types would cause an error.</p>","headings":[{"value":"Introduction","depth":1},{"value":"Functions","depth":1},{"value":"Function Types","depth":1},{"value":"Typing the function","depth":2},{"value":"Writing the function type","depth":2},{"value":"Inferring the types","depth":2},{"value":"Optional and Default Parameters","depth":1},{"value":"Rest Parameters","depth":1},{"value":"this","depth":1},{"value":"this and arrow functions","depth":2},{"value":"this parameters","depth":2},{"value":"this parameters in callbacks","depth":3},{"value":"Overloads","depth":1}],"frontmatter":{"permalink":"/docs/handbook/functions.html","title":"Functions"}}},"pageContext":{"slug":"/docs/handbook/functions.html","isOldHandbook":true}}}