{"componentChunkName":"component---src-templates-handbook-tsx","path":"/docs/handbook/interfaces.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":"5fab8cda-49f5-55be-9e35-d1e9a8f3ac75","excerpt":"Introduction One of TypeScript’s core principles is that type checking focuses on the shape that values have.\nThis is sometimes called “duck typing” or…","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>One of TypeScript’s core principles is that type checking focuses on the <em>shape</em> that values have.\nThis is sometimes called “duck typing” or “structural subtyping”.\nIn TypeScript, interfaces fill the role of naming these types, and are a powerful way of defining contracts within your code as well as contracts with code outside of your project.</p>\n<h1 id=\"our-first-interface\" style=\"position:relative;\"><a href=\"#our-first-interface\" aria-label=\"our first 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>Our First Interface</h1>\n<p>The easiest way to see how interfaces work is to start with a simple example:</p>\n<pre class=\"shiki\"><div class=\"language-id\">ts</div><div class='code-container'><code><span style=\"color: #0000FF\">function</span><span style=\"color: #000000\"> printLabel(labeledObj: { label: string }) {</span>\n<span style=\"color: #000000\">  console.log(labeledObj.label);</span>\n<span style=\"color: #000000\">}</span>\n\n<span style=\"color: #0000FF\">let</span><span style=\"color: #000000\"> myObj = { size: </span><span style=\"color: #09835A\">10</span><span style=\"color: #000000\">, label: </span><span style=\"color: #A31515\">\"Size 10 Object\"</span><span style=\"color: #000000\"> };</span>\n<span style=\"color: #000000\">printLabel(myObj);</span></code></div></pre>\n<p>The type checker checks the call to <code>printLabel</code>.\nThe <code>printLabel</code> function has a single parameter that requires that the object passed in has a property called <code>label</code> of type <code>string</code>.\nNotice that our object actually has more properties than this, but the compiler only checks that <em>at least</em> the ones required are present and match the types required.\nThere are some cases where TypeScript isn’t as lenient, which we’ll cover in a bit.</p>\n<p>We can write the same example again, this time using an interface to describe the requirement of having the <code>label</code> property that is a string:</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\"> LabeledValue {</span>\n<span style=\"color: #000000\">  label: string;</span>\n<span style=\"color: #000000\">}</span>\n\n<span style=\"color: #0000FF\">function</span><span style=\"color: #000000\"> printLabel(labeledObj: LabeledValue) {</span>\n<span style=\"color: #000000\">  console.log(labeledObj.label);</span>\n<span style=\"color: #000000\">}</span>\n\n<span style=\"color: #0000FF\">let</span><span style=\"color: #000000\"> myObj = { size: </span><span style=\"color: #09835A\">10</span><span style=\"color: #000000\">, label: </span><span style=\"color: #A31515\">\"Size 10 Object\"</span><span style=\"color: #000000\"> };</span>\n<span style=\"color: #000000\">printLabel(myObj);</span></code></div></pre>\n<p>The interface <code>LabeledValue</code> is a name we can now use to describe the requirement in the previous example.\nIt still represents having a single property called <code>label</code> that is of type <code>string</code>.\nNotice we didn’t have to explicitly say that the object we pass to <code>printLabel</code> implements this interface like we might have to in other languages.\nHere, it’s only the shape that matters. If the object we pass to the function meets the requirements listed, then it’s allowed.</p>\n<p>It’s worth pointing out that the type checker does not require that these properties come in any sort of order, only that the properties the interface requires are present and have the required type.</p>\n<h1 id=\"optional-properties\" style=\"position:relative;\"><a href=\"#optional-properties\" aria-label=\"optional 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>Optional Properties</h1>\n<p>Not all properties of an interface may be required.\nSome exist under certain conditions or may not be there at all.\nThese optional properties are popular when creating patterns like “option bags” where you pass an object to a function that only has a couple of properties filled in.</p>\n<p>Here’s an example of this pattern:</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\"> SquareConfig {</span>\n<span style=\"color: #000000\">  color?: string;</span>\n<span style=\"color: #000000\">  width?: number;</span>\n<span style=\"color: #000000\">}</span>\n\n<span style=\"color: #0000FF\">function</span><span style=\"color: #000000\"> createSquare(config: SquareConfig): { color: string; area: number } {</span>\n<span style=\"color: #000000\">  </span><span style=\"color: #0000FF\">let</span><span style=\"color: #000000\"> newSquare = { color: </span><span style=\"color: #A31515\">\"white\"</span><span style=\"color: #000000\">, area: </span><span style=\"color: #09835A\">100</span><span style=\"color: #000000\"> };</span>\n<span style=\"color: #000000\">  </span><span style=\"color: #0000FF\">if</span><span style=\"color: #000000\"> (config.color) {</span>\n<span style=\"color: #000000\">    newSquare.color = config.color;</span>\n<span style=\"color: #000000\">  }</span>\n<span style=\"color: #000000\">  </span><span style=\"color: #0000FF\">if</span><span style=\"color: #000000\"> (config.width) {</span>\n<span style=\"color: #000000\">    newSquare.area = config.width * config.width;</span>\n<span style=\"color: #000000\">  }</span>\n<span style=\"color: #000000\">  </span><span style=\"color: #0000FF\">return</span><span style=\"color: #000000\"> newSquare;</span>\n<span style=\"color: #000000\">}</span>\n\n<span style=\"color: #0000FF\">let</span><span style=\"color: #000000\"> mySquare = createSquare({ color: </span><span style=\"color: #A31515\">\"black\"</span><span style=\"color: #000000\"> });</span></code></div></pre>\n<p>Interfaces with optional properties are written similar to other interfaces, with each optional property denoted by a <code>?</code> at the end of the property name in the declaration.</p>\n<p>The advantage of optional properties is that you can describe these possibly available properties while still also preventing use of properties that are not part of the interface.\nFor example, had we mistyped the name of the <code>color</code> property in <code>createSquare</code>, we would get an error message letting us know:</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\"> SquareConfig {</span>\n<span style=\"color: #000000\">  color?: string;</span>\n<span style=\"color: #000000\">  width?: number;</span>\n<span style=\"color: #000000\">}</span>\n\n<span style=\"color: #0000FF\">function</span><span style=\"color: #000000\"> createSquare(config: SquareConfig): { color: string; area: number } {</span>\n<span style=\"color: #000000\">  </span><span style=\"color: #0000FF\">let</span><span style=\"color: #000000\"> newSquare = { color: </span><span style=\"color: #A31515\">\"white\"</span><span style=\"color: #000000\">, area: </span><span style=\"color: #09835A\">100</span><span style=\"color: #000000\"> };</span>\n<span style=\"color: #000000\">  </span><span style=\"color: #0000FF\">if</span><span style=\"color: #000000\"> (config.clor) {</span>\n<span style=\"color: #000000\">    </span><span style=\"color: #008000\">// Error: Property 'clor' does not exist on type 'SquareConfig'</span>\n<span style=\"color: #000000\">    newSquare.color = config.clor;</span>\n<span style=\"color: #000000\">  }</span>\n<span style=\"color: #000000\">  </span><span style=\"color: #0000FF\">if</span><span style=\"color: #000000\"> (config.width) {</span>\n<span style=\"color: #000000\">    newSquare.area = config.width * config.width;</span>\n<span style=\"color: #000000\">  }</span>\n<span style=\"color: #000000\">  </span><span style=\"color: #0000FF\">return</span><span style=\"color: #000000\"> newSquare;</span>\n<span style=\"color: #000000\">}</span>\n\n<span style=\"color: #0000FF\">let</span><span style=\"color: #000000\"> mySquare = createSquare({ color: </span><span style=\"color: #A31515\">\"black\"</span><span style=\"color: #000000\"> });</span></code></div></pre>\n<h1 id=\"readonly-properties\" style=\"position:relative;\"><a href=\"#readonly-properties\" aria-label=\"readonly 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>Readonly properties</h1>\n<p>Some properties should only be modifiable when an object is first created.\nYou can specify this by putting <code>readonly</code> before the name of the property:</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\"> Point {</span>\n<span style=\"color: #000000\">  </span><span style=\"color: #0000FF\">readonly</span><span style=\"color: #000000\"> x: number;</span>\n<span style=\"color: #000000\">  </span><span style=\"color: #0000FF\">readonly</span><span style=\"color: #000000\"> y: number;</span>\n<span style=\"color: #000000\">}</span></code></div></pre>\n<p>You can construct a <code>Point</code> by assigning an object literal.\nAfter the assignment, <code>x</code> and <code>y</code> can’t be changed.</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\"> p1: Point = { x: </span><span style=\"color: #09835A\">10</span><span style=\"color: #000000\">, y: </span><span style=\"color: #09835A\">20</span><span style=\"color: #000000\"> };</span>\n<span style=\"color: #000000\">p1.x = </span><span style=\"color: #09835A\">5</span><span style=\"color: #000000\">; </span><span style=\"color: #008000\">// error!</span></code></div></pre>\n<p>TypeScript comes with a <code>ReadonlyArray&#x3C;T></code> type that is the same as <code>Array&#x3C;T></code> with all mutating methods removed, so you can make sure you don’t change your arrays after creation:</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\"> a: number[] = [</span><span style=\"color: #09835A\">1</span><span style=\"color: #000000\">, </span><span style=\"color: #09835A\">2</span><span style=\"color: #000000\">, </span><span style=\"color: #09835A\">3</span><span style=\"color: #000000\">, </span><span style=\"color: #09835A\">4</span><span style=\"color: #000000\">];</span>\n<span style=\"color: #0000FF\">let</span><span style=\"color: #000000\"> ro: ReadonlyArray&lt;number&gt; = a;</span>\n<span style=\"color: #000000\">ro[</span><span style=\"color: #09835A\">0</span><span style=\"color: #000000\">] = </span><span style=\"color: #09835A\">12</span><span style=\"color: #000000\">; </span><span style=\"color: #008000\">// error!</span>\n<span style=\"color: #000000\">ro.push(</span><span style=\"color: #09835A\">5</span><span style=\"color: #000000\">); </span><span style=\"color: #008000\">// error!</span>\n<span style=\"color: #000000\">ro.length = </span><span style=\"color: #09835A\">100</span><span style=\"color: #000000\">; </span><span style=\"color: #008000\">// error!</span>\n<span style=\"color: #000000\">a = ro; </span><span style=\"color: #008000\">// error!</span></code></div></pre>\n<p>On the last line of the snippet you can see that even assigning the entire <code>ReadonlyArray</code> back to a normal array is illegal.\nYou can still override it with a type assertion, though:</p>\n<pre class=\"shiki\"><div class=\"language-id\">ts</div><div class='code-container'><code><span style=\"color: #000000\">a = ro </span><span style=\"color: #0000FF\">as</span><span style=\"color: #000000\"> number[];</span></code></div></pre>\n<h2 id=\"readonly-vs-const\" style=\"position:relative;\"><a href=\"#readonly-vs-const\" aria-label=\"readonly vs const 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>readonly</code> vs <code>const</code></h2>\n<p>The easiest way to remember whether to use <code>readonly</code> or <code>const</code> is to ask whether you’re using it on a variable or a property.\nVariables use <code>const</code> whereas properties use <code>readonly</code>.</p>\n<h1 id=\"excess-property-checks\" style=\"position:relative;\"><a href=\"#excess-property-checks\" aria-label=\"excess property checks 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>Excess Property Checks</h1>\n<p>In our first example using interfaces, TypeScript lets us pass <code>{ size: number; label: string; }</code> to something that only expected a <code>{ label: string; }</code>.\nWe also just learned about optional properties, and how they’re useful when describing so-called “option bags”.</p>\n<p>However, combining the two naively would allow an error to sneak in. For example, taking our last example using <code>createSquare</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\"> SquareConfig {</span>\n<span style=\"color: #000000\">  color?: string;</span>\n<span style=\"color: #000000\">  width?: number;</span>\n<span style=\"color: #000000\">}</span>\n\n<span style=\"color: #0000FF\">function</span><span style=\"color: #000000\"> createSquare(config: SquareConfig): { color: string; area: number } {</span>\n<span style=\"color: #000000\">  </span><span style=\"color: #008000\">// ...</span>\n<span style=\"color: #000000\">}</span>\n\n<span style=\"color: #0000FF\">let</span><span style=\"color: #000000\"> mySquare = createSquare({ colour: </span><span style=\"color: #A31515\">\"red\"</span><span style=\"color: #000000\">, width: </span><span style=\"color: #09835A\">100</span><span style=\"color: #000000\"> });</span></code></div></pre>\n<p>Notice the given argument to <code>createSquare</code> is spelled <em><code>colour</code></em> instead of <code>color</code>.\nIn plain JavaScript, this sort of thing fails silently.</p>\n<p>You could argue that this program is correctly typed, since the <code>width</code> properties are compatible, there’s no <code>color</code> property present, and the extra <code>colour</code> property is insignificant.</p>\n<p>However, TypeScript takes the stance that there’s probably a bug in this code.\nObject literals get special treatment and undergo <em>excess property checking</em> when assigning them to other variables, or passing them as arguments.\nIf an object literal has any properties that the “target type” doesn’t have, you’ll get an error:</p>\n<pre class=\"shiki\"><div class=\"language-id\">ts</div><div class='code-container'><code><span style=\"color: #008000\">// error: Object literal may only specify known properties, but 'colour' does not exist in type 'SquareConfig'. Did you mean to write 'color'?</span>\n<span style=\"color: #0000FF\">let</span><span style=\"color: #000000\"> mySquare = createSquare({ colour: </span><span style=\"color: #A31515\">\"red\"</span><span style=\"color: #000000\">, width: </span><span style=\"color: #09835A\">100</span><span style=\"color: #000000\"> });</span></code></div></pre>\n<p>Getting around these checks is actually really simple.\nThe easiest method is to just use a type assertion:</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\"> mySquare = createSquare({ width: </span><span style=\"color: #09835A\">100</span><span style=\"color: #000000\">, opacity: </span><span style=\"color: #09835A\">0.5</span><span style=\"color: #000000\"> } </span><span style=\"color: #0000FF\">as</span><span style=\"color: #000000\"> SquareConfig);</span></code></div></pre>\n<p>However, a better approach might be to add a string index signature if you’re sure that the object can have some extra properties that are used in some special way.\nIf <code>SquareConfig</code> can have <code>color</code> and <code>width</code> properties with the above types, but could <em>also</em> have any number of other properties, then we could define it like so:</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\"> SquareConfig {</span>\n<span style=\"color: #000000\">  color?: string;</span>\n<span style=\"color: #000000\">  width?: number;</span>\n<span style=\"color: #000000\">  [propName: string]: any;</span>\n<span style=\"color: #000000\">}</span></code></div></pre>\n<p>We’ll discuss index signatures in a bit, but here we’re saying a <code>SquareConfig</code> can have any number of properties, and as long as they aren’t <code>color</code> or <code>width</code>, their types don’t matter.</p>\n<p>One final way to get around these checks, which might be a bit surprising, is to assign the object to another variable:\nSince <code>squareOptions</code> won’t undergo excess property checks, the compiler won’t give you an error.</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\"> squareOptions = { colour: </span><span style=\"color: #A31515\">\"red\"</span><span style=\"color: #000000\">, width: </span><span style=\"color: #09835A\">100</span><span style=\"color: #000000\"> };</span>\n<span style=\"color: #0000FF\">let</span><span style=\"color: #000000\"> mySquare = createSquare(squareOptions);</span></code></div></pre>\n<p>The above workaround will work as long as you have a common property between <code>squareOptions</code> and <code>SquareConfig</code>.\nIn this example, it was the property <code>width</code>. It will however, fail if the variable does not have any common object property. For 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\"> squareOptions = { colour: </span><span style=\"color: #A31515\">\"red\"</span><span style=\"color: #000000\"> };</span>\n<span style=\"color: #0000FF\">let</span><span style=\"color: #000000\"> mySquare = createSquare(squareOptions);</span></code></div></pre>\n<p>Keep in mind that for simple code like above, you probably shouldn’t be trying to “get around” these checks.\nFor more complex object literals that have methods and hold state, you might need to keep these techniques in mind, but a majority of excess property errors are actually bugs.\nThat means if you’re running into excess property checking problems for something like option bags, you might need to revise some of your type declarations.\nIn this instance, if it’s okay to pass an object with both a <code>color</code> or <code>colour</code> property to <code>createSquare</code>, you should fix up the definition of <code>SquareConfig</code> to reflect that.</p>\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<p>Interfaces are capable of describing the wide range of shapes that JavaScript objects can take.\nIn addition to describing an object with properties, interfaces are also capable of describing function types.</p>\n<p>To describe a function type with an interface, we give the interface a call signature.\nThis is like a function declaration with only the parameter list and return type given. Each parameter in the parameter list requires both name and type.</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\"> SearchFunc {</span>\n<span style=\"color: #000000\">  (source: string, subString: string): boolean;</span>\n<span style=\"color: #000000\">}</span></code></div></pre>\n<p>Once defined, we can use this function type interface like we would other interfaces.\nHere, we show how you can create a variable of a function type and assign it a function value of the same 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\"> mySearch: SearchFunc;</span>\n<span style=\"color: #000000\">mySearch = </span><span style=\"color: #0000FF\">function</span><span style=\"color: #000000\">(source: string, subString: string) {</span>\n<span style=\"color: #000000\">  </span><span style=\"color: #0000FF\">let</span><span style=\"color: #000000\"> result = source.search(subString);</span>\n<span style=\"color: #000000\">  </span><span style=\"color: #0000FF\">return</span><span style=\"color: #000000\"> result &gt; -</span><span style=\"color: #09835A\">1</span><span style=\"color: #000000\">;</span>\n<span style=\"color: #000000\">};</span></code></div></pre>\n<p>For function types to correctly type check, the names of the parameters do not need to match.\nWe could have, for example, written the above example like this:</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\"> mySearch: SearchFunc;</span>\n<span style=\"color: #000000\">mySearch = </span><span style=\"color: #0000FF\">function</span><span style=\"color: #000000\">(src: string, sub: string): boolean {</span>\n<span style=\"color: #000000\">  </span><span style=\"color: #0000FF\">let</span><span style=\"color: #000000\"> result = src.search(sub);</span>\n<span style=\"color: #000000\">  </span><span style=\"color: #0000FF\">return</span><span style=\"color: #000000\"> result &gt; -</span><span style=\"color: #09835A\">1</span><span style=\"color: #000000\">;</span>\n<span style=\"color: #000000\">};</span></code></div></pre>\n<p>Function parameters are checked one at a time, with the type in each corresponding parameter position checked against each other.\nIf you do not want to specify types at all, TypeScript’s contextual typing can infer the argument types since the function value is assigned directly to a variable of type <code>SearchFunc</code>.\nHere, also, the return type of our function expression is implied by the values it returns (here <code>false</code> and <code>true</code>).</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\"> mySearch: SearchFunc;</span>\n<span style=\"color: #000000\">mySearch = </span><span style=\"color: #0000FF\">function</span><span style=\"color: #000000\">(src, sub) {</span>\n<span style=\"color: #000000\">  </span><span style=\"color: #0000FF\">let</span><span style=\"color: #000000\"> result = src.search(sub);</span>\n<span style=\"color: #000000\">  </span><span style=\"color: #0000FF\">return</span><span style=\"color: #000000\"> result &gt; -</span><span style=\"color: #09835A\">1</span><span style=\"color: #000000\">;</span>\n<span style=\"color: #000000\">};</span></code></div></pre>\n<p>Had the function expression returned numbers or strings, the type checker would have made an error that indicates return type doesn’t match the return type described in the <code>SearchFunc</code> interface.</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\"> mySearch: SearchFunc;</span>\n\n<span style=\"color: #008000\">// error: Type '(src: string, sub: string) =&gt; string' is not assignable to type 'SearchFunc'.</span>\n<span style=\"color: #008000\">// Type 'string' is not assignable to type 'boolean'.</span>\n<span style=\"color: #000000\">mySearch = </span><span style=\"color: #0000FF\">function</span><span style=\"color: #000000\">(src, sub) {</span>\n<span style=\"color: #000000\">  </span><span style=\"color: #0000FF\">let</span><span style=\"color: #000000\"> result = src.search(sub);</span>\n<span style=\"color: #000000\">  </span><span style=\"color: #0000FF\">return</span><span style=\"color: #000000\"> </span><span style=\"color: #A31515\">\"string\"</span><span style=\"color: #000000\">;</span>\n<span style=\"color: #000000\">};</span></code></div></pre>\n<h1 id=\"indexable-types\" style=\"position:relative;\"><a href=\"#indexable-types\" aria-label=\"indexable 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>Indexable Types</h1>\n<p>Similarly to how we can use interfaces to describe function types, we can also describe types that we can “index into” like <code>a[10]</code>, or <code>ageMap[\"daniel\"]</code>.\nIndexable types have an <em>index signature</em> that describes the types we can use to index into the object, along with the corresponding return types when indexing.\nLet’s take an example:</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\"> StringArray {</span>\n<span style=\"color: #000000\">  [index: number]: string;</span>\n<span style=\"color: #000000\">}</span>\n\n<span style=\"color: #0000FF\">let</span><span style=\"color: #000000\"> myArray: StringArray;</span>\n<span style=\"color: #000000\">myArray = [</span><span style=\"color: #A31515\">\"Bob\"</span><span style=\"color: #000000\">, </span><span style=\"color: #A31515\">\"Fred\"</span><span style=\"color: #000000\">];</span>\n\n<span style=\"color: #0000FF\">let</span><span style=\"color: #000000\"> myStr: string = myArray[</span><span style=\"color: #09835A\">0</span><span style=\"color: #000000\">];</span></code></div></pre>\n<p>Above, we have a <code>StringArray</code> interface that has an index signature.\nThis index signature states that when a <code>StringArray</code> is indexed with a <code>number</code>, it will return a <code>string</code>.</p>\n<p>There are two types of supported index signatures: string and number.\nIt is possible to support both types of indexers, but the type returned from a numeric indexer must be a subtype of the type returned from the string indexer.\nThis is because when indexing with a <code>number</code>, JavaScript will actually convert that to a <code>string</code> before indexing into an object.\nThat means that indexing with <code>100</code> (a <code>number</code>) is the same thing as indexing with <code>\"100\"</code> (a <code>string</code>), so the two need to be consistent.</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>\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\">  breed: string;</span>\n<span style=\"color: #000000\">}</span>\n\n<span style=\"color: #008000\">// Error: indexing with a numeric string might get you a completely separate type of Animal!</span>\n<span style=\"color: #0000FF\">interface</span><span style=\"color: #000000\"> NotOkay {</span>\n<span style=\"color: #000000\">  [x: number]: Animal;</span>\n<span style=\"color: #000000\">  [x: string]: Dog;</span>\n<span style=\"color: #000000\">}</span></code></div></pre>\n<p>While string index signatures are a powerful way to describe the “dictionary” pattern, they also enforce that all properties match their return type.\nThis is because a string index declares that <code>obj.property</code> is also available as <code>obj[\"property\"]</code>.\nIn the following example, <code>name</code>’s type does not match the string index’s type, and the type checker gives an error:</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\"> NumberDictionary {</span>\n<span style=\"color: #000000\">  [index: string]: number;</span>\n<span style=\"color: #000000\">  length: number; </span><span style=\"color: #008000\">// ok, length is a number</span>\n<span style=\"color: #000000\">  name: string; </span><span style=\"color: #008000\">// error, the type of 'name' is not a subtype of the indexer</span>\n<span style=\"color: #000000\">}</span></code></div></pre>\n<p>However, properties of different types are acceptable if the index signature is a union of the property types:</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\"> NumberOrStringDictionary {</span>\n<span style=\"color: #000000\">  [index: string]: number | string;</span>\n<span style=\"color: #000000\">  length: number; </span><span style=\"color: #008000\">// ok, length is a number</span>\n<span style=\"color: #000000\">  name: string; </span><span style=\"color: #008000\">// ok, name is a string</span>\n<span style=\"color: #000000\">}</span></code></div></pre>\n<p>Finally, you can make index signatures <code>readonly</code> in order to prevent assignment to their indices:</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\"> ReadonlyStringArray {</span>\n<span style=\"color: #000000\">  </span><span style=\"color: #0000FF\">readonly</span><span style=\"color: #000000\"> [index: number]: string;</span>\n<span style=\"color: #000000\">}</span>\n<span style=\"color: #0000FF\">let</span><span style=\"color: #000000\"> myArray: ReadonlyStringArray = [</span><span style=\"color: #A31515\">\"Alice\"</span><span style=\"color: #000000\">, </span><span style=\"color: #A31515\">\"Bob\"</span><span style=\"color: #000000\">];</span>\n<span style=\"color: #000000\">myArray[</span><span style=\"color: #09835A\">2</span><span style=\"color: #000000\">] = </span><span style=\"color: #A31515\">\"Mallory\"</span><span style=\"color: #000000\">; </span><span style=\"color: #008000\">// error!</span></code></div></pre>\n<p>You can’t set <code>myArray[2]</code> because the index signature is readonly.</p>\n<h1 id=\"class-types\" style=\"position:relative;\"><a href=\"#class-types\" aria-label=\"class 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>Class Types</h1>\n<h2 id=\"implementing-an-interface\" style=\"position:relative;\"><a href=\"#implementing-an-interface\" aria-label=\"implementing 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>Implementing an interface</h2>\n<p>One of the most common uses of interfaces in languages like C# and Java, that of explicitly enforcing that a class meets a particular contract, is also possible in TypeScript.</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\"> ClockInterface {</span>\n<span style=\"color: #000000\">  currentTime: Date;</span>\n<span style=\"color: #000000\">}</span>\n\n<span style=\"color: #0000FF\">class</span><span style=\"color: #000000\"> Clock </span><span style=\"color: #0000FF\">implements</span><span style=\"color: #000000\"> ClockInterface {</span>\n<span style=\"color: #000000\">  currentTime: Date = </span><span style=\"color: #0000FF\">new</span><span style=\"color: #000000\"> Date();</span>\n<span style=\"color: #000000\">  </span><span style=\"color: #0000FF\">constructor</span><span style=\"color: #000000\">(h: number, m: number) {}</span>\n<span style=\"color: #000000\">}</span></code></div></pre>\n<p>You can also describe methods in an interface that are implemented in the class, as we do with <code>setTime</code> in the below example:</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\"> ClockInterface {</span>\n<span style=\"color: #000000\">  currentTime: Date;</span>\n<span style=\"color: #000000\">  setTime(d: Date): void;</span>\n<span style=\"color: #000000\">}</span>\n\n<span style=\"color: #0000FF\">class</span><span style=\"color: #000000\"> Clock </span><span style=\"color: #0000FF\">implements</span><span style=\"color: #000000\"> ClockInterface {</span>\n<span style=\"color: #000000\">  currentTime: Date = </span><span style=\"color: #0000FF\">new</span><span style=\"color: #000000\"> Date();</span>\n<span style=\"color: #000000\">  setTime(d: Date) {</span>\n<span style=\"color: #000000\">    </span><span style=\"color: #0000FF\">this</span><span style=\"color: #000000\">.currentTime = d;</span>\n<span style=\"color: #000000\">  }</span>\n<span style=\"color: #000000\">  </span><span style=\"color: #0000FF\">constructor</span><span style=\"color: #000000\">(h: number, m: number) {}</span>\n<span style=\"color: #000000\">}</span></code></div></pre>\n<p>Interfaces describe the public side of the class, rather than both the public and private side.\nThis prohibits you from using them to check that a class also has particular types for the private side of the class instance.</p>\n<h2 id=\"difference-between-the-static-and-instance-sides-of-classes\" style=\"position:relative;\"><a href=\"#difference-between-the-static-and-instance-sides-of-classes\" aria-label=\"difference between the static and instance sides of 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>Difference between the static and instance sides of classes</h2>\n<p>When working with classes and interfaces, it helps to keep in mind that a class has <em>two</em> types: the type of the static side and the type of the instance side.\nYou may notice that if you create an interface with a construct signature and try to create a class that implements this interface you get an error:</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\"> ClockConstructor {</span>\n<span style=\"color: #000000\">  </span><span style=\"color: #0000FF\">new</span><span style=\"color: #000000\"> (hour: number, minute: number);</span>\n<span style=\"color: #000000\">}</span>\n\n<span style=\"color: #0000FF\">class</span><span style=\"color: #000000\"> Clock </span><span style=\"color: #0000FF\">implements</span><span style=\"color: #000000\"> ClockConstructor {</span>\n<span style=\"color: #000000\">  currentTime: Date;</span>\n<span style=\"color: #000000\">  </span><span style=\"color: #0000FF\">constructor</span><span style=\"color: #000000\">(h: number, m: number) {}</span>\n<span style=\"color: #000000\">}</span></code></div></pre>\n<p>This is because when a class implements an interface, only the instance side of the class is checked.\nSince the constructor sits in the static side, it is not included in this check.</p>\n<p>Instead, you would need to work with the static side of the class directly.\nIn this example, we define two interfaces, <code>ClockConstructor</code> for the constructor and <code>ClockInterface</code> for the instance methods.\nThen, for convenience, we define a constructor function <code>createClock</code> that creates instances of the type that is passed to it:</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\"> ClockConstructor {</span>\n<span style=\"color: #000000\">  </span><span style=\"color: #0000FF\">new</span><span style=\"color: #000000\"> (hour: number, minute: number): ClockInterface;</span>\n<span style=\"color: #000000\">}</span>\n<span style=\"color: #0000FF\">interface</span><span style=\"color: #000000\"> ClockInterface {</span>\n<span style=\"color: #000000\">  tick(): void;</span>\n<span style=\"color: #000000\">}</span>\n\n<span style=\"color: #0000FF\">function</span><span style=\"color: #000000\"> createClock(</span>\n<span style=\"color: #000000\">  ctor: ClockConstructor,</span>\n<span style=\"color: #000000\">  hour: number,</span>\n<span style=\"color: #000000\">  minute: number</span>\n<span style=\"color: #000000\">): ClockInterface {</span>\n<span style=\"color: #000000\">  </span><span style=\"color: #0000FF\">return</span><span style=\"color: #000000\"> </span><span style=\"color: #0000FF\">new</span><span style=\"color: #000000\"> ctor(hour, minute);</span>\n<span style=\"color: #000000\">}</span>\n\n<span style=\"color: #0000FF\">class</span><span style=\"color: #000000\"> DigitalClock </span><span style=\"color: #0000FF\">implements</span><span style=\"color: #000000\"> ClockInterface {</span>\n<span style=\"color: #000000\">  </span><span style=\"color: #0000FF\">constructor</span><span style=\"color: #000000\">(h: number, m: number) {}</span>\n<span style=\"color: #000000\">  tick() {</span>\n<span style=\"color: #000000\">    console.log(</span><span style=\"color: #A31515\">\"beep beep\"</span><span style=\"color: #000000\">);</span>\n<span style=\"color: #000000\">  }</span>\n<span style=\"color: #000000\">}</span>\n<span style=\"color: #0000FF\">class</span><span style=\"color: #000000\"> AnalogClock </span><span style=\"color: #0000FF\">implements</span><span style=\"color: #000000\"> ClockInterface {</span>\n<span style=\"color: #000000\">  </span><span style=\"color: #0000FF\">constructor</span><span style=\"color: #000000\">(h: number, m: number) {}</span>\n<span style=\"color: #000000\">  tick() {</span>\n<span style=\"color: #000000\">    console.log(</span><span style=\"color: #A31515\">\"tick tock\"</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\"> digital = createClock(DigitalClock, </span><span style=\"color: #09835A\">12</span><span style=\"color: #000000\">, </span><span style=\"color: #09835A\">17</span><span style=\"color: #000000\">);</span>\n<span style=\"color: #0000FF\">let</span><span style=\"color: #000000\"> analog = createClock(AnalogClock, </span><span style=\"color: #09835A\">7</span><span style=\"color: #000000\">, </span><span style=\"color: #09835A\">32</span><span style=\"color: #000000\">);</span></code></div></pre>\n<p>Because <code>createClock</code>’s first parameter is of type <code>ClockConstructor</code>, in <code>createClock(AnalogClock, 7, 32)</code>, it checks that <code>AnalogClock</code> has the correct constructor signature.</p>\n<p>Another simple way is to use class expressions:</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\"> ClockConstructor {</span>\n<span style=\"color: #000000\">  </span><span style=\"color: #0000FF\">new</span><span style=\"color: #000000\"> (hour: number, minute: number);</span>\n<span style=\"color: #000000\">}</span>\n\n<span style=\"color: #0000FF\">interface</span><span style=\"color: #000000\"> ClockInterface {</span>\n<span style=\"color: #000000\">  tick();</span>\n<span style=\"color: #000000\">}</span>\n\n<span style=\"color: #0000FF\">const</span><span style=\"color: #000000\"> Clock: ClockConstructor = </span><span style=\"color: #0000FF\">class</span><span style=\"color: #000000\"> Clock </span><span style=\"color: #0000FF\">implements</span><span style=\"color: #000000\"> ClockInterface {</span>\n<span style=\"color: #000000\">  </span><span style=\"color: #0000FF\">constructor</span><span style=\"color: #000000\">(h: number, m: number) {}</span>\n<span style=\"color: #000000\">  tick() {</span>\n<span style=\"color: #000000\">    console.log(</span><span style=\"color: #A31515\">\"beep beep\"</span><span style=\"color: #000000\">);</span>\n<span style=\"color: #000000\">  }</span>\n<span style=\"color: #000000\">};</span></code></div></pre>\n<h1 id=\"extending-interfaces\" style=\"position:relative;\"><a href=\"#extending-interfaces\" aria-label=\"extending interfaces 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>Extending Interfaces</h1>\n<p>Like classes, interfaces can extend each other.\nThis allows you to copy the members of one interface into another, which gives you more flexibility in how you separate your interfaces into reusable components.</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\"> Shape {</span>\n<span style=\"color: #000000\">  color: string;</span>\n<span style=\"color: #000000\">}</span>\n\n<span style=\"color: #0000FF\">interface</span><span style=\"color: #000000\"> Square </span><span style=\"color: #0000FF\">extends</span><span style=\"color: #000000\"> Shape {</span>\n<span style=\"color: #000000\">  sideLength: number;</span>\n<span style=\"color: #000000\">}</span>\n\n<span style=\"color: #0000FF\">let</span><span style=\"color: #000000\"> square = {} </span><span style=\"color: #0000FF\">as</span><span style=\"color: #000000\"> Square;</span>\n<span style=\"color: #000000\">square.color = </span><span style=\"color: #A31515\">\"blue\"</span><span style=\"color: #000000\">;</span>\n<span style=\"color: #000000\">square.sideLength = </span><span style=\"color: #09835A\">10</span><span style=\"color: #000000\">;</span></code></div></pre>\n<p>An interface can extend multiple interfaces, creating a combination of all of the interfaces.</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\"> Shape {</span>\n<span style=\"color: #000000\">  color: string;</span>\n<span style=\"color: #000000\">}</span>\n\n<span style=\"color: #0000FF\">interface</span><span style=\"color: #000000\"> PenStroke {</span>\n<span style=\"color: #000000\">  penWidth: number;</span>\n<span style=\"color: #000000\">}</span>\n\n<span style=\"color: #0000FF\">interface</span><span style=\"color: #000000\"> Square </span><span style=\"color: #0000FF\">extends</span><span style=\"color: #000000\"> Shape, PenStroke {</span>\n<span style=\"color: #000000\">  sideLength: number;</span>\n<span style=\"color: #000000\">}</span>\n\n<span style=\"color: #0000FF\">let</span><span style=\"color: #000000\"> square = {} </span><span style=\"color: #0000FF\">as</span><span style=\"color: #000000\"> Square;</span>\n<span style=\"color: #000000\">square.color = </span><span style=\"color: #A31515\">\"blue\"</span><span style=\"color: #000000\">;</span>\n<span style=\"color: #000000\">square.sideLength = </span><span style=\"color: #09835A\">10</span><span style=\"color: #000000\">;</span>\n<span style=\"color: #000000\">square.penWidth = </span><span style=\"color: #09835A\">5.0</span><span style=\"color: #000000\">;</span></code></div></pre>\n<h1 id=\"hybrid-types\" style=\"position:relative;\"><a href=\"#hybrid-types\" aria-label=\"hybrid 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>Hybrid Types</h1>\n<p>As we mentioned earlier, interfaces can describe the rich types present in real world JavaScript.\nBecause of JavaScript’s dynamic and flexible nature, you may occasionally encounter an object that works as a combination of some of the types described above.</p>\n<p>One such example is an object that acts as both a function and an object, with additional properties:</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\"> Counter {</span>\n<span style=\"color: #000000\">  (start: number): string;</span>\n<span style=\"color: #000000\">  interval: number;</span>\n<span style=\"color: #000000\">  reset(): void;</span>\n<span style=\"color: #000000\">}</span>\n\n<span style=\"color: #0000FF\">function</span><span style=\"color: #000000\"> getCounter(): Counter {</span>\n<span style=\"color: #000000\">  </span><span style=\"color: #0000FF\">let</span><span style=\"color: #000000\"> counter = </span><span style=\"color: #0000FF\">function</span><span style=\"color: #000000\">(start: number) {} </span><span style=\"color: #0000FF\">as</span><span style=\"color: #000000\"> Counter;</span>\n<span style=\"color: #000000\">  counter.interval = </span><span style=\"color: #09835A\">123</span><span style=\"color: #000000\">;</span>\n<span style=\"color: #000000\">  counter.reset = </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\"> counter;</span>\n<span style=\"color: #000000\">}</span>\n\n<span style=\"color: #0000FF\">let</span><span style=\"color: #000000\"> c = getCounter();</span>\n<span style=\"color: #000000\">c(</span><span style=\"color: #09835A\">10</span><span style=\"color: #000000\">);</span>\n<span style=\"color: #000000\">c.reset();</span>\n<span style=\"color: #000000\">c.interval = </span><span style=\"color: #09835A\">5.0</span><span style=\"color: #000000\">;</span></code></div></pre>\n<p>When interacting with 3rd-party JavaScript, you may need to use patterns like the above to fully describe the shape of the type.</p>\n<h1 id=\"interfaces-extending-classes\" style=\"position:relative;\"><a href=\"#interfaces-extending-classes\" aria-label=\"interfaces extending 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>Interfaces Extending Classes</h1>\n<p>When an interface type extends a class type it inherits the members of the class but not their implementations.\nIt is as if the interface had declared all of the members of the class without providing an implementation.\nInterfaces inherit even the private and protected members of a base class.\nThis means that when you create an interface that extends a class with private or protected members, that interface type can only be implemented by that class or a subclass of it.</p>\n<p>This is useful when you have a large inheritance hierarchy, but want to specify that your code works with only subclasses that have certain properties.\nThe subclasses don’t have to be related besides inheriting from the base class.\nFor 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\"> Control {</span>\n<span style=\"color: #000000\">  </span><span style=\"color: #0000FF\">private</span><span style=\"color: #000000\"> state: any;</span>\n<span style=\"color: #000000\">}</span>\n\n<span style=\"color: #0000FF\">interface</span><span style=\"color: #000000\"> SelectableControl </span><span style=\"color: #0000FF\">extends</span><span style=\"color: #000000\"> Control {</span>\n<span style=\"color: #000000\">  select(): void;</span>\n<span style=\"color: #000000\">}</span>\n\n<span style=\"color: #0000FF\">class</span><span style=\"color: #000000\"> Button </span><span style=\"color: #0000FF\">extends</span><span style=\"color: #000000\"> Control </span><span style=\"color: #0000FF\">implements</span><span style=\"color: #000000\"> SelectableControl {</span>\n<span style=\"color: #000000\">  select() {}</span>\n<span style=\"color: #000000\">}</span>\n\n<span style=\"color: #0000FF\">class</span><span style=\"color: #000000\"> TextBox </span><span style=\"color: #0000FF\">extends</span><span style=\"color: #000000\"> Control {</span>\n<span style=\"color: #000000\">  select() {}</span>\n<span style=\"color: #000000\">}</span>\n\n<span style=\"color: #008000\">// Error: Property 'state' is missing in type 'Image'.</span>\n<span style=\"color: #0000FF\">class</span><span style=\"color: #000000\"> Image </span><span style=\"color: #0000FF\">implements</span><span style=\"color: #000000\"> SelectableControl {</span>\n<span style=\"color: #000000\">  </span><span style=\"color: #0000FF\">private</span><span style=\"color: #000000\"> state: any;</span>\n<span style=\"color: #000000\">  select() {}</span>\n<span style=\"color: #000000\">}</span>\n\n<span style=\"color: #0000FF\">class</span><span style=\"color: #000000\"> Location {}</span></code></div></pre>\n<p>In the above example, <code>SelectableControl</code> contains all of the members of <code>Control</code>, including the private <code>state</code> property.\nSince <code>state</code> is a private member it is only possible for descendants of <code>Control</code> to implement <code>SelectableControl</code>.\nThis is because only descendants of <code>Control</code> will have a <code>state</code> private member that originates in the same declaration, which is a requirement for private members to be compatible.</p>\n<p>Within the <code>Control</code> class it is possible to access the <code>state</code> private member through an instance of <code>SelectableControl</code>.\nEffectively, a <code>SelectableControl</code> acts like a <code>Control</code> that is known to have a <code>select</code> method.\nThe <code>Button</code> and <code>TextBox</code> classes are subtypes of <code>SelectableControl</code> (because they both inherit from <code>Control</code> and have a <code>select</code> method), but the <code>Image</code> and <code>Location</code> classes are not.</p>","headings":[{"value":"Introduction","depth":1},{"value":"Our First Interface","depth":1},{"value":"Optional Properties","depth":1},{"value":"Readonly properties","depth":1},{"value":"readonly vs const","depth":2},{"value":"Excess Property Checks","depth":1},{"value":"Function Types","depth":1},{"value":"Indexable Types","depth":1},{"value":"Class Types","depth":1},{"value":"Implementing an interface","depth":2},{"value":"Difference between the static and instance sides of classes","depth":2},{"value":"Extending Interfaces","depth":1},{"value":"Hybrid Types","depth":1},{"value":"Interfaces Extending Classes","depth":1}],"frontmatter":{"permalink":"/docs/handbook/interfaces.html","title":"Interfaces"}}},"pageContext":{"slug":"/docs/handbook/interfaces.html","isOldHandbook":true}}}