{"componentChunkName":"component---src-templates-handbook-tsx","path":"/docs/handbook/classes.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":"ed6bbdf4-4846-5f1b-8185-455ceeb4b33c","excerpt":"Introduction Traditional JavaScript uses functions and prototype-based inheritance to build up reusable components, but this may feel a bit awkward to…","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>Traditional JavaScript uses functions and prototype-based inheritance to build up reusable components, but this may feel a bit awkward to programmers more comfortable with an object-oriented approach, where classes inherit functionality and objects are built from these classes.\nStarting with ECMAScript 2015, also known as ECMAScript 6, JavaScript programmers will be able to build their applications using this object-oriented class-based approach.\nIn TypeScript, we allow developers to use these techniques now, and compile them down to JavaScript that works across all major browsers and platforms, without having to wait for the next version of JavaScript.</p>\n<h1 id=\"classes\" style=\"position:relative;\"><a href=\"#classes\" aria-label=\"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>Classes</h1>\n<p>Let’s take a look at a simple class-based example:</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\"> Greeter {</span>\n<span style=\"color: #000000\">  greeting: string;</span>\n<span style=\"color: #000000\">  </span><span style=\"color: #0000FF\">constructor</span><span style=\"color: #000000\">(message: string) {</span>\n<span style=\"color: #000000\">    </span><span style=\"color: #0000FF\">this</span><span style=\"color: #000000\">.greeting = message;</span>\n<span style=\"color: #000000\">  }</span>\n<span style=\"color: #000000\">  greet() {</span>\n<span style=\"color: #000000\">    </span><span style=\"color: #0000FF\">return</span><span style=\"color: #000000\"> </span><span style=\"color: #A31515\">\"Hello, \"</span><span style=\"color: #000000\"> + </span><span style=\"color: #0000FF\">this</span><span style=\"color: #000000\">.greeting;</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\"> greeter = </span><span style=\"color: #0000FF\">new</span><span style=\"color: #000000\"> Greeter(</span><span style=\"color: #A31515\">\"world\"</span><span style=\"color: #000000\">);</span></code></div></pre>\n<p>The syntax should look familiar if you’ve used C# or Java before.\nWe declare a new class <code>Greeter</code>. This class has three members: a property called <code>greeting</code>, a constructor, and a method <code>greet</code>.</p>\n<p>You’ll notice that in the class when we refer to one of the members of the class we prepend <code>this.</code>.\nThis denotes that it’s a member access.</p>\n<p>In the last line we construct an instance of the <code>Greeter</code> class using <code>new</code>.\nThis calls into the constructor we defined earlier, creating a new object with the <code>Greeter</code> shape, and running the constructor to initialize it.</p>\n<h1 id=\"inheritance\" style=\"position:relative;\"><a href=\"#inheritance\" aria-label=\"inheritance 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>Inheritance</h1>\n<p>In TypeScript, we can use common object-oriented patterns.\nOne of the most fundamental patterns in class-based programming is being able to extend existing classes to create new ones using inheritance.</p>\n<p>Let’s take a look at an example:</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\"> Animal {</span>\n<span style=\"color: #000000\">  move(distanceInMeters: number = </span><span style=\"color: #09835A\">0</span><span style=\"color: #000000\">) {</span>\n<span style=\"color: #000000\">    console.log(</span><span style=\"color: #A31515\">`Animal moved </span><span style=\"color: #0000FF\">${</span><span style=\"color: #A31515\">distanceInMeters</span><span style=\"color: #0000FF\">}</span><span style=\"color: #A31515\">m.`</span><span style=\"color: #000000\">);</span>\n<span style=\"color: #000000\">  }</span>\n<span style=\"color: #000000\">}</span>\n\n<span style=\"color: #0000FF\">class</span><span style=\"color: #000000\"> Dog </span><span style=\"color: #0000FF\">extends</span><span style=\"color: #000000\"> Animal {</span>\n<span style=\"color: #000000\">  bark() {</span>\n<span style=\"color: #000000\">    console.log(</span><span style=\"color: #A31515\">\"Woof! Woof!\"</span><span style=\"color: #000000\">);</span>\n<span style=\"color: #000000\">  }</span>\n<span style=\"color: #000000\">}</span>\n\n<span style=\"color: #0000FF\">const</span><span style=\"color: #000000\"> dog = </span><span style=\"color: #0000FF\">new</span><span style=\"color: #000000\"> Dog();</span>\n<span style=\"color: #000000\">dog.bark();</span>\n<span style=\"color: #000000\">dog.move(</span><span style=\"color: #09835A\">10</span><span style=\"color: #000000\">);</span>\n<span style=\"color: #000000\">dog.bark();</span></code></div></pre>\n<p>This example shows the most basic inheritance feature: classes inherit properties and methods from base classes.\nHere, <code>Dog</code> is a <em>derived</em> class that derives from the <code>Animal</code> <em>base</em> class using the <code>extends</code> keyword.\nDerived classes are often called <em>subclasses</em>, and base classes are often called <em>superclasses</em>.</p>\n<p>Because <code>Dog</code> extends the functionality from <code>Animal</code>, we were able to create an instance of <code>Dog</code> that could both <code>bark()</code> and <code>move()</code>.</p>\n<p>Let’s now look at a more complex example.</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\"> Animal {</span>\n<span style=\"color: #000000\">  name: string;</span>\n<span style=\"color: #000000\">  </span><span style=\"color: #0000FF\">constructor</span><span style=\"color: #000000\">(theName: string) {</span>\n<span style=\"color: #000000\">    </span><span style=\"color: #0000FF\">this</span><span style=\"color: #000000\">.name = theName;</span>\n<span style=\"color: #000000\">  }</span>\n<span style=\"color: #000000\">  move(distanceInMeters: number = </span><span style=\"color: #09835A\">0</span><span style=\"color: #000000\">) {</span>\n<span style=\"color: #000000\">    console.log(</span><span style=\"color: #A31515\">`</span><span style=\"color: #0000FF\">${this</span><span style=\"color: #A31515\">.name</span><span style=\"color: #0000FF\">}</span><span style=\"color: #A31515\"> moved </span><span style=\"color: #0000FF\">${</span><span style=\"color: #A31515\">distanceInMeters</span><span style=\"color: #0000FF\">}</span><span style=\"color: #A31515\">m.`</span><span style=\"color: #000000\">);</span>\n<span style=\"color: #000000\">  }</span>\n<span style=\"color: #000000\">}</span>\n\n<span style=\"color: #0000FF\">class</span><span style=\"color: #000000\"> Snake </span><span style=\"color: #0000FF\">extends</span><span style=\"color: #000000\"> Animal {</span>\n<span style=\"color: #000000\">  </span><span style=\"color: #0000FF\">constructor</span><span style=\"color: #000000\">(name: string) {</span>\n<span style=\"color: #000000\">    </span><span style=\"color: #0000FF\">super</span><span style=\"color: #000000\">(name);</span>\n<span style=\"color: #000000\">  }</span>\n<span style=\"color: #000000\">  move(distanceInMeters = </span><span style=\"color: #09835A\">5</span><span style=\"color: #000000\">) {</span>\n<span style=\"color: #000000\">    console.log(</span><span style=\"color: #A31515\">\"Slithering...\"</span><span style=\"color: #000000\">);</span>\n<span style=\"color: #000000\">    </span><span style=\"color: #0000FF\">super</span><span style=\"color: #000000\">.move(distanceInMeters);</span>\n<span style=\"color: #000000\">  }</span>\n<span style=\"color: #000000\">}</span>\n\n<span style=\"color: #0000FF\">class</span><span style=\"color: #000000\"> Horse </span><span style=\"color: #0000FF\">extends</span><span style=\"color: #000000\"> Animal {</span>\n<span style=\"color: #000000\">  </span><span style=\"color: #0000FF\">constructor</span><span style=\"color: #000000\">(name: string) {</span>\n<span style=\"color: #000000\">    </span><span style=\"color: #0000FF\">super</span><span style=\"color: #000000\">(name);</span>\n<span style=\"color: #000000\">  }</span>\n<span style=\"color: #000000\">  move(distanceInMeters = </span><span style=\"color: #09835A\">45</span><span style=\"color: #000000\">) {</span>\n<span style=\"color: #000000\">    console.log(</span><span style=\"color: #A31515\">\"Galloping...\"</span><span style=\"color: #000000\">);</span>\n<span style=\"color: #000000\">    </span><span style=\"color: #0000FF\">super</span><span style=\"color: #000000\">.move(distanceInMeters);</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\"> sam = </span><span style=\"color: #0000FF\">new</span><span style=\"color: #000000\"> Snake(</span><span style=\"color: #A31515\">\"Sammy the Python\"</span><span style=\"color: #000000\">);</span>\n<span style=\"color: #0000FF\">let</span><span style=\"color: #000000\"> tom: Animal = </span><span style=\"color: #0000FF\">new</span><span style=\"color: #000000\"> Horse(</span><span style=\"color: #A31515\">\"Tommy the Palomino\"</span><span style=\"color: #000000\">);</span>\n\n<span style=\"color: #000000\">sam.move();</span>\n<span style=\"color: #000000\">tom.move(</span><span style=\"color: #09835A\">34</span><span style=\"color: #000000\">);</span></code></div></pre>\n<p>This example covers a few other features we didn’t previously mention.\nAgain, we see the <code>extends</code> keywords used to create two new subclasses of <code>Animal</code>: <code>Horse</code> and <code>Snake</code>.</p>\n<p>One difference from the prior example is that each derived class that contains a constructor function <em>must</em> call <code>super()</code> which will execute the constructor of the base class.\nWhat’s more, before we <em>ever</em> access a property on <code>this</code> in a constructor body, we <em>have</em> to call <code>super()</code>.\nThis is an important rule that TypeScript will enforce.</p>\n<p>The example also shows how to override methods in the base class with methods that are specialized for the subclass.\nHere both <code>Snake</code> and <code>Horse</code> create a <code>move</code> method that overrides the <code>move</code> from <code>Animal</code>, giving it functionality specific to each class.\nNote that even though <code>tom</code> is declared as an <code>Animal</code>, since its value is a <code>Horse</code>, calling <code>tom.move(34)</code> will call the overriding method in <code>Horse</code>:</p>\n<pre><code class=\"language-Text\">Slithering...\nSammy the Python moved 5m.\nGalloping...\nTommy the Palomino moved 34m.\n</code></pre>\n<h1 id=\"public-private-and-protected-modifiers\" style=\"position:relative;\"><a href=\"#public-private-and-protected-modifiers\" aria-label=\"public private and protected modifiers 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>Public, private, and protected modifiers</h1>\n<h2 id=\"public-by-default\" style=\"position:relative;\"><a href=\"#public-by-default\" aria-label=\"public by default 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>Public by default</h2>\n<p>In our examples, we’ve been able to freely access the members that we declared throughout our programs.\nIf you’re familiar with classes in other languages, you may have noticed in the above examples we haven’t had to use the word <code>public</code> to accomplish this; for instance, C# requires that each member be explicitly labeled <code>public</code> to be visible.\nIn TypeScript, each member is <code>public</code> by default.</p>\n<p>You may still mark a member <code>public</code> explicitly.\nWe could have written the <code>Animal</code> class from the previous section in the following way:</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\"> Animal {</span>\n<span style=\"color: #000000\">  </span><span style=\"color: #0000FF\">public</span><span style=\"color: #000000\"> name: string;</span>\n<span style=\"color: #000000\">  </span><span style=\"color: #0000FF\">public</span><span style=\"color: #000000\"> </span><span style=\"color: #0000FF\">constructor</span><span style=\"color: #000000\">(theName: string) {</span>\n<span style=\"color: #000000\">    </span><span style=\"color: #0000FF\">this</span><span style=\"color: #000000\">.name = theName;</span>\n<span style=\"color: #000000\">  }</span>\n<span style=\"color: #000000\">  </span><span style=\"color: #0000FF\">public</span><span style=\"color: #000000\"> move(distanceInMeters: number) {</span>\n<span style=\"color: #000000\">    console.log(</span><span style=\"color: #A31515\">`</span><span style=\"color: #0000FF\">${this</span><span style=\"color: #A31515\">.name</span><span style=\"color: #0000FF\">}</span><span style=\"color: #A31515\"> moved </span><span style=\"color: #0000FF\">${</span><span style=\"color: #A31515\">distanceInMeters</span><span style=\"color: #0000FF\">}</span><span style=\"color: #A31515\">m.`</span><span style=\"color: #000000\">);</span>\n<span style=\"color: #000000\">  }</span>\n<span style=\"color: #000000\">}</span></code></div></pre>\n<h2 id=\"ecmascript-private-fields\" style=\"position:relative;\"><a href=\"#ecmascript-private-fields\" aria-label=\"ecmascript private fields 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>ECMAScript Private Fields</h2>\n<p>With TypeScript 3.8, TypeScript supports the new JavaScript syntax for private fields:</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\"> Animal {</span>\n<span style=\"color: #000000\">    #name: string;</span>\n<span style=\"color: #000000\">    </span><span style=\"color: #0000FF\">constructor</span><span style=\"color: #000000\">(theName: string) { </span><span style=\"color: #0000FF\">this</span><span style=\"color: #000000\">.#name = theName; }</span>\n<span style=\"color: #000000\">}</span>\n\n<span style=\"color: #0000FF\">new</span><span style=\"color: #000000\"> Animal(</span><span style=\"color: #A31515\">\"Cat\"</span><span style=\"color: #000000\">).#name; </span><span style=\"color: #008000\">// Property '#name' is not accessible outside class 'Animal' because it has a private identifier.</span></code></div></pre>\n<p>This syntax is built into the JavaScript runtime and can have better guarantees about the isolation of each private field.\nRight now, the best documentation for these private fields is in the TypeScript 3.8 <a href=\"https://devblogs.microsoft.com/typescript/announcing-typescript-3-8-beta/#ecmascript-private-fields\">release notes</a>.</p>\n<h2 id=\"understanding-typescripts-private\" style=\"position:relative;\"><a href=\"#understanding-typescripts-private\" aria-label=\"understanding typescripts private 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>Understanding TypeScript’s <code>private</code></h2>\n<p>TypeScript also has it’s own way to declare a member as being marked <code>private</code>, it cannot be accessed from outside of its containing class. For example:</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\"> Animal {</span>\n<span style=\"color: #000000\">  </span><span style=\"color: #0000FF\">private</span><span style=\"color: #000000\"> name: string;</span>\n<span style=\"color: #000000\">  </span><span style=\"color: #0000FF\">constructor</span><span style=\"color: #000000\">(theName: string) {</span>\n<span style=\"color: #000000\">    </span><span style=\"color: #0000FF\">this</span><span style=\"color: #000000\">.name = theName;</span>\n<span style=\"color: #000000\">  }</span>\n<span style=\"color: #000000\">}</span>\n\n<span style=\"color: #0000FF\">new</span><span style=\"color: #000000\"> Animal(</span><span style=\"color: #A31515\">\"Cat\"</span><span style=\"color: #000000\">).name; </span><span style=\"color: #008000\">// Error: 'name' is private;</span></code></div></pre>\n<p>TypeScript is a structural type system.\nWhen we compare two different types, regardless of where they came from, if the types of all members are compatible, then we say the types themselves are compatible.</p>\n<p>However, when comparing types that have <code>private</code> and <code>protected</code> members, we treat these types differently.\nFor two types to be considered compatible, if one of them has a <code>private</code> member, then the other must have a <code>private</code> member that originated in the same declaration.\nThe same applies to <code>protected</code> members.</p>\n<p>Let’s look at an example to better see how this plays out in practice:</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\"> Animal {</span>\n<span style=\"color: #000000\">  </span><span style=\"color: #0000FF\">private</span><span style=\"color: #000000\"> name: string;</span>\n<span style=\"color: #000000\">  </span><span style=\"color: #0000FF\">constructor</span><span style=\"color: #000000\">(theName: string) {</span>\n<span style=\"color: #000000\">    </span><span style=\"color: #0000FF\">this</span><span style=\"color: #000000\">.name = theName;</span>\n<span style=\"color: #000000\">  }</span>\n<span style=\"color: #000000\">}</span>\n\n<span style=\"color: #0000FF\">class</span><span style=\"color: #000000\"> Rhino </span><span style=\"color: #0000FF\">extends</span><span style=\"color: #000000\"> Animal {</span>\n<span style=\"color: #000000\">  </span><span style=\"color: #0000FF\">constructor</span><span style=\"color: #000000\">() {</span>\n<span style=\"color: #000000\">    </span><span style=\"color: #0000FF\">super</span><span style=\"color: #000000\">(</span><span style=\"color: #A31515\">\"Rhino\"</span><span style=\"color: #000000\">);</span>\n<span style=\"color: #000000\">  }</span>\n<span style=\"color: #000000\">}</span>\n\n<span style=\"color: #0000FF\">class</span><span style=\"color: #000000\"> Employee {</span>\n<span style=\"color: #000000\">  </span><span style=\"color: #0000FF\">private</span><span style=\"color: #000000\"> name: string;</span>\n<span style=\"color: #000000\">  </span><span style=\"color: #0000FF\">constructor</span><span style=\"color: #000000\">(theName: string) {</span>\n<span style=\"color: #000000\">    </span><span style=\"color: #0000FF\">this</span><span style=\"color: #000000\">.name = theName;</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\"> animal = </span><span style=\"color: #0000FF\">new</span><span style=\"color: #000000\"> Animal(</span><span style=\"color: #A31515\">\"Goat\"</span><span style=\"color: #000000\">);</span>\n<span style=\"color: #0000FF\">let</span><span style=\"color: #000000\"> rhino = </span><span style=\"color: #0000FF\">new</span><span style=\"color: #000000\"> Rhino();</span>\n<span style=\"color: #0000FF\">let</span><span style=\"color: #000000\"> employee = </span><span style=\"color: #0000FF\">new</span><span style=\"color: #000000\"> Employee(</span><span style=\"color: #A31515\">\"Bob\"</span><span style=\"color: #000000\">);</span>\n\n<span style=\"color: #000000\">animal = rhino;</span>\n<span style=\"color: #000000\">animal = employee; </span><span style=\"color: #008000\">// Error: 'Animal' and 'Employee' are not compatible</span></code></div></pre>\n<p>In this example, we have an <code>Animal</code> and a <code>Rhino</code>, with <code>Rhino</code> being a subclass of <code>Animal</code>.\nWe also have a new class <code>Employee</code> that looks identical to <code>Animal</code> in terms of shape.\nWe create some instances of these classes and then try to assign them to each other to see what will happen.\nBecause <code>Animal</code> and <code>Rhino</code> share the <code>private</code> side of their shape from the same declaration of <code>private name: string</code> in <code>Animal</code>, they are compatible. However, this is not the case for <code>Employee</code>.\nWhen we try to assign from an <code>Employee</code> to <code>Animal</code> we get an error that these types are not compatible.\nEven though <code>Employee</code> also has a <code>private</code> member called <code>name</code>, it’s not the one we declared in <code>Animal</code>.</p>\n<h2 id=\"understanding-protected\" style=\"position:relative;\"><a href=\"#understanding-protected\" aria-label=\"understanding protected 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>Understanding <code>protected</code></h2>\n<p>The <code>protected</code> modifier acts much like the <code>private</code> modifier with the exception that members declared <code>protected</code> can also be accessed within deriving classes. For example,</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\"> Person {</span>\n<span style=\"color: #000000\">  </span><span style=\"color: #0000FF\">protected</span><span style=\"color: #000000\"> name: string;</span>\n<span style=\"color: #000000\">  </span><span style=\"color: #0000FF\">constructor</span><span style=\"color: #000000\">(name: string) {</span>\n<span style=\"color: #000000\">    </span><span style=\"color: #0000FF\">this</span><span style=\"color: #000000\">.name = name;</span>\n<span style=\"color: #000000\">  }</span>\n<span style=\"color: #000000\">}</span>\n\n<span style=\"color: #0000FF\">class</span><span style=\"color: #000000\"> Employee </span><span style=\"color: #0000FF\">extends</span><span style=\"color: #000000\"> Person {</span>\n<span style=\"color: #000000\">  </span><span style=\"color: #0000FF\">private</span><span style=\"color: #000000\"> department: string;</span>\n\n<span style=\"color: #000000\">  </span><span style=\"color: #0000FF\">constructor</span><span style=\"color: #000000\">(name: string, department: string) {</span>\n<span style=\"color: #000000\">    </span><span style=\"color: #0000FF\">super</span><span style=\"color: #000000\">(name);</span>\n<span style=\"color: #000000\">    </span><span style=\"color: #0000FF\">this</span><span style=\"color: #000000\">.department = department;</span>\n<span style=\"color: #000000\">  }</span>\n\n<span style=\"color: #000000\">  </span><span style=\"color: #0000FF\">public</span><span style=\"color: #000000\"> getElevatorPitch() {</span>\n<span style=\"color: #000000\">    </span><span style=\"color: #0000FF\">return</span><span style=\"color: #000000\"> </span><span style=\"color: #A31515\">`Hello, my name is </span><span style=\"color: #0000FF\">${this</span><span style=\"color: #A31515\">.name</span><span style=\"color: #0000FF\">}</span><span style=\"color: #A31515\"> and I work in </span><span style=\"color: #0000FF\">${this</span><span style=\"color: #A31515\">.department</span><span style=\"color: #0000FF\">}</span><span style=\"color: #A31515\">.`</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\"> howard = </span><span style=\"color: #0000FF\">new</span><span style=\"color: #000000\"> Employee(</span><span style=\"color: #A31515\">\"Howard\"</span><span style=\"color: #000000\">, </span><span style=\"color: #A31515\">\"Sales\"</span><span style=\"color: #000000\">);</span>\n<span style=\"color: #000000\">console.log(howard.getElevatorPitch());</span>\n<span style=\"color: #000000\">console.log(howard.name); </span><span style=\"color: #008000\">// error</span></code></div></pre>\n<p>Notice that while we can’t use <code>name</code> from outside of <code>Person</code>, we can still use it from within an instance method of <code>Employee</code> because <code>Employee</code> derives from <code>Person</code>.</p>\n<p>A constructor may also be marked <code>protected</code>.\nThis means that the class cannot be instantiated outside of its containing class, but can be extended. For example,</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\"> Person {</span>\n<span style=\"color: #000000\">  </span><span style=\"color: #0000FF\">protected</span><span style=\"color: #000000\"> name: string;</span>\n<span style=\"color: #000000\">  </span><span style=\"color: #0000FF\">protected</span><span style=\"color: #000000\"> </span><span style=\"color: #0000FF\">constructor</span><span style=\"color: #000000\">(theName: string) {</span>\n<span style=\"color: #000000\">    </span><span style=\"color: #0000FF\">this</span><span style=\"color: #000000\">.name = theName;</span>\n<span style=\"color: #000000\">  }</span>\n<span style=\"color: #000000\">}</span>\n\n<span style=\"color: #008000\">// Employee can extend Person</span>\n<span style=\"color: #0000FF\">class</span><span style=\"color: #000000\"> Employee </span><span style=\"color: #0000FF\">extends</span><span style=\"color: #000000\"> Person {</span>\n<span style=\"color: #000000\">  </span><span style=\"color: #0000FF\">private</span><span style=\"color: #000000\"> department: string;</span>\n\n<span style=\"color: #000000\">  </span><span style=\"color: #0000FF\">constructor</span><span style=\"color: #000000\">(name: string, department: string) {</span>\n<span style=\"color: #000000\">    </span><span style=\"color: #0000FF\">super</span><span style=\"color: #000000\">(name);</span>\n<span style=\"color: #000000\">    </span><span style=\"color: #0000FF\">this</span><span style=\"color: #000000\">.department = department;</span>\n<span style=\"color: #000000\">  }</span>\n\n<span style=\"color: #000000\">  </span><span style=\"color: #0000FF\">public</span><span style=\"color: #000000\"> getElevatorPitch() {</span>\n<span style=\"color: #000000\">    </span><span style=\"color: #0000FF\">return</span><span style=\"color: #000000\"> </span><span style=\"color: #A31515\">`Hello, my name is </span><span style=\"color: #0000FF\">${this</span><span style=\"color: #A31515\">.name</span><span style=\"color: #0000FF\">}</span><span style=\"color: #A31515\"> and I work in </span><span style=\"color: #0000FF\">${this</span><span style=\"color: #A31515\">.department</span><span style=\"color: #0000FF\">}</span><span style=\"color: #A31515\">.`</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\"> howard = </span><span style=\"color: #0000FF\">new</span><span style=\"color: #000000\"> Employee(</span><span style=\"color: #A31515\">\"Howard\"</span><span style=\"color: #000000\">, </span><span style=\"color: #A31515\">\"Sales\"</span><span style=\"color: #000000\">);</span>\n<span style=\"color: #0000FF\">let</span><span style=\"color: #000000\"> john = </span><span style=\"color: #0000FF\">new</span><span style=\"color: #000000\"> Person(</span><span style=\"color: #A31515\">\"John\"</span><span style=\"color: #000000\">); </span><span style=\"color: #008000\">// Error: The 'Person' constructor is protected</span></code></div></pre>\n<h1 id=\"readonly-modifier\" style=\"position:relative;\"><a href=\"#readonly-modifier\" aria-label=\"readonly modifier 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>Readonly modifier</h1>\n<p>You can make properties readonly by using the <code>readonly</code> keyword.\nReadonly properties must be initialized at their declaration or in the constructor.</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\"> Octopus {</span>\n<span style=\"color: #000000\">  </span><span style=\"color: #0000FF\">readonly</span><span style=\"color: #000000\"> name: string;</span>\n<span style=\"color: #000000\">  </span><span style=\"color: #0000FF\">readonly</span><span style=\"color: #000000\"> numberOfLegs: number = </span><span style=\"color: #09835A\">8</span><span style=\"color: #000000\">;</span>\n<span style=\"color: #000000\">  </span><span style=\"color: #0000FF\">constructor</span><span style=\"color: #000000\">(theName: string) {</span>\n<span style=\"color: #000000\">    </span><span style=\"color: #0000FF\">this</span><span style=\"color: #000000\">.name = theName;</span>\n<span style=\"color: #000000\">  }</span>\n<span style=\"color: #000000\">}</span>\n<span style=\"color: #0000FF\">let</span><span style=\"color: #000000\"> dad = </span><span style=\"color: #0000FF\">new</span><span style=\"color: #000000\"> Octopus(</span><span style=\"color: #A31515\">\"Man with the 8 strong legs\"</span><span style=\"color: #000000\">);</span>\n<span style=\"color: #000000\">dad.name = </span><span style=\"color: #A31515\">\"Man with the 3-piece suit\"</span><span style=\"color: #000000\">; </span><span style=\"color: #008000\">// error! name is readonly.</span></code></div></pre>\n<h2 id=\"parameter-properties\" style=\"position:relative;\"><a href=\"#parameter-properties\" aria-label=\"parameter properties 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>Parameter properties</h2>\n<p>In our last example, we had to declare a readonly member <code>name</code> and a constructor parameter <code>theName</code> in the <code>Octopus</code> class. This is needed in order to have the value of <code>theName</code> accessible after the <code>Octopus</code> constructor is executed.\n<em>Parameter properties</em> let you create and initialize a member in one place.\nHere’s a further revision of the previous <code>Octopus</code> class using a parameter property:</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\"> Octopus {</span>\n<span style=\"color: #000000\">  </span><span style=\"color: #0000FF\">readonly</span><span style=\"color: #000000\"> numberOfLegs: number = </span><span style=\"color: #09835A\">8</span><span style=\"color: #000000\">;</span>\n<span style=\"color: #000000\">  </span><span style=\"color: #0000FF\">constructor</span><span style=\"color: #000000\">(</span><span style=\"color: #0000FF\">readonly</span><span style=\"color: #000000\"> name: string) {}</span>\n<span style=\"color: #000000\">}</span></code></div></pre>\n<p>Notice how we dropped <code>theName</code> altogether and just use the shortened <code>readonly name: string</code> parameter on the constructor to create and initialize the <code>name</code> member.\nWe’ve consolidated the declarations and assignment into one location.</p>\n<p>Parameter properties are declared by prefixing a constructor parameter with an accessibility modifier or <code>readonly</code>, or both.\nUsing <code>private</code> for a parameter property declares and initializes a private member; likewise, the same is done for <code>public</code>, <code>protected</code>, and <code>readonly</code>.</p>\n<h1 id=\"accessors\" style=\"position:relative;\"><a href=\"#accessors\" aria-label=\"accessors 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>Accessors</h1>\n<p>TypeScript supports getters/setters as a way of intercepting accesses to a member of an object.\nThis gives you a way of having finer-grained control over how a member is accessed on each object.</p>\n<p>Let’s convert a simple class to use <code>get</code> and <code>set</code>.\nFirst, let’s start with an example without getters and setters.</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\"> Employee {</span>\n<span style=\"color: #000000\">  fullName: string;</span>\n<span style=\"color: #000000\">}</span>\n\n<span style=\"color: #0000FF\">let</span><span style=\"color: #000000\"> employee = </span><span style=\"color: #0000FF\">new</span><span style=\"color: #000000\"> Employee();</span>\n<span style=\"color: #000000\">employee.fullName = </span><span style=\"color: #A31515\">\"Bob Smith\"</span><span style=\"color: #000000\">;</span>\n<span style=\"color: #0000FF\">if</span><span style=\"color: #000000\"> (employee.fullName) {</span>\n<span style=\"color: #000000\">  console.log(employee.fullName);</span>\n<span style=\"color: #000000\">}</span></code></div></pre>\n<p>While allowing people to randomly set <code>fullName</code> directly is pretty handy, we may also want enforce some constraints when <code>fullName</code> is set.</p>\n<p>In this version, we add a setter that checks the length of the <code>newName</code> to make sure it’s compatible with the max-length of our backing database field. If it isn’t we throw an error notifying client code that something went wrong.</p>\n<p>To preserve existing functionality, we also add a simple getter that retrieves <code>fullName</code> unmodified.</p>\n<pre class=\"shiki\"><div class=\"language-id\">ts</div><div class='code-container'><code><span style=\"color: #0000FF\">const</span><span style=\"color: #000000\"> fullNameMaxLength = </span><span style=\"color: #09835A\">10</span><span style=\"color: #000000\">;</span>\n\n<span style=\"color: #0000FF\">class</span><span style=\"color: #000000\"> Employee {</span>\n<span style=\"color: #000000\">  </span><span style=\"color: #0000FF\">private</span><span style=\"color: #000000\"> _fullName: string;</span>\n\n<span style=\"color: #000000\">  </span><span style=\"color: #0000FF\">get</span><span style=\"color: #000000\"> fullName(): string {</span>\n<span style=\"color: #000000\">    </span><span style=\"color: #0000FF\">return</span><span style=\"color: #000000\"> </span><span style=\"color: #0000FF\">this</span><span style=\"color: #000000\">._fullName;</span>\n<span style=\"color: #000000\">  }</span>\n\n<span style=\"color: #000000\">  </span><span style=\"color: #0000FF\">set</span><span style=\"color: #000000\"> fullName(newName: string) {</span>\n<span style=\"color: #000000\">    </span><span style=\"color: #0000FF\">if</span><span style=\"color: #000000\"> (newName && newName.length &gt; fullNameMaxLength) {</span>\n<span style=\"color: #000000\">      </span><span style=\"color: #0000FF\">throw</span><span style=\"color: #000000\"> </span><span style=\"color: #0000FF\">new</span><span style=\"color: #000000\"> Error(</span><span style=\"color: #A31515\">\"fullName has a max length of \"</span><span style=\"color: #000000\"> + fullNameMaxLength);</span>\n<span style=\"color: #000000\">    }</span>\n\n<span style=\"color: #000000\">    </span><span style=\"color: #0000FF\">this</span><span style=\"color: #000000\">._fullName = newName;</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\"> employee = </span><span style=\"color: #0000FF\">new</span><span style=\"color: #000000\"> Employee();</span>\n<span style=\"color: #000000\">employee.fullName = </span><span style=\"color: #A31515\">\"Bob Smith\"</span><span style=\"color: #000000\">;</span>\n<span style=\"color: #0000FF\">if</span><span style=\"color: #000000\"> (employee.fullName) {</span>\n<span style=\"color: #000000\">  console.log(employee.fullName);</span>\n<span style=\"color: #000000\">}</span></code></div></pre>\n<p>To prove to ourselves that our accessor is now checking the length of values, we can attempt to assign a name longer than 10 characters and verify that we get an error.</p>\n<p>A couple of things to note about accessors:</p>\n<p>First, accessors require you to set the compiler to output ECMAScript 5 or higher.\nDownleveling to ECMAScript 3 is not supported.\nSecond, accessors with a <code>get</code> and no <code>set</code> are automatically inferred to be <code>readonly</code>.\nThis is helpful when generating a <code>.d.ts</code> file from your code, because users of your property can see that they can’t change it.</p>\n<h1 id=\"static-properties\" style=\"position:relative;\"><a href=\"#static-properties\" aria-label=\"static properties 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>Static Properties</h1>\n<p>Up to this point, we’ve only talked about the <em>instance</em> members of the class, those that show up on the object when it’s instantiated.\nWe can also create <em>static</em> members of a class, those that are visible on the class itself rather than on the instances.\nIn this example, we use <code>static</code> on the origin, as it’s a general value for all grids.\nEach instance accesses this value through prepending the name of the class.\nSimilarly to prepending <code>this.</code> in front of instance accesses, here we prepend <code>Grid.</code> in front of static accesses.</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\"> Grid {</span>\n<span style=\"color: #000000\">  </span><span style=\"color: #0000FF\">static</span><span style=\"color: #000000\"> origin = { x: </span><span style=\"color: #09835A\">0</span><span style=\"color: #000000\">, y: </span><span style=\"color: #09835A\">0</span><span style=\"color: #000000\"> };</span>\n<span style=\"color: #000000\">  calculateDistanceFromOrigin(point: { x: number; y: number }) {</span>\n<span style=\"color: #000000\">    </span><span style=\"color: #0000FF\">let</span><span style=\"color: #000000\"> xDist = point.x - Grid.origin.x;</span>\n<span style=\"color: #000000\">    </span><span style=\"color: #0000FF\">let</span><span style=\"color: #000000\"> yDist = point.y - Grid.origin.y;</span>\n<span style=\"color: #000000\">    </span><span style=\"color: #0000FF\">return</span><span style=\"color: #000000\"> Math.sqrt(xDist * xDist + yDist * yDist) / </span><span style=\"color: #0000FF\">this</span><span style=\"color: #000000\">.scale;</span>\n<span style=\"color: #000000\">  }</span>\n<span style=\"color: #000000\">  </span><span style=\"color: #0000FF\">constructor</span><span style=\"color: #000000\">(</span><span style=\"color: #0000FF\">public</span><span style=\"color: #000000\"> scale: number) {}</span>\n<span style=\"color: #000000\">}</span>\n\n<span style=\"color: #0000FF\">let</span><span style=\"color: #000000\"> grid1 = </span><span style=\"color: #0000FF\">new</span><span style=\"color: #000000\"> Grid(</span><span style=\"color: #09835A\">1.0</span><span style=\"color: #000000\">); </span><span style=\"color: #008000\">// 1x scale</span>\n<span style=\"color: #0000FF\">let</span><span style=\"color: #000000\"> grid2 = </span><span style=\"color: #0000FF\">new</span><span style=\"color: #000000\"> Grid(</span><span style=\"color: #09835A\">5.0</span><span style=\"color: #000000\">); </span><span style=\"color: #008000\">// 5x scale</span>\n\n<span style=\"color: #000000\">console.log(grid1.calculateDistanceFromOrigin({ x: </span><span style=\"color: #09835A\">10</span><span style=\"color: #000000\">, y: </span><span style=\"color: #09835A\">10</span><span style=\"color: #000000\"> }));</span>\n<span style=\"color: #000000\">console.log(grid2.calculateDistanceFromOrigin({ x: </span><span style=\"color: #09835A\">10</span><span style=\"color: #000000\">, y: </span><span style=\"color: #09835A\">10</span><span style=\"color: #000000\"> }));</span></code></div></pre>\n<h1 id=\"abstract-classes\" style=\"position:relative;\"><a href=\"#abstract-classes\" aria-label=\"abstract 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>Abstract Classes</h1>\n<p>Abstract classes are base classes from which other classes may be derived.\nThey may not be instantiated directly.\nUnlike an interface, an abstract class may contain implementation details for its members.\nThe <code>abstract</code> keyword is used to define abstract classes as well as abstract methods within an abstract class.</p>\n<pre class=\"shiki\"><div class=\"language-id\">ts</div><div class='code-container'><code><span style=\"color: #0000FF\">abstract</span><span style=\"color: #000000\"> </span><span style=\"color: #0000FF\">class</span><span style=\"color: #000000\"> Animal {</span>\n<span style=\"color: #000000\">  </span><span style=\"color: #0000FF\">abstract</span><span style=\"color: #000000\"> makeSound(): void;</span>\n<span style=\"color: #000000\">  move(): void {</span>\n<span style=\"color: #000000\">    console.log(</span><span style=\"color: #A31515\">\"roaming the earth...\"</span><span style=\"color: #000000\">);</span>\n<span style=\"color: #000000\">  }</span>\n<span style=\"color: #000000\">}</span></code></div></pre>\n<p>Methods within an abstract class that are marked as abstract do not contain an implementation and must be implemented in derived classes.\nAbstract methods share a similar syntax to interface methods.\nBoth define the signature of a method without including a method body.\nHowever, abstract methods must include the <code>abstract</code> keyword and may optionally include access modifiers.</p>\n<pre class=\"shiki\"><div class=\"language-id\">ts</div><div class='code-container'><code><span style=\"color: #0000FF\">abstract</span><span style=\"color: #000000\"> </span><span style=\"color: #0000FF\">class</span><span style=\"color: #000000\"> Department {</span>\n<span style=\"color: #000000\">  </span><span style=\"color: #0000FF\">constructor</span><span style=\"color: #000000\">(</span><span style=\"color: #0000FF\">public</span><span style=\"color: #000000\"> name: string) {}</span>\n\n<span style=\"color: #000000\">  printName(): void {</span>\n<span style=\"color: #000000\">    console.log(</span><span style=\"color: #A31515\">\"Department name: \"</span><span style=\"color: #000000\"> + </span><span style=\"color: #0000FF\">this</span><span style=\"color: #000000\">.name);</span>\n<span style=\"color: #000000\">  }</span>\n\n<span style=\"color: #000000\">  </span><span style=\"color: #0000FF\">abstract</span><span style=\"color: #000000\"> printMeeting(): void; </span><span style=\"color: #008000\">// must be implemented in derived classes</span>\n<span style=\"color: #000000\">}</span>\n\n<span style=\"color: #0000FF\">class</span><span style=\"color: #000000\"> AccountingDepartment </span><span style=\"color: #0000FF\">extends</span><span style=\"color: #000000\"> Department {</span>\n<span style=\"color: #000000\">  </span><span style=\"color: #0000FF\">constructor</span><span style=\"color: #000000\">() {</span>\n<span style=\"color: #000000\">    </span><span style=\"color: #0000FF\">super</span><span style=\"color: #000000\">(</span><span style=\"color: #A31515\">\"Accounting and Auditing\"</span><span style=\"color: #000000\">); </span><span style=\"color: #008000\">// constructors in derived classes must call super()</span>\n<span style=\"color: #000000\">  }</span>\n\n<span style=\"color: #000000\">  printMeeting(): void {</span>\n<span style=\"color: #000000\">    console.log(</span><span style=\"color: #A31515\">\"The Accounting Department meets each Monday at 10am.\"</span><span style=\"color: #000000\">);</span>\n<span style=\"color: #000000\">  }</span>\n\n<span style=\"color: #000000\">  generateReports(): void {</span>\n<span style=\"color: #000000\">    console.log(</span><span style=\"color: #A31515\">\"Generating accounting reports...\"</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\"> department: Department; </span><span style=\"color: #008000\">// ok to create a reference to an abstract type</span>\n<span style=\"color: #000000\">department = </span><span style=\"color: #0000FF\">new</span><span style=\"color: #000000\"> Department(); </span><span style=\"color: #008000\">// error: cannot create an instance of an abstract class</span>\n<span style=\"color: #000000\">department = </span><span style=\"color: #0000FF\">new</span><span style=\"color: #000000\"> AccountingDepartment(); </span><span style=\"color: #008000\">// ok to create and assign a non-abstract subclass</span>\n<span style=\"color: #000000\">department.printName();</span>\n<span style=\"color: #000000\">department.printMeeting();</span>\n<span style=\"color: #000000\">department.generateReports(); </span><span style=\"color: #008000\">// error: method doesn't exist on declared abstract type</span></code></div></pre>\n<h1 id=\"advanced-techniques\" style=\"position:relative;\"><a href=\"#advanced-techniques\" aria-label=\"advanced techniques 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>Advanced Techniques</h1>\n<h2 id=\"constructor-functions\" style=\"position:relative;\"><a href=\"#constructor-functions\" aria-label=\"constructor 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>Constructor functions</h2>\n<p>When you declare a class in TypeScript, you are actually creating multiple declarations at the same time.\nThe first is the type of the <em>instance</em> 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\"> Greeter {</span>\n<span style=\"color: #000000\">  greeting: string;</span>\n<span style=\"color: #000000\">  </span><span style=\"color: #0000FF\">constructor</span><span style=\"color: #000000\">(message: string) {</span>\n<span style=\"color: #000000\">    </span><span style=\"color: #0000FF\">this</span><span style=\"color: #000000\">.greeting = message;</span>\n<span style=\"color: #000000\">  }</span>\n<span style=\"color: #000000\">  greet() {</span>\n<span style=\"color: #000000\">    </span><span style=\"color: #0000FF\">return</span><span style=\"color: #000000\"> </span><span style=\"color: #A31515\">\"Hello, \"</span><span style=\"color: #000000\"> + </span><span style=\"color: #0000FF\">this</span><span style=\"color: #000000\">.greeting;</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\"> greeter: Greeter;</span>\n<span style=\"color: #000000\">greeter = </span><span style=\"color: #0000FF\">new</span><span style=\"color: #000000\"> Greeter(</span><span style=\"color: #A31515\">\"world\"</span><span style=\"color: #000000\">);</span>\n<span style=\"color: #000000\">console.log(greeter.greet()); </span><span style=\"color: #008000\">// \"Hello, world\"</span></code></div></pre>\n<p>Here, when we say <code>let greeter: Greeter</code>, we’re using <code>Greeter</code> as the type of instances of the class <code>Greeter</code>.\nThis is almost second nature to programmers from other object-oriented languages.</p>\n<p>We’re also creating another value that we call the <em>constructor function</em>.\nThis is the function that is called when we <code>new</code> up instances of the class.\nTo see what this looks like in practice, let’s take a look at the JavaScript created by the above 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\"> Greeter = (</span><span style=\"color: #0000FF\">function</span><span style=\"color: #000000\">() {</span>\n<span style=\"color: #000000\">  </span><span style=\"color: #0000FF\">function</span><span style=\"color: #000000\"> Greeter(message) {</span>\n<span style=\"color: #000000\">    </span><span style=\"color: #0000FF\">this</span><span style=\"color: #000000\">.greeting = message;</span>\n<span style=\"color: #000000\">  }</span>\n<span style=\"color: #000000\">  Greeter.prototype.greet = </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: #A31515\">\"Hello, \"</span><span style=\"color: #000000\"> + </span><span style=\"color: #0000FF\">this</span><span style=\"color: #000000\">.greeting;</span>\n<span style=\"color: #000000\">  };</span>\n<span style=\"color: #000000\">  </span><span style=\"color: #0000FF\">return</span><span style=\"color: #000000\"> Greeter;</span>\n<span style=\"color: #000000\">})();</span>\n\n<span style=\"color: #0000FF\">let</span><span style=\"color: #000000\"> greeter;</span>\n<span style=\"color: #000000\">greeter = </span><span style=\"color: #0000FF\">new</span><span style=\"color: #000000\"> Greeter(</span><span style=\"color: #A31515\">\"world\"</span><span style=\"color: #000000\">);</span>\n<span style=\"color: #000000\">console.log(greeter.greet()); </span><span style=\"color: #008000\">// \"Hello, world\"</span></code></div></pre>\n<p>Here, <code>let Greeter</code> is going to be assigned the constructor function.\nWhen we call <code>new</code> and run this function, we get an instance of the class.\nThe constructor function also contains all of the static members of the class.\nAnother way to think of each class is that there is an <em>instance</em> side and a <em>static</em> side.</p>\n<p>Let’s modify the example a bit to show this difference:</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\"> Greeter {</span>\n<span style=\"color: #000000\">  </span><span style=\"color: #0000FF\">static</span><span style=\"color: #000000\"> standardGreeting = </span><span style=\"color: #A31515\">\"Hello, there\"</span><span style=\"color: #000000\">;</span>\n<span style=\"color: #000000\">  greeting: string;</span>\n<span style=\"color: #000000\">  greet() {</span>\n<span style=\"color: #000000\">    </span><span style=\"color: #0000FF\">if</span><span style=\"color: #000000\"> (</span><span style=\"color: #0000FF\">this</span><span style=\"color: #000000\">.greeting) {</span>\n<span style=\"color: #000000\">      </span><span style=\"color: #0000FF\">return</span><span style=\"color: #000000\"> </span><span style=\"color: #A31515\">\"Hello, \"</span><span style=\"color: #000000\"> + </span><span style=\"color: #0000FF\">this</span><span style=\"color: #000000\">.greeting;</span>\n<span style=\"color: #000000\">    } </span><span style=\"color: #0000FF\">else</span><span style=\"color: #000000\"> {</span>\n<span style=\"color: #000000\">      </span><span style=\"color: #0000FF\">return</span><span style=\"color: #000000\"> Greeter.standardGreeting;</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\"> greeter1: Greeter;</span>\n<span style=\"color: #000000\">greeter1 = </span><span style=\"color: #0000FF\">new</span><span style=\"color: #000000\"> Greeter();</span>\n<span style=\"color: #000000\">console.log(greeter1.greet()); </span><span style=\"color: #008000\">// \"Hello, there\"</span>\n\n<span style=\"color: #0000FF\">let</span><span style=\"color: #000000\"> greeterMaker: </span><span style=\"color: #0000FF\">typeof</span><span style=\"color: #000000\"> Greeter = Greeter;</span>\n<span style=\"color: #000000\">greeterMaker.standardGreeting = </span><span style=\"color: #A31515\">\"Hey there!\"</span><span style=\"color: #000000\">;</span>\n\n<span style=\"color: #0000FF\">let</span><span style=\"color: #000000\"> greeter2: Greeter = </span><span style=\"color: #0000FF\">new</span><span style=\"color: #000000\"> greeterMaker();</span>\n<span style=\"color: #000000\">console.log(greeter2.greet()); </span><span style=\"color: #008000\">// \"Hey there!\"</span></code></div></pre>\n<p>In this example, <code>greeter1</code> works similarly to before.\nWe instantiate the <code>Greeter</code> class, and use this object.\nThis we have seen before.</p>\n<p>Next, we then use the class directly.\nHere we create a new variable called <code>greeterMaker</code>.\nThis variable will hold the class itself, or said another way its constructor function.\nHere we use <code>typeof Greeter</code>, that is “give me the type of the <code>Greeter</code> class itself” rather than the instance type.\nOr, more precisely, “give me the type of the symbol called <code>Greeter</code>,” which is the type of the constructor function.\nThis type will contain all of the static members of Greeter along with the constructor that creates instances of the <code>Greeter</code> class.\nWe show this by using <code>new</code> on <code>greeterMaker</code>, creating new instances of <code>Greeter</code> and invoking them as before.</p>\n<h2 id=\"using-a-class-as-an-interface\" style=\"position:relative;\"><a href=\"#using-a-class-as-an-interface\" aria-label=\"using a class as an interface 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 a class as an interface</h2>\n<p>As we said in the previous section, a class declaration creates two things: a type representing instances of the class and a constructor function.\nBecause classes create types, you can use them in the same places you would be able to use interfaces.</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\"> Point {</span>\n<span style=\"color: #000000\">  x: number;</span>\n<span style=\"color: #000000\">  y: number;</span>\n<span style=\"color: #000000\">}</span>\n\n<span style=\"color: #0000FF\">interface</span><span style=\"color: #000000\"> Point3d </span><span style=\"color: #0000FF\">extends</span><span style=\"color: #000000\"> Point {</span>\n<span style=\"color: #000000\">  z: number;</span>\n<span style=\"color: #000000\">}</span>\n\n<span style=\"color: #0000FF\">let</span><span style=\"color: #000000\"> point3d: Point3d = { x: </span><span style=\"color: #09835A\">1</span><span style=\"color: #000000\">, y: </span><span style=\"color: #09835A\">2</span><span style=\"color: #000000\">, z: </span><span style=\"color: #09835A\">3</span><span style=\"color: #000000\"> };</span></code></div></pre>","headings":[{"value":"Introduction","depth":1},{"value":"Classes","depth":1},{"value":"Inheritance","depth":1},{"value":"Public, private, and protected modifiers","depth":1},{"value":"Public by default","depth":2},{"value":"ECMAScript Private Fields","depth":2},{"value":"Understanding TypeScript’s private","depth":2},{"value":"Understanding protected","depth":2},{"value":"Readonly modifier","depth":1},{"value":"Parameter properties","depth":2},{"value":"Accessors","depth":1},{"value":"Static Properties","depth":1},{"value":"Abstract Classes","depth":1},{"value":"Advanced Techniques","depth":1},{"value":"Constructor functions","depth":2},{"value":"Using a class as an interface","depth":2}],"frontmatter":{"permalink":"/docs/handbook/classes.html","title":"Classes"}}},"pageContext":{"slug":"/docs/handbook/classes.html","isOldHandbook":true}}}