{"componentChunkName":"component---src-templates-documentation-tsx","path":"/docs/handbook/typescript-in-5-minutes.html","result":{"data":{"allSitePage":{"nodes":[{"path":"/dev-404-page/"},{"path":"/docs/handbook/nightly-builds.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/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/basic-types.html"},{"path":"/docs/handbook/classes.html"},{"path":"/docs/handbook/enums.html"},{"path":"/docs/handbook/functions.html"},{"path":"/docs/handbook/generics.html"},{"path":"/docs/handbook/interfaces.html"},{"path":"/docs/handbook/literal-types.html"},{"path":"/docs/handbook/intro.html"},{"path":"/docs/handbook/unions-and-intersections.html"},{"path":"/docs/handbook/declaration-files/dts-from-js.html"},{"path":"/docs/handbook/intro-to-js-ts.html"},{"path":"/docs/handbook/jsdoc-supported-types.html"},{"path":"/docs/handbook/type-checking-javascript-files.html"},{"path":"/docs/handbook/compiler-options-in-msbuild.html"},{"path":"/docs/handbook/compiler-options.html"},{"path":"/docs/handbook/configuring-watch.html"},{"path":"/docs/handbook/integrating-with-build-tools.html"},{"path":"/docs/handbook/project-references.html"},{"path":"/docs/handbook/tsconfig-json.html"},{"path":"/docs/handbook/advanced-types.html"},{"path":"/docs/handbook/declaration-merging.html"},{"path":"/docs/handbook/decorators.html"},{"path":"/docs/handbook/iterators-and-generators.html"},{"path":"/docs/handbook/jsx.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/symbols.html"},{"path":"/docs/handbook/triple-slash-directives.html"},{"path":"/docs/handbook/type-compatibility.html"},{"path":"/docs/handbook/type-inference.html"},{"path":"/docs/handbook/utility-types.html"},{"path":"/docs/handbook/variable-declarations.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-1.html"},{"path":"/docs/handbook/release-notes/typescript-2-2.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/release-notes/typescript-3-9.html"},{"path":"/docs/handbook/asp-net-core.html"},{"path":"/docs/handbook/angular.html"},{"path":"/docs/handbook/babel-with-typescript.html"},{"path":"/docs/handbook/dom-manipulation.html"},{"path":"/docs/handbook/gulp.html"},{"path":"/docs/handbook/migrating-from-javascript.html"},{"path":"/docs/handbook/react.html"},{"path":"/docs/handbook/typescript-tooling-in-5-minutes.html"},{"path":"/vo/docs/handbook/basic-types.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":"/vo/tsconfig"},{"path":"/ja/tsconfig"},{"path":"/pt/tsconfig"},{"path":"/play"},{"path":"/en/play"},{"path":"/es/play"},{"path":"/ja/play"},{"path":"/pt/play"},{"path":"/vo/play"},{"path":"/zh/play"},{"path":"/fa/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/4-0/new-checks/class-constructor-code-flow.ts"},{"path":"/play/4-0/new-js-features/jsdoc-deprecated.ts"},{"path":"/play/4-0/new-js-features/logical-operators-and-assignment.ts"},{"path":"/play/4-0/new-js-features/nullish-coalescing.ts"},{"path":"/play/4-0/new-ts-features/named-tuples.ts"},{"path":"/play/4-0/new-ts-features/unknown-in-catch.ts"},{"path":"/play/4-0/new-ts-features/variadic-tuples.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/language/soundness.ts"},{"path":"/es/play/typescript/language/structural-typing.ts"},{"path":"/es/play/typescript/language/type-guards.ts"},{"path":"/es/play/typescript/language/type-widening-and-narrowing.ts"},{"path":"/es/play/typescript/meta-types/conditional-types.ts"},{"path":"/es/play/typescript/meta-types/discriminate-types.ts"},{"path":"/es/play/typescript/meta-types/indexed-types.ts"},{"path":"/es/play/typescript/meta-types/mapped-types.ts"},{"path":"/es/play/typescript/primitives/any.ts"},{"path":"/es/play/typescript/primitives/literals.ts"},{"path":"/es/play/typescript/primitives/union-and-intersection-types.ts"},{"path":"/es/play/typescript/primitives/unknown-and-never.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":"/fa/play/javascript/javascript-essentials/hello-world.ts"},{"path":"/ja/play/3-7/fixits/big-number-literals.ts"},{"path":"/ja/play/3-7/syntax-and-messaging/nullish-coalescing.ts"},{"path":"/ja/play/3-7/syntax-and-messaging/optional-chaining.ts"},{"path":"/ja/play/3-8/breaking-changes/checking-unions-with-index-signatures.ts"},{"path":"/ja/play/javascript/external-apis/typescript-with-deno.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/primitives/unknown-and-never.ts"},{"path":"/ja/play/typescript/type-primitives/built-in-utility-types.ts"},{"path":"/ja/play/typescript/type-primitives/nullable-types.ts"},{"path":"/ja/play/typescript/type-primitives/tuples.ts"},{"path":"/pt/play/javascript/javascript-essentials/code-flow.ts"},{"path":"/pt/play/javascript/javascript-essentials/functions.ts"},{"path":"/pt/play/javascript/javascript-essentials/hello-world.ts"},{"path":"/pt/play/javascript/javascript-essentials/objects-and-arrays.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/javascript-essentials/objects-and-arrays.ts"},{"path":"/zh/play/javascript/modern-javascript/async-await.ts"},{"path":"/zh/play/javascript/modern-javascript/immutability.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":"/ja/play/3-7/syntax-and-messaging/flattened-error-reporting.ts"},{"path":"/ja/play/3-7/fixits/infer-from-usage-changes.ts"},{"path":"/ja/play/3-7/fixits/const-to-let.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/bootstrap"},{"path":"/es/docs/bootstrap"},{"path":"/ja/docs/bootstrap"},{"path":"/vo/docs/bootstrap"},{"path":"/zh/docs/bootstrap"},{"path":"/docs/"},{"path":"/es/docs/"},{"path":"/ja/docs/"},{"path":"/vo/docs/"},{"path":"/zh/docs/"},{"path":"/docs/handbook/"},{"path":"/es/docs/handbook/"},{"path":"/ja/docs/handbook/"},{"path":"/vo/docs/handbook/"},{"path":"/zh/docs/handbook/"},{"path":"/branding/"},{"path":"/dev/bug-workbench/"},{"path":"/dev/playground-plugins/"},{"path":"/dev/sandbox/"},{"path":"/dev/twoslash/"},{"path":"/dev/typescript-vfs/"},{"path":"/upcoming/"}]},"markdownRemark":{"id":"fd76824c-d7c6-5bb3-84a6-77757d94d5aa","excerpt":"The relationship between TypeScript and JavaScript is rather unique among modern programming languages.\nTypeScript sits as a layer on-top of JavaScript…","html":"<p>The relationship between TypeScript and JavaScript is rather unique among modern programming languages.\nTypeScript sits as a layer on-top of JavaScript, offering the features of JavaScript and then adds its own layer on top of that. This layer is the TypeScript type system.</p>\n<p>JavaScript already has a set of language primitives like <code>string</code>, <code>number</code>, <code>object</code>, <code>undefined</code> etc, however there are no ahead-of-time checks that these are consistently assigned across your whole codebase. TypeScript acts as that layer.</p>\n<p>This means that your existing working JavaScript code is also TypeScript code, however TypeScript’s type-checker might highlight discrepancies between what you thought was happening and what the JavaScript language does.</p>\n<p>This tutorial tries to give you a 5 minute overview of the type-system, with a focus on understanding the type-system language extensions which TypeScript adds.</p>\n<h2 id=\"types-by-inference\" style=\"position:relative;\"><a href=\"#types-by-inference\" aria-label=\"types by inference 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>Types by Inference</h2>\n<p>TypeScript knows the JavaScript language and will generate types for you in many cases.\nFor example in creating a variable and assigning it to a particular value, TypeScript will use the value as its type.</p>\n<pre class=\"shiki twoslash\"><div class=\"language-id\">ts</div><div class='code-container'><code><span style=\"color: #0000FF\">let</span><span style=\"color: #000000\"> </span><span style=\"color: #1A1A1A\"><data-lsp lsp='let helloWorld: string'>helloWorld</data-lsp> </span><span style=\"color: #000000\">= </span><span style=\"color: #A31515\">\"Hello World\"</span><span style=\"color: #000000\">;</span>\n<span class='query'>//  ^ = let helloWorld: string</span></code><a href='https://www.typescriptlang.org/play/#code/DYUwLgBAFizA9gdXgJ2AEwgXggIgBKwITJrq4DcAUAPQ0QQB6A-EA'>Try</a></div></pre>\n<p>By understanding how JavaScript works, TypeScript can build a type-system which accepts JavaScript code but has types. This offers a type-system without needing to add extra characters to make types explicit in your code. Which is how TypeScript knows that <code>helloWorld</code> is a <code>string</code> in the above example.</p>\n<p>It’s quite possible that you have used VS Code with JavaScript, and had editor auto-completion as you worked.\nThat is because the understanding of JavaScript baked into TypeScript has been used under-the-hood to improve working with JavaScript.</p>\n<h2 id=\"defining-types\" style=\"position:relative;\"><a href=\"#defining-types\" aria-label=\"defining 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>Defining Types</h2>\n<p>JavaScript is a dynamic language which allows for a lot of design patterns. Some design patterns can be hard to provide types for automatically (because they might use dynamic programming) in those cases TypeScript supports an extension of the JavaScript language which offers places for you to tell TypeScript what the types should be.</p>\n<p>Here is an example of creating an object which has an inferred type which includes <code>name: string</code> and <code>id: number</code>:</p>\n<pre class=\"shiki twoslash\"><div class=\"language-id\">ts</div><div class='code-container'><code><span style=\"color: #0000FF\">const</span><span style=\"color: #000000\"> </span><span style=\"color: #1A1A1A\"><data-lsp lsp='const user: {&amp;#13;    name: string;&amp;#13;    id: number;&amp;#13;}'>user</data-lsp> </span><span style=\"color: #000000\">= {</span>\n<span style=\"color: #000000\">  <data-lsp lsp='(property) name: string'>name</data-lsp>: </span><span style=\"color: #A31515\">\"Hayes\"</span><span style=\"color: #000000\">,</span>\n<span style=\"color: #000000\">  <data-lsp lsp='(property) id: number'>id</data-lsp>: </span><span style=\"color: #09835A\">0</span><span style=\"color: #000000\">,</span>\n<span style=\"color: #000000\">};</span></code><a href='https://www.typescriptlang.org/play/#code/MYewdgzgLgBArhApgJxgXhgbwFAxmAQwFtEAuGAIgAkCBPRCCgGlxgEsATcgBhYF8A3EA'>Try</a></div></pre>\n<p>An explicit way to describe this object’s shape is via an <code>interface</code> declaration:</p>\n<pre class=\"shiki twoslash\"><div class=\"language-id\">ts</div><div class='code-container'><code><span style=\"color: #0000FF\">interface</span><span style=\"color: #000000\"> <data-lsp lsp='interface User'>User</data-lsp> {</span>\n<span style=\"color: #000000\">  </span><span style=\"color: #1A1A1A\"><data-lsp lsp='(property) User.name: string'>name</data-lsp></span><span style=\"color: #000000\">: string;</span>\n<span style=\"color: #000000\">  </span><span style=\"color: #1A1A1A\"><data-lsp lsp='(property) User.id: number'>id</data-lsp></span><span style=\"color: #000000\">: number;</span>\n<span style=\"color: #000000\">}</span></code><a href='https://www.typescriptlang.org/play/#code/JYOwLgpgTgZghgYwgAgKoGdrIN4ChnIhwC2EAXMumFKAOYDc+ywAJhSAK7EBG0jAvkA'>Try</a></div></pre>\n<p>You can then declare that a JavaScript object conforms to that shape of your new <code>interface</code> by using syntax like <code>: TypeName</code> after a variable declaration:</p>\n<pre class=\"shiki twoslash\"><div class=\"language-id\">ts</div><div class='code-container'><code><span style=\"color: #0000FF\">const</span><span style=\"color: #000000\"> </span><span style=\"color: #1A1A1A\"><data-lsp lsp='const user: User'>user</data-lsp></span><span style=\"color: #000000\">:</span><span style=\"color: #1A1A1A\"> <data-lsp lsp='interface User'>User</data-lsp> </span><span style=\"color: #000000\">= {</span>\n<span style=\"color: #000000\">  <data-lsp lsp='(property) User.name: string'>name</data-lsp>: </span><span style=\"color: #A31515\">\"Hayes\"</span><span style=\"color: #000000\">,</span>\n<span style=\"color: #000000\">  <data-lsp lsp='(property) User.id: number'>id</data-lsp>: </span><span style=\"color: #09835A\">0</span><span style=\"color: #000000\">,</span>\n<span style=\"color: #000000\">};</span></code><a href='https://www.typescriptlang.org/play/#code/JYOwLgpgTgZghgYwgAgKoGdrIN4ChnIhwC2EAXMumFKAOYDc+ywAJhSAK7EBG0jAvrgD0Q5AFoJCDmAljcCAPYgqyDpigUMWALw4mRUhQBEACTgBPCOiMAaJqwoAGO-3pA'>Try</a></div></pre>\n<p>TypeScript will warn you if you provide an object which doesn’t match the interface you have provided:</p>\n<pre class=\"shiki twoslash\"><div class=\"language-id\">ts</div><div class='code-container'><code><span style=\"color: #0000FF\">interface</span><span style=\"color: #000000\"> <data-lsp lsp='interface User'>User</data-lsp> {</span>\n<span style=\"color: #000000\">  </span><span style=\"color: #1A1A1A\"><data-lsp lsp='(property) User.name: string'>name</data-lsp></span><span style=\"color: #000000\">: string;</span>\n<span style=\"color: #000000\">  </span><span style=\"color: #1A1A1A\"><data-lsp lsp='(property) User.id: number'>id</data-lsp></span><span style=\"color: #000000\">: number;</span>\n<span style=\"color: #000000\">}</span>\n\n<span style=\"color: #0000FF\">const</span><span style=\"color: #000000\"> </span><span style=\"color: #1A1A1A\"><data-lsp lsp='const user: User'>user</data-lsp></span><span style=\"color: #000000\">:</span><span style=\"color: #1A1A1A\"> <data-lsp lsp='interface User'>User</data-lsp> </span><span style=\"color: #000000\">= {</span>\n<span style=\"color: #000000\">  <data-lsp lsp='(property) username: string'>username</data-lsp>: </span><span style=\"color: #A31515\">\"Hayes\"</span><span style=\"color: #000000\">,</span>\n<span class=\"error\"><span>Type '{ username: string; id: number; }' is not assignable to type 'User'.\n  Object literal may only specify known properties, and 'username' does not exist in type 'User'.</span><span class=\"code\">2322</span></span><span class=\"error-behind\">Type '{ username: string; id: number; }' is not assignable to type 'User'.\n  Object literal may only specify known properties, and 'username' does not exist in type 'User'.</span><span style=\"color: #000000\">  <data-lsp lsp='(property) User.id: number'>id</data-lsp>: </span><span style=\"color: #09835A\">0</span><span style=\"color: #000000\">,</span>\n<span style=\"color: #000000\">};</span></code><a href='https://www.typescriptlang.org/play/#code/PTAEAEFMCdoe2gZwFygEwGY1oFAEsA7AFxgDMBDAY0lAFVEZQBvHUUA8gW0lUSOkIBzANytQeACaoCAV04AjGKIC+OHJTgE+oGQ2ip6jALzMxumB26oARAAlyAT0iJrAGjGTUABnfLhQA'>Try</a></div></pre>\n<p>Because JavaScript supports classes and object-oriented programming, so does TypeScript - an interface declaration can also be used with classes:</p>\n<pre class=\"shiki twoslash\"><div class=\"language-id\">ts</div><div class='code-container'><code><span style=\"color: #0000FF\">interface</span><span style=\"color: #000000\"> <data-lsp lsp='interface User'>User</data-lsp> {</span>\n<span style=\"color: #000000\">  </span><span style=\"color: #1A1A1A\"><data-lsp lsp='(property) User.name: string'>name</data-lsp></span><span style=\"color: #000000\">: string;</span>\n<span style=\"color: #000000\">  </span><span style=\"color: #1A1A1A\"><data-lsp lsp='(property) User.id: number'>id</data-lsp></span><span style=\"color: #000000\">: number;</span>\n<span style=\"color: #000000\">}</span>\n\n<span style=\"color: #0000FF\">class</span><span style=\"color: #000000\"> <data-lsp lsp='class UserAccount'>UserAccount</data-lsp> {</span>\n<span style=\"color: #000000\">  </span><span style=\"color: #1A1A1A\"><data-lsp lsp='(property) UserAccount.name: string'>name</data-lsp></span><span style=\"color: #000000\">: string;</span>\n<span style=\"color: #000000\">  </span><span style=\"color: #1A1A1A\"><data-lsp lsp='(property) UserAccount.id: number'>id</data-lsp></span><span style=\"color: #000000\">: number;</span>\n\n<span style=\"color: #000000\">  </span><span style=\"color: #0000FF\">constructor</span><span style=\"color: #000000\">(</span><span style=\"color: #1A1A1A\"><data-lsp lsp='(parameter) name: string'>name</data-lsp></span><span style=\"color: #000000\">: string, </span><span style=\"color: #1A1A1A\"><data-lsp lsp='(parameter) id: number'>id</data-lsp></span><span style=\"color: #000000\">: number) {</span>\n<span style=\"color: #000000\">    </span><span style=\"color: #0000FF\">this</span><span style=\"color: #000000\">.<data-lsp lsp='(property) UserAccount.name: string'>name</data-lsp> = </span><span style=\"color: #1A1A1A\"><data-lsp lsp='(parameter) name: string'>name</data-lsp></span><span style=\"color: #000000\">;</span>\n<span style=\"color: #000000\">    </span><span style=\"color: #0000FF\">this</span><span style=\"color: #000000\">.<data-lsp lsp='(property) UserAccount.id: number'>id</data-lsp> = </span><span style=\"color: #1A1A1A\"><data-lsp lsp='(parameter) id: number'>id</data-lsp></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\"> </span><span style=\"color: #1A1A1A\"><data-lsp lsp='const user: User'>user</data-lsp></span><span style=\"color: #000000\">:</span><span style=\"color: #1A1A1A\"> <data-lsp lsp='interface User'>User</data-lsp> </span><span style=\"color: #000000\">= </span><span style=\"color: #0000FF\">new</span><span style=\"color: #000000\"> <data-lsp lsp='constructor UserAccount(name: string, id: number): UserAccount'>UserAccount</data-lsp>(</span><span style=\"color: #A31515\">\"Murphy\"</span><span style=\"color: #000000\">, </span><span style=\"color: #09835A\">1</span><span style=\"color: #000000\">);</span></code><a href='https://www.typescriptlang.org/play/#code/JYOwLgpgTgZghgYwgAgKoGdrIN4ChnIhwC2EAXMumFKAOYDc+ywAJhSAK7EBG0jAvrlwIANnHTo0mKAEEECAPYdwOJkVIUqNEAyat2XXlEZNFILRwRgFUABTrylanQA0zNoUPQAlKoIEwAAtgdAA6B2QAXkISCEZ-ZCCQ0NYo93jkQUFhBXMwZA5pCgwsaJAIAHcpaDlFZTBbACIAWQ4oAAdAgE9GtwBGb3ogA'>Try</a></div></pre>\n<p>Interfaces can be used to annotate parameters and return values to functions:</p>\n<pre class=\"shiki twoslash\"><div class=\"language-id\">ts</div><div class='code-container'><code><span style=\"color: #0000FF\">function</span><span style=\"color: #000000\"> <data-lsp lsp='function getAdminUser(): User'>getAdminUser</data-lsp>(): <data-lsp lsp='interface User'>User</data-lsp> {</span>\n<span style=\"color: #000000\">  </span><span style=\"color: #008000\">//...</span>\n<span style=\"color: #000000\">}</span>\n\n<span style=\"color: #0000FF\">function</span><span style=\"color: #000000\"> <data-lsp lsp='function deleteUser(user: User): void'>deleteUser</data-lsp>(</span><span style=\"color: #1A1A1A\"><data-lsp lsp='(parameter) user: User'>user</data-lsp></span><span style=\"color: #000000\">: <data-lsp lsp='interface User'>User</data-lsp>) {</span>\n<span style=\"color: #000000\">  </span><span style=\"color: #008000\">// ...</span>\n<span style=\"color: #000000\">}</span></code><a href='https://www.typescriptlang.org/play/#code/PTAEAEDsHsFECd7XgZwFAEtIBcCm8AzAQwGNdQBVFfUAbzVFEiIFtcAuUFbeLAcwDcDUBgAmnSAFcWAI3xCAvmhCgAtOpKTs61WgKTIJbBmiRQfXNgCCollir4AFAEpOD+HWEgAdL7RK9AyMTM1FcABtLXHdHSWp4N3jnT0YVX29-IA'>Try</a></div></pre>\n<p>There are already a small set of primitive types available in JavaScript: <code>boolean</code>, <code>bigint</code>, <code>null</code>, <code>number</code>, <code>string</code>, <code>symbol</code>, <code>object</code> and <code>undefined</code>, which you can use in an interface. TypeScript extends this list with a few more. for example: <code>any</code> (allow anything), <a href=\"/en/play#example/unknown-and-never\"><code>unknown</code></a> (ensure someone using this type declares what the type is), <a href=\"/en/play#example/unknown-and-never\"><code>never</code></a> (it’s not possible that this type could happen) <code>void</code> (a function which returns <code>undefined</code> or has no return value).</p>\n<p>You’ll see quite quickly that there are two syntaxes for building types: <a href=\"/play/?e=83#example/types-vs-interfaces\">Interfaces and Types</a> - you should prefer <code>interface</code>, and use <code>type</code> when you need specific features.</p>\n<h2 id=\"composing-types\" style=\"position:relative;\"><a href=\"#composing-types\" aria-label=\"composing 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>Composing Types</h2>\n<p>Similar to how you would create larger complex objects by composing them together TypeScript has tools for doing this with types.\nThe two most popular techniques you would use in everyday code to create new types by working with many smaller types are Unions and Generics.</p>\n<h3 id=\"unions\" style=\"position:relative;\"><a href=\"#unions\" aria-label=\"unions 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>Unions</h3>\n<p>A union is a way to declare that a type could be one of many types. For example, you could describe a <code>boolean</code> type as being either <code>true</code> or <code>false</code>:</p>\n<pre class=\"shiki twoslash\"><div class=\"language-id\">ts</div><div class='code-container'><code><span style=\"color: #0000FF\">type</span><span style=\"color: #000000\"> <data-lsp lsp='type MyBool = boolean'>MyBool</data-lsp> = true | false;</span></code><a href='https://www.typescriptlang.org/play/#code/C4TwDgpgBAsiBCB7RAbKBeKwBOBXaAPlAGYCGKAzhANxA'>Try</a></div></pre>\n<p><em>Note:</em> If you hover over <code>MyBool</code> above, you’ll see that it is classed as <code>boolean</code> - that’s a property of the Structural Type System, which we’ll get to later.</p>\n<p>One of the most popular use-cases for union types is to describe a set of <code>string</code>s or <code>number</code>s <a href=\"/docs/handbook/literal-types.html\">literal</a> which a value is allowed to be:</p>\n<pre class=\"shiki twoslash\"><div class=\"language-id\">ts</div><div class='code-container'><code><span style=\"color: #0000FF\">type</span><span style=\"color: #000000\"> <data-lsp lsp='type WindowStates = &amp;quot;open&amp;quot; | &amp;quot;closed&amp;quot; | &amp;quot;minimized&amp;quot;'>WindowStates</data-lsp> = </span><span style=\"color: #A31515\">\"open\"</span><span style=\"color: #000000\"> | </span><span style=\"color: #A31515\">\"closed\"</span><span style=\"color: #000000\"> | </span><span style=\"color: #A31515\">\"minimized\"</span><span style=\"color: #000000\">;</span>\n<span style=\"color: #0000FF\">type</span><span style=\"color: #000000\"> <data-lsp lsp='type LockStates = &amp;quot;locked&amp;quot; | &amp;quot;unlocked&amp;quot;'>LockStates</data-lsp> = </span><span style=\"color: #A31515\">\"locked\"</span><span style=\"color: #000000\"> | </span><span style=\"color: #A31515\">\"unlocked\"</span><span style=\"color: #000000\">;</span>\n<span style=\"color: #0000FF\">type</span><span style=\"color: #000000\"> <data-lsp lsp='type OddNumbersUnderTen = 1 | 3 | 5 | 7 | 9'>OddNumbersUnderTen</data-lsp> = </span><span style=\"color: #09835A\">1</span><span style=\"color: #000000\"> | </span><span style=\"color: #09835A\">3</span><span style=\"color: #000000\"> | </span><span style=\"color: #09835A\">5</span><span style=\"color: #000000\"> | </span><span style=\"color: #09835A\">7</span><span style=\"color: #000000\"> | </span><span style=\"color: #09835A\">9</span><span style=\"color: #000000\">;</span></code><a href='https://www.typescriptlang.org/play/#code/C4TwDgpgBA6glgOwCYHsDuBlYBDYEDOUAvFAEQqQKlQA+ZAxgDYr4RLV2kC2icPAXm1IBuAFChIUADIp6Aayy4CxMs3lDaZAK4I1coWInQA8kiQA5LVwBGEAE74AqsnsAVCAhUBGTQGZNAKyaAOyaAJzCQA'>Try</a></div></pre>\n<p>Unions provide a way to handle different types too, for example you may have a function which accepts an <code>array</code> or a <code>string</code>.</p>\n<pre class=\"shiki twoslash\"><div class=\"language-id\">ts</div><div class='code-container'><code><span style=\"color: #0000FF\">function</span><span style=\"color: #000000\"> <data-lsp lsp='function getLength(obj: string | string[]): number'>getLength</data-lsp>(</span><span style=\"color: #1A1A1A\"><data-lsp lsp='(parameter) obj: string | string[]'>obj</data-lsp></span><span style=\"color: #000000\">: string | string[]) {</span>\n<span style=\"color: #000000\">  </span><span style=\"color: #0000FF\">return</span><span style=\"color: #000000\"> </span><span style=\"color: #1A1A1A\"><data-lsp lsp='(parameter) obj: string | string[]'>obj</data-lsp></span><span style=\"color: #000000\">.<data-lsp lsp='(property) length: number'>length</data-lsp>;</span>\n<span style=\"color: #000000\">}</span></code><a href='https://www.typescriptlang.org/play/#code/GYVwdgxgLglg9mABAcwKZQDKrMqALACjgCMArALkQGcoAnGHRAH2roeQG0BdASkQG8AUIkS10IWkhKkAdABtsuPAG5BAXyA'>Try</a></div></pre>\n<p>TypeScript understands how code changes what the variable could be with time, you can use these checks to narrow the type down.</p>\n<table>\n<thead>\n<tr>\n<th>Type</th>\n<th>Predicate</th>\n</tr>\n</thead>\n<tbody>\n<tr>\n<td>string</td>\n<td><code>typeof s === \"string\"</code></td>\n</tr>\n<tr>\n<td>number</td>\n<td><code>typeof n === \"number\"</code></td>\n</tr>\n<tr>\n<td>boolean</td>\n<td><code>typeof b === \"boolean\"</code></td>\n</tr>\n<tr>\n<td>undefined</td>\n<td><code>typeof undefined === \"undefined\"</code></td>\n</tr>\n<tr>\n<td>function</td>\n<td><code>typeof f === \"function\"</code></td>\n</tr>\n<tr>\n<td>array</td>\n<td><code>Array.isArray(a)</code></td>\n</tr>\n</tbody>\n</table>\n<p>For example, you could differentiate between a <code>string</code> and an <code>array</code>, using <code>typeof obj === \"string\"</code> and TypeScript will know what the object is down different code paths.</p>\n<!-- prettier-ignore -->\n<pre class=\"shiki twoslash\"><div class=\"language-id\">ts</div><div class='code-container'><code><span style=\"color: #0000FF\">function</span><span style=\"color: #000000\"> <data-lsp lsp='function wrapInArray(obj: string | string[]): string[]'>wrapInArray</data-lsp>(</span><span style=\"color: #1A1A1A\"><data-lsp lsp='(parameter) obj: string | string[]'>obj</data-lsp></span><span style=\"color: #000000\">: string | string[]) {</span>\n<span style=\"color: #000000\">  </span><span style=\"color: #0000FF\">if</span><span style=\"color: #000000\"> (</span><span style=\"color: #0000FF\">typeof</span><span style=\"color: #000000\"> </span><span style=\"color: #1A1A1A\"><data-lsp lsp='(parameter) obj: string | string[]'>obj</data-lsp></span><span style=\"color: #000000\"> === </span><span style=\"color: #A31515\">\"string\"</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: #1A1A1A\"><data-lsp lsp='(parameter) obj: string'>obj</data-lsp></span><span style=\"color: #000000\">];</span>\n<span class='query'>//          ^ = (parameter) obj: string</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\"> </span><span style=\"color: #1A1A1A\"><data-lsp lsp='(parameter) obj: string[]'>obj</data-lsp></span><span style=\"color: #000000\">;</span>\n<span style=\"color: #000000\">  }</span>\n<span style=\"color: #000000\">}</span></code><a href='https://www.typescriptlang.org/play/#code/GYVwdgxgLglg9mABAdwE4EMAOBJMBBVDATwAo4AjAKwC5EBnKVGMAc0QB97HmWBtAXQCUiAN4AoRIhjBEJKEUwBTODIqVEAXi2IARAyasdw8ZMmpFUEKiS81-ANxiA9E9NvTAPQD8ExAF9ERQAbOkVRXzMLKyQ1R0k-MT8gA'>Try</a></div></pre>\n<h3 id=\"generics\" style=\"position:relative;\"><a href=\"#generics\" aria-label=\"generics permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>Generics</h3>\n<p>You can get very deep into the TypeScript generic system, but at a 1 minute high-level explanation, generics are a way to provide variables to types.</p>\n<p>A common example is an array, an array without generics could contain anything. An array with generics can describe what values are inside in the array.</p>\n<pre class=\"shiki\"><div class=\"language-id\">ts</div><div class='code-container'><code><span style=\"color: #0000FF\">type</span><span style=\"color: #000000\"> StringArray = Array&lt;string&gt;;</span>\n<span style=\"color: #0000FF\">type</span><span style=\"color: #000000\"> NumberArray = Array&lt;number&gt;;</span>\n<span style=\"color: #0000FF\">type</span><span style=\"color: #000000\"> ObjectWithNameArray = Array&lt;{ </span><span style=\"color: #1A1A1A\">name</span><span style=\"color: #000000\">: string }&gt;;</span></code></div></pre>\n<p>You can declare your own types which use generics:</p>\n<pre class=\"shiki twoslash\"><div class=\"language-id\">ts</div><div class='code-container'><code><span style=\"color: #0000FF\">interface</span><span style=\"color: #000000\"> <data-lsp lsp='interface Backpack&amp;lt;Type>'>Backpack</data-lsp>&lt;<data-lsp lsp='(type parameter) Type in Backpack&amp;lt;Type>'>Type</data-lsp>> {</span>\n<span style=\"color: #000000\">  <data-lsp lsp='(property) Backpack&amp;lt;Type>.add: (obj: Type) => void'>add</data-lsp>: (</span><span style=\"color: #1A1A1A\"><data-lsp lsp='(parameter) obj: Type'>obj</data-lsp></span><span style=\"color: #000000\">: <data-lsp lsp='(type parameter) Type in Backpack&amp;lt;Type>'>Type</data-lsp>) </span><span style=\"color: #0000FF\">=&gt;</span><span style=\"color: #000000\"> void;</span>\n<span style=\"color: #000000\">  <data-lsp lsp='(property) Backpack&amp;lt;Type>.get: () => Type'>get</data-lsp>: () </span><span style=\"color: #0000FF\">=&gt;</span><span style=\"color: #000000\"> <data-lsp lsp='(type parameter) Type in Backpack&amp;lt;Type>'>Type</data-lsp>;</span>\n<span style=\"color: #000000\">}</span>\n\n<span style=\"color: #008000\">// This line is a shortcut to tell TypeScript there is a</span>\n<span style=\"color: #008000\">// constant called `backpack`, and to not worry about where it came from</span>\n<span style=\"color: #0000FF\">declare</span><span style=\"color: #000000\"> </span><span style=\"color: #0000FF\">const</span><span style=\"color: #000000\"> </span><span style=\"color: #1A1A1A\"><data-lsp lsp='const backpack: Backpack&amp;lt;string>'>backpack</data-lsp></span><span style=\"color: #000000\">:</span><span style=\"color: #1A1A1A\"> <data-lsp lsp='interface Backpack&amp;lt;Type>'>Backpack</data-lsp>&lt;string></span><span style=\"color: #000000\">;</span>\n\n<span style=\"color: #008000\">// object is a string, because we declared it above as the variable part of Backpack</span>\n<span style=\"color: #0000FF\">const</span><span style=\"color: #000000\"> </span><span style=\"color: #1A1A1A\"><data-lsp lsp='const object: string'>object</data-lsp> </span><span style=\"color: #000000\">= </span><span style=\"color: #1A1A1A\"><data-lsp lsp='const backpack: Backpack&amp;lt;string>'>backpack</data-lsp></span><span style=\"color: #000000\">.<data-lsp lsp='(property) Backpack&amp;lt;string>.get: () => string'>get</data-lsp>();</span>\n\n<span style=\"color: #008000\">// Due to backpack variable being a string, you cannot pass a number to the add function</span>\n<span style=\"color: #1A1A1A\"><data-lsp lsp='const backpack: Backpack&amp;lt;string>'>backpack</data-lsp></span><span style=\"color: #000000\">.<data-lsp lsp='(property) Backpack&amp;lt;string>.add: (obj: string) => void'>add</data-lsp>(</span><span style=\"color: #09835A\"><data-err>23</data-err></span><span style=\"color: #000000\">);</span>\n<span class=\"error\"><span>Argument of type 'number' is not assignable to parameter of type 'string'.</span><span class=\"code\">2345</span></span><span class=\"error-behind\">Argument of type 'number' is not assignable to parameter of type 'string'.</span></code><a href='https://www.typescriptlang.org/play/#code/PTAEAEFMCdoe2gZwFygEwGYAsBWAUAJYB2ALjAGYCGAxpKAEI0DWADswDwAqAni5AHygA3nlChKAEwmoAFHABGAK1Q8+ASlABeQQDc4BCQG5RoAOaQSsjdtCrIxgL548IWwAsCiUABtidT+KgiG4IJNQAriSgJHDRkN7etryQAMrU0AQsUSRuMP5elC5g1HBEiCSUpKDUlAmQEqAABvLMbNRMjQA04kQNMaBEcFEA7gjQ3OLycJGgw7nQ-lE1ALZ05PDLeBKQ1N6UC9Wl5aAt7W1MqIxnHOUZRKb8xkWgCoo7UQGUQSR3pt3yO0o4UQdGGdG2u329VABCilCmOjolC8OToOn2BHh3jobGgUTg5AYrWYeBKZXxSneWhOxPaADpzCQZGonq4ACLhOj9U6sZigdEZLF0AHEUyBW6i7rcabVSqDKJsRAFAbhZYA6DRWKo8RSUDkcJEagkAilPA8850yQSGSYFlAA'>Try</a></div></pre>\n<h2 id=\"structural-type-system\" style=\"position:relative;\"><a href=\"#structural-type-system\" aria-label=\"structural type system 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>Structural Type System</h2>\n<p>One of TypeScript’s core principles is that type checking focuses on the <em>shape</em> which values have.\nThis is sometimes called “duck typing” or “structural typing”.</p>\n<p>In a structural type system if two objects have the same shape, they are considered the same.</p>\n<pre class=\"shiki twoslash\"><div class=\"language-id\">ts</div><div class='code-container'><code><span style=\"color: #0000FF\">interface</span><span style=\"color: #000000\"> <data-lsp lsp='interface Point'>Point</data-lsp> {</span>\n<span style=\"color: #000000\">  </span><span style=\"color: #1A1A1A\"><data-lsp lsp='(property) Point.x: number'>x</data-lsp></span><span style=\"color: #000000\">: number;</span>\n<span style=\"color: #000000\">  </span><span style=\"color: #1A1A1A\"><data-lsp lsp='(property) Point.y: number'>y</data-lsp></span><span style=\"color: #000000\">: number;</span>\n<span style=\"color: #000000\">}</span>\n\n<span style=\"color: #0000FF\">function</span><span style=\"color: #000000\"> <data-lsp lsp='function printPoint(p: Point): void'>printPoint</data-lsp>(</span><span style=\"color: #1A1A1A\"><data-lsp lsp='(parameter) p: Point'>p</data-lsp></span><span style=\"color: #000000\">: <data-lsp lsp='interface Point'>Point</data-lsp>) {</span>\n<span style=\"color: #000000\">  <data-lsp lsp='var console: Console'>console</data-lsp>.<data-lsp lsp='(method) Console.log(...data: any[]): void'>log</data-lsp>(</span><span style=\"color: #A31515\">`</span><span style=\"color: #0000FF\">${</span><span style=\"color: #1A1A1A\"><data-lsp lsp='(parameter) p: Point'>p</data-lsp></span><span style=\"color: #A31515\">.<data-lsp lsp='(property) Point.x: number'>x</data-lsp></span><span style=\"color: #0000FF\">}</span><span style=\"color: #A31515\">, </span><span style=\"color: #0000FF\">${</span><span style=\"color: #1A1A1A\"><data-lsp lsp='(parameter) p: Point'>p</data-lsp></span><span style=\"color: #A31515\">.<data-lsp lsp='(property) Point.y: number'>y</data-lsp></span><span style=\"color: #0000FF\">}</span><span style=\"color: #A31515\">`</span><span style=\"color: #000000\">);</span>\n<span style=\"color: #000000\">}</span>\n\n<span style=\"color: #008000\">// prints \"12, 26\"</span>\n<span style=\"color: #0000FF\">const</span><span style=\"color: #000000\"> </span><span style=\"color: #1A1A1A\"><data-lsp lsp='const point: {&amp;#13;    x: number;&amp;#13;    y: number;&amp;#13;}'>point</data-lsp> </span><span style=\"color: #000000\">= { <data-lsp lsp='(property) x: number'>x</data-lsp>: </span><span style=\"color: #09835A\">12</span><span style=\"color: #000000\">, <data-lsp lsp='(property) y: number'>y</data-lsp>: </span><span style=\"color: #09835A\">26</span><span style=\"color: #000000\"> };</span>\n<span style=\"color: #000000\"><data-lsp lsp='function printPoint(p: Point): void'>printPoint</data-lsp>(</span><span style=\"color: #1A1A1A\"><data-lsp lsp='const point: {&amp;#13;    x: number;&amp;#13;    y: number;&amp;#13;}'>point</data-lsp></span><span style=\"color: #000000\">);</span></code><a href='https://www.typescriptlang.org/play/#code/JYOwLgpgTgZghgYwgAgAoHtRmQbwFDLIAeAXMiAK4C2ARtANwHICeZltDeAvnnjBSARhg6EMgAOULBiwAKcWRngAlLiYJRAZ3QAbCADod6AOayABgBIc4-US4AaZFZvMuZ5Yx54A9N4lTwTWQAIgBGACZHcIA2YLwNEE1scUxwZABeXGIyCMdWZBjkLkZJaVSweXKPIA'>Try</a></div></pre>\n<p>The <code>point</code> variable is never declared to be a <code>Point</code> type, but TypeScript compares the shape of <code>point</code> to the shape of <code>Point</code> in the type-check.\nBecause they both have the same shape, then it passes.</p>\n<p>The shape matching only requires a subset of the object’s fields to match.</p>\n<pre class=\"shiki twoslash\"><div class=\"language-id\">ts</div><div class='code-container'><code><span style=\"color: #0000FF\">const</span><span style=\"color: #000000\"> </span><span style=\"color: #1A1A1A\"><data-lsp lsp='const point3: {&amp;#13;    x: number;&amp;#13;    y: number;&amp;#13;    z: number;&amp;#13;}'>point3</data-lsp> </span><span style=\"color: #000000\">= { <data-lsp lsp='(property) x: number'>x</data-lsp>: </span><span style=\"color: #09835A\">12</span><span style=\"color: #000000\">, <data-lsp lsp='(property) y: number'>y</data-lsp>: </span><span style=\"color: #09835A\">26</span><span style=\"color: #000000\">, <data-lsp lsp='(property) z: number'>z</data-lsp>: </span><span style=\"color: #09835A\">89</span><span style=\"color: #000000\"> };</span>\n<span style=\"color: #000000\"><data-lsp lsp='function printPoint(p: Point): void'>printPoint</data-lsp>(</span><span style=\"color: #1A1A1A\"><data-lsp lsp='const point3: {&amp;#13;    x: number;&amp;#13;    y: number;&amp;#13;    z: number;&amp;#13;}'>point3</data-lsp></span><span style=\"color: #000000\">); </span><span style=\"color: #008000\">// prints \"12, 26\"</span>\n\n<span style=\"color: #0000FF\">const</span><span style=\"color: #000000\"> </span><span style=\"color: #1A1A1A\"><data-lsp lsp='const rect: {&amp;#13;    x: number;&amp;#13;    y: number;&amp;#13;    width: number;&amp;#13;    height: number;&amp;#13;}'>rect</data-lsp> </span><span style=\"color: #000000\">= { <data-lsp lsp='(property) x: number'>x</data-lsp>: </span><span style=\"color: #09835A\">33</span><span style=\"color: #000000\">, <data-lsp lsp='(property) y: number'>y</data-lsp>: </span><span style=\"color: #09835A\">3</span><span style=\"color: #000000\">, <data-lsp lsp='(property) width: number'>width</data-lsp>: </span><span style=\"color: #09835A\">30</span><span style=\"color: #000000\">, <data-lsp lsp='(property) height: number'>height</data-lsp>: </span><span style=\"color: #09835A\">80</span><span style=\"color: #000000\"> };</span>\n<span style=\"color: #000000\"><data-lsp lsp='function printPoint(p: Point): void'>printPoint</data-lsp>(</span><span style=\"color: #1A1A1A\"><data-lsp lsp='const rect: {&amp;#13;    x: number;&amp;#13;    y: number;&amp;#13;    width: number;&amp;#13;    height: number;&amp;#13;}'>rect</data-lsp></span><span style=\"color: #000000\">); </span><span style=\"color: #008000\">// prints \"33, 3\"</span>\n\n<span style=\"color: #0000FF\">const</span><span style=\"color: #000000\"> </span><span style=\"color: #1A1A1A\"><data-lsp lsp='const color: {&amp;#13;    hex: string;&amp;#13;}'>color</data-lsp> </span><span style=\"color: #000000\">= { <data-lsp lsp='(property) hex: string'>hex</data-lsp>: </span><span style=\"color: #A31515\">\"#187ABF\"</span><span style=\"color: #000000\"> };</span>\n\n<span style=\"color: #000000\"><data-lsp lsp='function printPoint(p: Point): void'>printPoint</data-lsp>(</span><span style=\"color: #1A1A1A\"><data-err><data-lsp lsp='const color: {&amp;#13;    hex: string;&amp;#13;}'>color</data-lsp></data-err></span><span style=\"color: #000000\">);</span>\n<span class=\"error\"><span>Argument of type '{ hex: string; }' is not assignable to parameter of type 'Point'.\n  Type '{ hex: string; }' is missing the following properties from type 'Point': x, y</span><span class=\"code\">2345</span></span><span class=\"error-behind\">Argument of type '{ hex: string; }' is not assignable to parameter of type 'Point'.\n  Type '{ hex: string; }' is missing the following properties from type 'Point': x, y</span></code><a href='https://www.typescriptlang.org/play/#code/PTAEAEFMCdoe2gZwFygEwGYAsBWAUAJYB2ALjAGYCGAxpKAApzEmgDeeooAHqkQK4BbAEYwA3B1ABPXoJHRxAXzx5yfItRIE4RUAAdozRswAUu1EdIBKNhOrbEcADaQAdI7gBzYwAMAJK10XLgUAGlB-QMkFb0tFPBBQAFpk6j4SZMS8OyJEFl0mUgxQAF42blQARjQw6XQANjCAL1QADgBOUAVxfUMCklM+jFjQBJ7SRFAAIiqwtDrJ5Wzc0GhIDRKynlAMDBrUXdAAdwIAExIAC32ABjDzyAIPc5JWq87ug1ILftWNYdGPkgTSY7MIYBZZewsOzuaAbVigO5bSYAYgqLQA7ABBABCADFJm9lGMSF9jNCELEgA'>Try</a></div></pre>\n<p>Finally, to really nail this point down, structurally there is no difference between how classes and objects conform to shapes:</p>\n<pre class=\"shiki twoslash\"><div class=\"language-id\">ts</div><div class='code-container'><code><span style=\"color: #0000FF\">class</span><span style=\"color: #000000\"> <data-lsp lsp='class VirtualPoint'>VirtualPoint</data-lsp> {</span>\n<span style=\"color: #000000\">  </span><span style=\"color: #1A1A1A\"><data-lsp lsp='(property) VirtualPoint.x: number'>x</data-lsp></span><span style=\"color: #000000\">: number;</span>\n<span style=\"color: #000000\">  </span><span style=\"color: #1A1A1A\"><data-lsp lsp='(property) VirtualPoint.y: number'>y</data-lsp></span><span style=\"color: #000000\">: number;</span>\n\n<span style=\"color: #000000\">  </span><span style=\"color: #0000FF\">constructor</span><span style=\"color: #000000\">(</span><span style=\"color: #1A1A1A\"><data-lsp lsp='(parameter) x: number'>x</data-lsp></span><span style=\"color: #000000\">: number, </span><span style=\"color: #1A1A1A\"><data-lsp lsp='(parameter) y: number'>y</data-lsp></span><span style=\"color: #000000\">: number) {</span>\n<span style=\"color: #000000\">    </span><span style=\"color: #0000FF\">this</span><span style=\"color: #000000\">.<data-lsp lsp='(property) VirtualPoint.x: number'>x</data-lsp> = </span><span style=\"color: #1A1A1A\"><data-lsp lsp='(parameter) x: number'>x</data-lsp></span><span style=\"color: #000000\">;</span>\n<span style=\"color: #000000\">    </span><span style=\"color: #0000FF\">this</span><span style=\"color: #000000\">.<data-lsp lsp='(property) VirtualPoint.y: number'>y</data-lsp> = </span><span style=\"color: #1A1A1A\"><data-lsp lsp='(parameter) y: number'>y</data-lsp></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\"> </span><span style=\"color: #1A1A1A\"><data-lsp lsp='const newVPoint: VirtualPoint'>newVPoint</data-lsp> </span><span style=\"color: #000000\">= </span><span style=\"color: #0000FF\">new</span><span style=\"color: #000000\"> <data-lsp lsp='constructor VirtualPoint(x: number, y: number): VirtualPoint'>VirtualPoint</data-lsp>(</span><span style=\"color: #09835A\">13</span><span style=\"color: #000000\">, </span><span style=\"color: #09835A\">56</span><span style=\"color: #000000\">);</span>\n<span style=\"color: #000000\"><data-lsp lsp='function printPoint(p: Point): void'>printPoint</data-lsp>(</span><span style=\"color: #1A1A1A\"><data-lsp lsp='const newVPoint: VirtualPoint'>newVPoint</data-lsp></span><span style=\"color: #000000\">); </span><span style=\"color: #008000\">// prints \"13, 56\"</span></code><a href='https://www.typescriptlang.org/play/#code/PTAEAEFMCdoe2gZwFygEwGYAsBWAUAJYB2ALjAGYCGAxpKAApzEmgDeeooAHqkQK4BbAEYwA3B1ABPXoJHRxAXzx5yfItRIE4RUAAdozRswAUu1EdIBKNhOrbEcADaQAdI7gBzYwAMAJK10XLgUAGlB-QMkFb0tFPBBQAFpk6j4SZMS8akdKRERQADUCaBI+SkcLFnZOHlB+YTEJaTrZRtt7Emg+DQRjWvq5MOaBmGtqzlASAAsCRCDQAF5ucQnJmbnJRakV0CUlLI66yAB3AsqtohPC4tLyyuMARgwwnAA2WLx9QyZSY0vTyqxUAJL6kfIAIieL1e4KAA'>Try</a></div></pre>\n<p>If the object or class has all the required properties, then TypeScript will say they match regardless of the implementation details.</p>\n<h2 id=\"next-steps\" style=\"position:relative;\"><a href=\"#next-steps\" aria-label=\"next steps 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>Next Steps</h2>\n<p>This doc is a high level 5 minute overview of the sort of syntax and tools you would use in everyday code. From here you should:</p>\n<ul>\n<li>Read the full Handbook <a href=\"/docs/handbook/intro.html\">from start to finish</a> (30m)</li>\n<li>Explore the <a href=\"/play#show-examples\">Playground examples</a>.</li>\n</ul>","headings":[{"value":"Types by Inference","depth":2},{"value":"Defining Types","depth":2},{"value":"Composing Types","depth":2},{"value":"Unions","depth":3},{"value":"Generics","depth":3},{"value":"Structural Type System","depth":2},{"value":"Next Steps","depth":2}],"frontmatter":{"permalink":"/docs/handbook/typescript-in-5-minutes.html","title":"TypeScript for JavaScript Programmers","disable_toc":null,"oneline":"Learn how TypeScript extends JavaScript"}},"prev":null,"next":null},"pageContext":{"id":"typescript-for-javascript-programmers","slug":"/docs/handbook/typescript-in-5-minutes.html","repoPath":"/packages/documentation/copy/en/get-started/TS for JS Programmers.md","lang":"en","modifiedTime":"2020-08-07T17:00:36.335Z"}}}