{"componentChunkName":"component---src-templates-handbook-tsx","path":"/docs/handbook/migrating-from-javascript.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":"f4948c0c-973a-5063-ab4d-e35f232f5afc","excerpt":"TypeScript doesn’t exist in a vacuum.\nIt was built with the JavaScript ecosystem in mind, and a lot of JavaScript exists today.\nConverting a JavaScript codebase…","html":"<p>TypeScript doesn’t exist in a vacuum.\nIt was built with the JavaScript ecosystem in mind, and a lot of JavaScript exists today.\nConverting a JavaScript codebase over to TypeScript is, while somewhat tedious, usually not challenging.\nIn this tutorial, we’re going to look at how you might start out.\nWe assume you’ve read enough of the handbook to write new TypeScript code.</p>\n<p>If you’re looking to convert a React project, we recommend looking at the <a href=\"https://github.com/Microsoft/TypeScript-React-Conversion-Guide#typescript-react-conversion-guide\">React Conversion Guide</a> first.</p>\n<h1 id=\"setting-up-your-directories\" style=\"position:relative;\"><a href=\"#setting-up-your-directories\" aria-label=\"setting up your directories 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>Setting up your Directories</h1>\n<p>If you’re writing in plain JavaScript, it’s likely that you’re running your JavaScript directly,\nwhere your <code>.js</code> files are in a <code>src</code>, <code>lib</code>, or <code>dist</code> directory, and then ran as desired.</p>\n<p>If that’s the case, the files that you’ve written are going to be used as inputs to TypeScript, and you’ll run the outputs it produces.\nDuring our JS to TS migration, we’ll need to separate our input files to prevent TypeScript from overwriting them.\nIf your output files need to reside in a specific directory, then that will be your output directory.</p>\n<p>You might also be running some intermediate steps on your JavaScript, such as bundling or using another transpiler like Babel.\nIn this case, you might already have a folder structure like this set up.</p>\n<p>From this point on, we’re going to assume that your directory is set up something like this:</p>\n<pre><code>projectRoot\n├── src\n│   ├── file1.js\n│   └── file2.js\n├── built\n└── tsconfig.json\n</code></pre>\n<p>If you have a <code>tests</code> folder outside of your <code>src</code> directory, you might have one <code>tsconfig.json</code> in <code>src</code>, and one in <code>tests</code> as well.</p>\n<h1 id=\"writing-a-configuration-file\" style=\"position:relative;\"><a href=\"#writing-a-configuration-file\" aria-label=\"writing a configuration file permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>Writing a Configuration File</h1>\n<p>TypeScript uses a file called <code>tsconfig.json</code> for managing your project’s options, such as which files you want to include, and what sorts of checking you want to perform.\nLet’s create a bare-bones one for our project:</p>\n<pre class=\"shiki\"><div class=\"language-id\">json</div><div class='code-container'><code><span style=\"color: #000000\">{</span>\n<span style=\"color: #000000\">    </span><span style=\"color: #0451A5\">\"compilerOptions\"</span><span style=\"color: #000000\">: {</span>\n<span style=\"color: #000000\">        </span><span style=\"color: #0451A5\">\"outDir\"</span><span style=\"color: #000000\">: </span><span style=\"color: #A31515\">\"./built\"</span><span style=\"color: #000000\">,</span>\n<span style=\"color: #000000\">        </span><span style=\"color: #0451A5\">\"allowJs\"</span><span style=\"color: #000000\">: </span><span style=\"color: #0000FF\">true</span><span style=\"color: #000000\">,</span>\n<span style=\"color: #000000\">        </span><span style=\"color: #0451A5\">\"target\"</span><span style=\"color: #000000\">: </span><span style=\"color: #A31515\">\"es5\"</span>\n<span style=\"color: #000000\">    },</span>\n<span style=\"color: #000000\">    </span><span style=\"color: #0451A5\">\"include\"</span><span style=\"color: #000000\">: [</span>\n<span style=\"color: #000000\">        </span><span style=\"color: #A31515\">\"./src/**/*\"</span>\n<span style=\"color: #000000\">    ]</span>\n<span style=\"color: #000000\">}</span></code></div></pre>\n<p>Here we’re specifying a few things to TypeScript:</p>\n<ol>\n<li>Read in any files it understands in the <code>src</code> directory (with <code>include</code>).</li>\n<li>Accept JavaScript files as inputs (with <code>allowJs</code>).</li>\n<li>Emit all of the output files in <code>built</code> (with <code>outDir</code>).</li>\n<li>Translate newer JavaScript constructs down to an older version like ECMAScript 5 (using <code>target</code>).</li>\n</ol>\n<p>At this point, if you try running <code>tsc</code> at the root of your project, you should see output files in the <code>built</code> directory.\nThe layout of files in <code>built</code> should look identical to the layout of <code>src</code>.\nYou should now have TypeScript working with your project.</p>\n<h2 id=\"early-benefits\" style=\"position:relative;\"><a href=\"#early-benefits\" aria-label=\"early benefits 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>Early Benefits</h2>\n<p>Even at this point you can get some great benefits from TypeScript understanding your project.\nIf you open up an editor like <a href=\"https://code.visualstudio.com\">VS Code</a> or <a href=\"https://visualstudio.com\">Visual Studio</a>, you’ll see that you can often get some tooling support like completion.\nYou can also catch certain bugs with options like:</p>\n<ul>\n<li><code>noImplicitReturns</code> which prevents you from forgetting to return at the end of a function.</li>\n<li><code>noFallthroughCasesInSwitch</code> which is helpful if you never want to forget a <code>break</code> statement between <code>case</code>s in a <code>switch</code> block.</li>\n</ul>\n<p>TypeScript will also warn about unreachable code and labels, which you can disable with <code>allowUnreachableCode</code> and <code>allowUnusedLabels</code> respectively.</p>\n<h1 id=\"integrating-with-build-tools\" style=\"position:relative;\"><a href=\"#integrating-with-build-tools\" aria-label=\"integrating with build tools 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>Integrating with Build Tools</h1>\n<p>You might have some more build steps in your pipeline.\nPerhaps you concatenate something to each of your files.\nEach build tool is different, but we’ll do our best to cover the gist of things.</p>\n<h2 id=\"gulp\" style=\"position:relative;\"><a href=\"#gulp\" aria-label=\"gulp 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>Gulp</h2>\n<p>If you’re using Gulp in some fashion, we have a tutorial on <a href=\"/1f3f9788b46091bf5b98aca325342c56/Gulp.md\">using Gulp</a> with TypeScript, and integrating with common build tools like Browserify, Babelify, and Uglify.\nYou can read more there.</p>\n<h2 id=\"webpack\" style=\"position:relative;\"><a href=\"#webpack\" aria-label=\"webpack 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>Webpack</h2>\n<p>Webpack integration is pretty simple.\nYou can use <code>awesome-typescript-loader</code>, a TypeScript loader, combined with <code>source-map-loader</code> for easier debugging.\nSimply run</p>\n<pre class=\"shiki\"><div class=\"language-id\">shell</div><div class='code-container'><code><span style=\"color: #000000\">npm install awesome-typescript-loader source-map-loader</span></code></div></pre>\n<p>and merge in options from the following into your <code>webpack.config.js</code> file:</p>\n<pre class=\"shiki\"><div class=\"language-id\">js</div><div class='code-container'><code><span style=\"color: #000000\">module.exports = {</span>\n<span style=\"color: #000000\">    entry: </span><span style=\"color: #A31515\">\"./src/index.ts\"</span><span style=\"color: #000000\">,</span>\n<span style=\"color: #000000\">    output: {</span>\n<span style=\"color: #000000\">        filename: </span><span style=\"color: #A31515\">\"./dist/bundle.js\"</span><span style=\"color: #000000\">,</span>\n<span style=\"color: #000000\">    },</span>\n\n<span style=\"color: #000000\">    </span><span style=\"color: #008000\">// Enable sourcemaps for debugging webpack's output.</span>\n<span style=\"color: #000000\">    devtool: </span><span style=\"color: #A31515\">\"source-map\"</span><span style=\"color: #000000\">,</span>\n\n<span style=\"color: #000000\">    resolve: {</span>\n<span style=\"color: #000000\">        </span><span style=\"color: #008000\">// Add '.ts' and '.tsx' as resolvable extensions.</span>\n<span style=\"color: #000000\">        extensions: [</span><span style=\"color: #A31515\">\"\"</span><span style=\"color: #000000\">, </span><span style=\"color: #A31515\">\".webpack.js\"</span><span style=\"color: #000000\">, </span><span style=\"color: #A31515\">\".web.js\"</span><span style=\"color: #000000\">, </span><span style=\"color: #A31515\">\".ts\"</span><span style=\"color: #000000\">, </span><span style=\"color: #A31515\">\".tsx\"</span><span style=\"color: #000000\">, </span><span style=\"color: #A31515\">\".js\"</span><span style=\"color: #000000\">]</span>\n<span style=\"color: #000000\">    },</span>\n\n<span style=\"color: #000000\">    module: {</span>\n<span style=\"color: #000000\">        rules: [</span>\n<span style=\"color: #000000\">        </span>\n<span style=\"color: #000000\">          </span><span style=\"color: #008000\">// All files with a '.ts' or '.tsx' extension will be handled by 'awesome-typescript-loader'.</span>\n<span style=\"color: #000000\">          { test:</span><span style=\"color: #A31515\"> /\\.tsx</span><span style=\"color: #000000\">?</span><span style=\"color: #0000FF\">$</span><span style=\"color: #A31515\">/</span><span style=\"color: #000000\">, loader: </span><span style=\"color: #A31515\">\"awesome-typescript-loader\"</span><span style=\"color: #000000\"> },</span>\n<span style=\"color: #000000\">          </span>\n<span style=\"color: #000000\">          </span><span style=\"color: #008000\">// All output '.js' files will have any sourcemaps re-processed by 'source-map-loader'.</span>\n<span style=\"color: #000000\">          { test:</span><span style=\"color: #A31515\"> /\\.js</span><span style=\"color: #0000FF\">$</span><span style=\"color: #A31515\">/</span><span style=\"color: #000000\">, loader: </span><span style=\"color: #A31515\">\"source-map-loader\"</span><span style=\"color: #000000\"> }</span>\n<span style=\"color: #000000\">        ]</span>\n<span style=\"color: #000000\">    },</span>\n\n<span style=\"color: #000000\">    </span><span style=\"color: #008000\">// Other options...</span>\n<span style=\"color: #000000\">};</span></code></div></pre>\n<p>It’s important to note that awesome-typescript-loader will need to run before any other loader that deals with <code>.js</code> files.</p>\n<p>The same goes for <a href=\"https://github.com/TypeStrong/ts-loader\">ts-loader</a>, another TypeScript loader for Webpack.\nYou can read more about the differences between the two <a href=\"https://github.com/s-panferov/awesome-typescript-loader#differences-between-ts-loader\">here</a>.</p>\n<p>You can see an example of using Webpack in our <a href=\"./React%20&#x26;%20Webpack.md\">tutorial on React and Webpack</a>.</p>\n<h1 id=\"moving-to-typescript-files\" style=\"position:relative;\"><a href=\"#moving-to-typescript-files\" aria-label=\"moving to typescript files 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>Moving to TypeScript Files</h1>\n<p>At this point, you’re probably ready to start using TypeScript files.\nThe first step is to rename one of your <code>.js</code> files to <code>.ts</code>.\nIf your file uses JSX, you’ll need to rename it to <code>.tsx</code>.</p>\n<p>Finished with that step?\nGreat!\nYou’ve successfully migrated a file from JavaScript to TypeScript!</p>\n<p>Of course, that might not feel right.\nIf you open that file in an editor with TypeScript support (or if you run <code>tsc --pretty</code>), you might see red squiggles on certain lines.\nYou should think of these the same way you’d think of red squiggles in an editor like Microsoft Word.\nTypeScript will still translate your code, just like Word will still let you print your documents.</p>\n<p>If that sounds too lax for you, you can tighten that behavior up.\nIf, for instance, you <em>don’t</em> want TypeScript to compile to JavaScript in the face of errors, you can use the <code>noEmitOnError</code> option.\nIn that sense, TypeScript has a dial on its strictness, and you can turn that knob up as high as you want.</p>\n<p>If you plan on using the stricter settings that are available, it’s best to turn them on now (see <a href=\"#getting-stricter-checks\">Getting Stricter Checks</a> below).\nFor instance, if you never want TypeScript to silently infer <code>any</code> for a type without you explicitly saying so, you can use <code>noImplicitAny</code> before you start modifying your files.\nWhile it might feel somewhat overwhelming, the long-term gains become apparent much more quickly.</p>\n<h2 id=\"weeding-out-errors\" style=\"position:relative;\"><a href=\"#weeding-out-errors\" aria-label=\"weeding out errors 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>Weeding out Errors</h2>\n<p>Like we mentioned, it’s not unexpected to get error messages after conversion.\nThe important thing is to actually go one by one through these and decide how to deal with the errors.\nOften these will be legitimate bugs, but sometimes you’ll have to explain what you’re trying to do a little better to TypeScript.</p>\n<h3 id=\"importing-from-modules\" style=\"position:relative;\"><a href=\"#importing-from-modules\" aria-label=\"importing from modules 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>Importing from Modules</h3>\n<p>You might start out getting a bunch of errors like <code>Cannot find name 'require'.</code>, and <code>Cannot find name 'define'.</code>.\nIn these cases, it’s likely that you’re using modules.\nWhile you can just convince TypeScript that these exist by writing out</p>\n<pre class=\"shiki\"><div class=\"language-id\">ts</div><div class='code-container'><code><span style=\"color: #008000\">// For Node/CommonJS</span>\n<span style=\"color: #0000FF\">declare</span><span style=\"color: #000000\"> </span><span style=\"color: #0000FF\">function</span><span style=\"color: #000000\"> require(path: string): any;</span></code></div></pre>\n<p>or</p>\n<pre class=\"shiki\"><div class=\"language-id\">ts</div><div class='code-container'><code><span style=\"color: #008000\">// For RequireJS/AMD</span>\n<span style=\"color: #0000FF\">declare</span><span style=\"color: #000000\"> </span><span style=\"color: #0000FF\">function</span><span style=\"color: #000000\"> define(...args: any[]): any;</span></code></div></pre>\n<p>it’s better to get rid of those calls and use TypeScript syntax for imports.</p>\n<p>First, you’ll need to enable some module system by setting TypeScript’s <code>module</code> flag.\nValid options are <code>commonjs</code>, <code>amd</code>, <code>system</code>, and <code>umd</code>.</p>\n<p>If you had the following Node/CommonJS code:</p>\n<pre class=\"shiki\"><div class=\"language-id\">js</div><div class='code-container'><code><span style=\"color: #0000FF\">var</span><span style=\"color: #000000\"> foo = require(</span><span style=\"color: #A31515\">\"foo\"</span><span style=\"color: #000000\">);</span>\n\n<span style=\"color: #000000\">foo.doStuff();</span></code></div></pre>\n<p>or the following RequireJS/AMD code:</p>\n<pre class=\"shiki\"><div class=\"language-id\">js</div><div class='code-container'><code><span style=\"color: #000000\">define([</span><span style=\"color: #A31515\">\"foo\"</span><span style=\"color: #000000\">], </span><span style=\"color: #0000FF\">function</span><span style=\"color: #000000\">(foo) {</span>\n<span style=\"color: #000000\">    foo.doStuff();</span>\n<span style=\"color: #000000\">})</span></code></div></pre>\n<p>then you would write the following TypeScript code:</p>\n<pre class=\"shiki\"><div class=\"language-id\">ts</div><div class='code-container'><code><span style=\"color: #0000FF\">import</span><span style=\"color: #000000\"> foo = </span><span style=\"color: #0000FF\">require</span><span style=\"color: #000000\">(</span><span style=\"color: #A31515\">\"foo\"</span><span style=\"color: #000000\">);</span>\n\n<span style=\"color: #000000\">foo.doStuff();</span></code></div></pre>\n<h3 id=\"getting-declaration-files\" style=\"position:relative;\"><a href=\"#getting-declaration-files\" aria-label=\"getting declaration files 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>Getting Declaration Files</h3>\n<p>If you started converting over to TypeScript imports, you’ll probably run into errors like <code>Cannot find module 'foo'.</code>.\nThe issue here is that you likely don’t have <em>declaration files</em> to describe your library.\nLuckily this is pretty easy.\nIf TypeScript complains about a package like <code>lodash</code>, you can just write</p>\n<pre class=\"shiki\"><div class=\"language-id\">shell</div><div class='code-container'><code><span style=\"color: #000000\">npm install -S @types/lodash</span></code></div></pre>\n<p>If you’re using a module option other than <code>commonjs</code>, you’ll need to set your <code>moduleResolution</code> option to <code>node</code>.</p>\n<p>After that, you’ll be able to import lodash with no issues, and get accurate completions.</p>\n<h3 id=\"exporting-from-modules\" style=\"position:relative;\"><a href=\"#exporting-from-modules\" aria-label=\"exporting from modules 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>Exporting from Modules</h3>\n<p>Typically, exporting from a module involves adding properties to a value like <code>exports</code> or <code>module.exports</code>.\nTypeScript allows you to use top-level export statements.\nFor instance, if you exported a function like so:</p>\n<pre class=\"shiki\"><div class=\"language-id\">js</div><div class='code-container'><code><span style=\"color: #000000\">module.exports.feedPets = </span><span style=\"color: #0000FF\">function</span><span style=\"color: #000000\">(pets) {</span>\n<span style=\"color: #000000\">    </span><span style=\"color: #008000\">// ...</span>\n<span style=\"color: #000000\">}</span></code></div></pre>\n<p>you could write that out as the following:</p>\n<pre class=\"shiki\"><div class=\"language-id\">ts</div><div class='code-container'><code><span style=\"color: #0000FF\">export</span><span style=\"color: #000000\"> </span><span style=\"color: #0000FF\">function</span><span style=\"color: #000000\"> feedPets(pets) {</span>\n<span style=\"color: #000000\">    </span><span style=\"color: #008000\">// ...</span>\n<span style=\"color: #000000\">}</span></code></div></pre>\n<p>Sometimes you’ll entirely overwrite the exports object.\nThis is a common pattern people use to make their modules immediately callable like in this snippet:</p>\n<pre class=\"shiki\"><div class=\"language-id\">js</div><div class='code-container'><code><span style=\"color: #0000FF\">var</span><span style=\"color: #000000\"> express = require(</span><span style=\"color: #A31515\">\"express\"</span><span style=\"color: #000000\">);</span>\n<span style=\"color: #0000FF\">var</span><span style=\"color: #000000\"> app = express();</span></code></div></pre>\n<p>You might have previously written that like so:</p>\n<pre class=\"shiki\"><div class=\"language-id\">js</div><div class='code-container'><code><span style=\"color: #0000FF\">function</span><span style=\"color: #000000\"> foo() {</span>\n<span style=\"color: #000000\">    </span><span style=\"color: #008000\">// ...</span>\n<span style=\"color: #000000\">}</span>\n<span style=\"color: #000000\">module.exports = foo;</span></code></div></pre>\n<p>In TypeScript, you can model this with the <code>export =</code> construct.</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\"> foo() {</span>\n<span style=\"color: #000000\">    </span><span style=\"color: #008000\">// ...</span>\n<span style=\"color: #000000\">}</span>\n<span style=\"color: #0000FF\">export</span><span style=\"color: #000000\"> = foo;</span></code></div></pre>\n<h3 id=\"too-manytoo-few-arguments\" style=\"position:relative;\"><a href=\"#too-manytoo-few-arguments\" aria-label=\"too manytoo few arguments 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>Too many/too few arguments</h3>\n<p>You’ll sometimes find yourself calling a function with too many/few arguments.\nTypically, this is a bug, but in some cases, you might have declared a function that uses the <code>arguments</code> object instead of writing out any parameters:</p>\n<pre class=\"shiki\"><div class=\"language-id\">js</div><div class='code-container'><code><span style=\"color: #0000FF\">function</span><span style=\"color: #000000\"> myCoolFunction() {</span>\n<span style=\"color: #000000\">    </span><span style=\"color: #0000FF\">if</span><span style=\"color: #000000\"> (</span><span style=\"color: #0000FF\">arguments</span><span style=\"color: #000000\">.length == </span><span style=\"color: #09835A\">2</span><span style=\"color: #000000\"> && !Array.isArray(</span><span style=\"color: #0000FF\">arguments</span><span style=\"color: #000000\">[</span><span style=\"color: #09835A\">1</span><span style=\"color: #000000\">])) {</span>\n<span style=\"color: #000000\">        </span><span style=\"color: #0000FF\">var</span><span style=\"color: #000000\"> f = </span><span style=\"color: #0000FF\">arguments</span><span style=\"color: #000000\">[</span><span style=\"color: #09835A\">0</span><span style=\"color: #000000\">];</span>\n<span style=\"color: #000000\">        </span><span style=\"color: #0000FF\">var</span><span style=\"color: #000000\"> arr = </span><span style=\"color: #0000FF\">arguments</span><span style=\"color: #000000\">[</span><span style=\"color: #09835A\">1</span><span style=\"color: #000000\">];</span>\n<span style=\"color: #000000\">        </span><span style=\"color: #008000\">// ...</span>\n<span style=\"color: #000000\">    }</span>\n<span style=\"color: #000000\">    </span><span style=\"color: #008000\">// ...</span>\n<span style=\"color: #000000\">}</span>\n\n<span style=\"color: #000000\">myCoolFunction(</span><span style=\"color: #0000FF\">function</span><span style=\"color: #000000\">(x) { console.log(x) }, [</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: #000000\">myCoolFunction(</span><span style=\"color: #0000FF\">function</span><span style=\"color: #000000\">(x) { console.log(x) }, </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></code></div></pre>\n<p>In this case, we need to use TypeScript to tell any of our callers about the ways <code>myCoolFunction</code> can be called using function overloads.</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\"> myCoolFunction(f: (x: number) </span><span style=\"color: #0000FF\">=&gt;</span><span style=\"color: #000000\"> void, nums: number[]): void;</span>\n<span style=\"color: #0000FF\">function</span><span style=\"color: #000000\"> myCoolFunction(f: (x: number) </span><span style=\"color: #0000FF\">=&gt;</span><span style=\"color: #000000\"> void, ...nums: number[]): void;</span>\n<span style=\"color: #0000FF\">function</span><span style=\"color: #000000\"> myCoolFunction() {</span>\n<span style=\"color: #000000\">    </span><span style=\"color: #0000FF\">if</span><span style=\"color: #000000\"> (</span><span style=\"color: #0000FF\">arguments</span><span style=\"color: #000000\">.length == </span><span style=\"color: #09835A\">2</span><span style=\"color: #000000\"> && !Array.isArray(</span><span style=\"color: #0000FF\">arguments</span><span style=\"color: #000000\">[</span><span style=\"color: #09835A\">1</span><span style=\"color: #000000\">])) {</span>\n<span style=\"color: #000000\">        </span><span style=\"color: #0000FF\">var</span><span style=\"color: #000000\"> f = </span><span style=\"color: #0000FF\">arguments</span><span style=\"color: #000000\">[</span><span style=\"color: #09835A\">0</span><span style=\"color: #000000\">];</span>\n<span style=\"color: #000000\">        </span><span style=\"color: #0000FF\">var</span><span style=\"color: #000000\"> arr = </span><span style=\"color: #0000FF\">arguments</span><span style=\"color: #000000\">[</span><span style=\"color: #09835A\">1</span><span style=\"color: #000000\">];</span>\n<span style=\"color: #000000\">        </span><span style=\"color: #008000\">// ...</span>\n<span style=\"color: #000000\">    }</span>\n<span style=\"color: #000000\">    </span><span style=\"color: #008000\">// ...</span>\n<span style=\"color: #000000\">}</span></code></div></pre>\n<p>We added two overload signatures to <code>myCoolFunction</code>.\nThe first checks states that <code>myCoolFunction</code> takes a function (which takes a <code>number</code>), and then a list of <code>number</code>s.\nThe second one says that it will take a function as well, and then uses a rest parameter (<code>...nums</code>) to state that any number of arguments after that need to be <code>number</code>s.</p>\n<h3 id=\"sequentially-added-properties\" style=\"position:relative;\"><a href=\"#sequentially-added-properties\" aria-label=\"sequentially added 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>Sequentially Added Properties</h3>\n<p>Some people find it more aesthetically pleasing to create an object and add properties immediately after like so:</p>\n<pre class=\"shiki\"><div class=\"language-id\">js</div><div class='code-container'><code><span style=\"color: #0000FF\">var</span><span style=\"color: #000000\"> options = {};</span>\n<span style=\"color: #000000\">options.color = </span><span style=\"color: #A31515\">\"red\"</span><span style=\"color: #000000\">;</span>\n<span style=\"color: #000000\">options.volume = </span><span style=\"color: #09835A\">11</span><span style=\"color: #000000\">;</span></code></div></pre>\n<p>TypeScript will say that you can’t assign to <code>color</code> and <code>volume</code> because it first figured out the type of <code>options</code> as <code>{}</code> which doesn’t have any properties.\nIf you instead moved the declarations into the object literal themselves, you’d get no errors:</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\"> options = {</span>\n<span style=\"color: #000000\">    color: </span><span style=\"color: #A31515\">\"red\"</span><span style=\"color: #000000\">,</span>\n<span style=\"color: #000000\">    volume: </span><span style=\"color: #09835A\">11</span>\n<span style=\"color: #000000\">};</span></code></div></pre>\n<p>You could also define the type of <code>options</code> and add a type assertion on the object literal.</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\"> Options { color: string; volume: number }</span>\n\n<span style=\"color: #0000FF\">let</span><span style=\"color: #000000\"> options = {} </span><span style=\"color: #0000FF\">as</span><span style=\"color: #000000\"> Options;</span>\n<span style=\"color: #000000\">options.color = </span><span style=\"color: #A31515\">\"red\"</span><span style=\"color: #000000\">;</span>\n<span style=\"color: #000000\">options.volume = </span><span style=\"color: #09835A\">11</span><span style=\"color: #000000\">;</span></code></div></pre>\n<p>Alternatively, you can just say <code>options</code> has the type <code>any</code> which is the easiest thing to do, but which will benefit you the least.</p>\n<h3 id=\"any-object-and-\" style=\"position:relative;\"><a href=\"#any-object-and-\" aria-label=\"any object and  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>any</code>, <code>Object</code>, and <code>{}</code></h3>\n<p>You might be tempted to use <code>Object</code> or <code>{}</code> to say that a value can have any property on it because <code>Object</code> is, for most purposes, the most general type.\nHowever <strong><code>any</code> is actually the type you want to use</strong> in those situations, since it’s the most <em>flexible</em> type.</p>\n<p>For instance, if you have something that’s typed as <code>Object</code> you won’t be able to call methods like <code>toLowerCase()</code> on it.\nBeing more general usually means you can do less with a type, but <code>any</code> is special in that it is the most general type while still allowing you to do anything with it.\nThat means you can call it, construct it, access properties on it, etc.\nKeep in mind though, whenever you use <code>any</code>, you lose out on most of the error checking and editor support that TypeScript gives you.</p>\n<p>If a decision ever comes down to <code>Object</code> and <code>{}</code>, you should prefer <code>{}</code>.\nWhile they are mostly the same, technically <code>{}</code> is a more general type than <code>Object</code> in certain esoteric cases.</p>\n<h2 id=\"getting-stricter-checks\" style=\"position:relative;\"><a href=\"#getting-stricter-checks\" aria-label=\"getting stricter 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>Getting Stricter Checks</h2>\n<p>TypeScript comes with certain checks to give you more safety and analysis of your program.\nOnce you’ve converted your codebase to TypeScript, you can start enabling these checks for greater safety.</p>\n<h3 id=\"no-implicit-any\" style=\"position:relative;\"><a href=\"#no-implicit-any\" aria-label=\"no implicit any 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>No Implicit <code>any</code></h3>\n<p>There are certain cases where TypeScript can’t figure out what certain types should be.\nTo be as lenient as possible, it will decide to use the type <code>any</code> in its place.\nWhile this is great for migration, using <code>any</code> means that you’re not getting any type safety, and you won’t get the same tooling support you’d get elsewhere.\nYou can tell TypeScript to flag these locations down and give an error with the <code>noImplicitAny</code> option.</p>\n<h3 id=\"strict-null--undefined-checks\" style=\"position:relative;\"><a href=\"#strict-null--undefined-checks\" aria-label=\"strict null  undefined 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>Strict <code>null</code> &#x26; <code>undefined</code> Checks</h3>\n<p>By default, TypeScript assumes that <code>null</code> and <code>undefined</code> are in the domain of every type.\nThat means anything declared with the type <code>number</code> could be <code>null</code> or <code>undefined</code>.\nSince <code>null</code> and <code>undefined</code> are such a frequent source of bugs in JavaScript and TypeScript, TypeScript has the <code>strictNullChecks</code> option to spare you the stress of worrying about these issues.</p>\n<p>When <code>strictNullChecks</code> is enabled, <code>null</code> and <code>undefined</code> get their own types called <code>null</code> and <code>undefined</code> respectively.\nWhenever anything is <em>possibly</em> <code>null</code>, you can use a union type with the original type.\nSo for instance, if something could be a <code>number</code> or <code>null</code>, you’d write the type out as <code>number | null</code>.</p>\n<p>If you ever have a value that TypeScript thinks is possibly <code>null</code>/<code>undefined</code>, but you know better, you can use the postfix <code>!</code> operator to tell it otherwise.</p>\n<pre class=\"shiki\"><div class=\"language-id\">ts</div><div class='code-container'><code><span style=\"color: #0000FF\">declare</span><span style=\"color: #000000\"> </span><span style=\"color: #0000FF\">var</span><span style=\"color: #000000\"> foo: string[] | null;</span>\n\n<span style=\"color: #000000\">foo.length;  </span><span style=\"color: #008000\">// error - 'foo' is possibly 'null'</span>\n\n<span style=\"color: #000000\">foo!.length; </span><span style=\"color: #008000\">// okay - 'foo!' just has type 'string[]'</span></code></div></pre>\n<p>As a heads up, when using <code>strictNullChecks</code>, your dependencies may need to be updated to use <code>strictNullChecks</code> as well.</p>\n<h3 id=\"no-implicit-any-for-this\" style=\"position:relative;\"><a href=\"#no-implicit-any-for-this\" aria-label=\"no implicit any for this permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>No Implicit <code>any</code> for <code>this</code></h3>\n<p>When you use the <code>this</code> keyword outside of classes, it has the type <code>any</code> by default.\nFor instance, imagine a <code>Point</code> class, and imagine a function that we wish to add as a method:</p>\n<pre class=\"shiki\"><div class=\"language-id\">ts</div><div class='code-container'><code><span style=\"color: #0000FF\">class</span><span style=\"color: #000000\"> Point {</span>\n<span style=\"color: #000000\">    </span><span style=\"color: #0000FF\">constructor</span><span style=\"color: #000000\">(</span><span style=\"color: #0000FF\">public</span><span style=\"color: #000000\"> x, </span><span style=\"color: #0000FF\">public</span><span style=\"color: #000000\"> y) {}</span>\n<span style=\"color: #000000\">    getDistance(p: Point) {</span>\n<span style=\"color: #000000\">        </span><span style=\"color: #0000FF\">let</span><span style=\"color: #000000\"> dx = p.x - </span><span style=\"color: #0000FF\">this</span><span style=\"color: #000000\">.x;</span>\n<span style=\"color: #000000\">        </span><span style=\"color: #0000FF\">let</span><span style=\"color: #000000\"> dy = p.y - </span><span style=\"color: #0000FF\">this</span><span style=\"color: #000000\">.y;</span>\n<span style=\"color: #000000\">        </span><span style=\"color: #0000FF\">return</span><span style=\"color: #000000\"> Math.sqrt(dx ** </span><span style=\"color: #09835A\">2</span><span style=\"color: #000000\"> + dy ** </span><span style=\"color: #09835A\">2</span><span style=\"color: #000000\">);</span>\n<span style=\"color: #000000\">    }</span>\n<span style=\"color: #000000\">}</span>\n<span style=\"color: #008000\">// ...</span>\n\n<span style=\"color: #008000\">// Reopen the interface.</span>\n<span style=\"color: #0000FF\">interface</span><span style=\"color: #000000\"> Point {</span>\n<span style=\"color: #000000\">    distanceFromOrigin(point: Point): number;</span>\n<span style=\"color: #000000\">}</span>\n<span style=\"color: #000000\">Point.prototype.distanceFromOrigin = </span><span style=\"color: #0000FF\">function</span><span style=\"color: #000000\">(point: Point) {</span>\n<span style=\"color: #000000\">    </span><span style=\"color: #0000FF\">return</span><span style=\"color: #000000\"> </span><span style=\"color: #0000FF\">this</span><span style=\"color: #000000\">.getDistance({ x: </span><span style=\"color: #09835A\">0</span><span style=\"color: #000000\">, y: </span><span style=\"color: #09835A\">0</span><span style=\"color: #000000\">});</span>\n<span style=\"color: #000000\">}</span></code></div></pre>\n<p>This has the same problems we mentioned above - we could easily have misspelled <code>getDistance</code> and not gotten an error.\nFor this reason, TypeScript has the <code>noImplicitThis</code> option.\nWhen that option is set, TypeScript will issue an error when <code>this</code> is used without an explicit (or inferred) type.\nThe fix is to use a <code>this</code>-parameter to give an explicit type in the interface or in the function itself:</p>\n<pre class=\"shiki\"><div class=\"language-id\">ts</div><div class='code-container'><code><span style=\"color: #000000\">Point.prototype.distanceFromOrigin = </span><span style=\"color: #0000FF\">function</span><span style=\"color: #000000\">(this: Point, point: Point) {</span>\n<span style=\"color: #000000\">    </span><span style=\"color: #0000FF\">return</span><span style=\"color: #000000\"> </span><span style=\"color: #0000FF\">this</span><span style=\"color: #000000\">.getDistance({ x: </span><span style=\"color: #09835A\">0</span><span style=\"color: #000000\">, y: </span><span style=\"color: #09835A\">0</span><span style=\"color: #000000\">});</span>\n<span style=\"color: #000000\">}</span></code></div></pre>","headings":[{"value":"Setting up your Directories","depth":1},{"value":"Writing a Configuration File","depth":1},{"value":"Early Benefits","depth":2},{"value":"Integrating with Build Tools","depth":1},{"value":"Gulp","depth":2},{"value":"Webpack","depth":2},{"value":"Moving to TypeScript Files","depth":1},{"value":"Weeding out Errors","depth":2},{"value":"Importing from Modules","depth":3},{"value":"Getting Declaration Files","depth":3},{"value":"Exporting from Modules","depth":3},{"value":"Too many/too few arguments","depth":3},{"value":"Sequentially Added Properties","depth":3},{"value":"any, Object, and {}","depth":3},{"value":"Getting Stricter Checks","depth":2},{"value":"No Implicit any","depth":3},{"value":"Strict null & undefined Checks","depth":3},{"value":"No Implicit any for this","depth":3}],"frontmatter":{"permalink":"/docs/handbook/migrating-from-javascript.html","title":"Migrating from JavaScript"}}},"pageContext":{"slug":"/docs/handbook/migrating-from-javascript.html","isOldHandbook":true}}}