{"id":512,"date":"2015-09-16T15:35:55","date_gmt":"2015-09-16T13:35:55","guid":{"rendered":"http:\/\/blog.thecodecampus.de\/?p=512"},"modified":"2023-12-20T19:47:15","modified_gmt":"2023-12-20T18:47:15","slug":"converting-javascript-to-typescript-common-problems","status":"publish","type":"post","link":"https:\/\/www.thecodecampus.de\/blog\/converting-javascript-to-typescript-common-problems\/","title":{"rendered":"Converting JavaScript to TypeScript &#8211; Common Problems"},"content":{"rendered":"<h1>Converting JavaScript code to TypeScript<\/h1>\n<p>Here at theCodeCampus and w11k we are using TypeScript in web projects for 12 months now. Our experiences with TypeScript are overwhelming\u00a0so far! Using TypeScript is a huge improvement over a JavaScript-based development, especially in larger projects.<\/p>\n<p class=\"\">One of TypeScript&#8217;s advantages and often communicated characteristic is that its syntax is a (typed) superset of JavaScript. Hence, existing JavaScript code is valid TypeScript code. So far, so good! The selling point is that you can easily take existing JavaScript code and convert it to TypeScript, simply by changing the file extension from &#8216;js&#8217; to &#8216;ts&#8217;.<\/p>\n<h2 class=\"\">Problem 1: Inferred &#8216;any type&#8217;<\/h2>\n<p>When new TypeScript users try this in practice, they often experience this error message:<\/p>\n<pre class=\"\">error TS7006 Parameter 'methodParam' implicitly has an 'any' type.<\/pre>\n<p>The reason for this is quite simple: When e.g. a method parameter does not declare a type (which isn&#8217;t possible\u00a0when the code used to be valid JavaScript), the TypeScript compiler warns that the &#8216;any type&#8217; was inferred. Let&#8217;s say your method looks like this:<\/p>\n<pre class=\"lang:js decode:true\">function myFunction(methodParam) {\r\n    \/\/ ...\r\n}\r\n<\/pre>\n<p>The solution is to simply explicitly add a type, either the &#8216;any type&#8217; or a more precise one. Both declaration below would work:<\/p>\n<pre class=\"lang:js decode:true\">function myFunction(methodParam: any) {\r\n    \/\/ ...\r\n}\r\n\r\n<\/pre>\n<pre class=\"lang:js decode:true\">function myFunction(methodParam: string) {\r\n    \/\/ ...\r\n}\r\n<\/pre>\n<p>&nbsp;<\/p>\n<p>Another solution is to change the behavior of the TypeScript compiler.\u00a0How to do this, depends on the way you used to invoke the compiler. The command line tool &#8216;tsc&#8217; uses the flag &#8216;&#8211;noImplicitAny&#8217; to toggle this behavior. If you use a tsconfig.json (what you hopefully do), you can alter the compiler flags there:<\/p>\n<pre class=\"\">{\r\n  \"compilerOptions\": {\r\n    \"noImplicitAny\": false,\r\n  }\r\n}<\/pre>\n<p>However, I highly recommend to leave this flag on or respectively to add this flag to the &#8216;tsc&#8217; invocation. After all, you want the compiler to assist you in writing better code and adding reasonable type annotations to your variables is one part of it.<\/p>\n<h2>Problem 2:\u00a0Inferred Structural Types for Object Literals<\/h2>\n<p>Take the following perfectly valid JavaScript code:<\/p>\n<pre class=\"\">var obj = {\r\n    key1: 1,\r\n    key2: 2\r\n};\r\n\r\nif (CONDITION) {\r\n  obj.key3 = 3;  \r\n}<\/pre>\n<p>We first initialize the object &#8216;obj&#8217; with 2 properties. Then, based on a condition, we add another property &#8216;key3&#8217;. If the condition is false, the expression &#8216;obj.key3&#8217; will evaluate to &#8216;undefined&#8217;. All this is common JavaScript code. However, if we pass this to the TypeScript compiler, we get an error message:<\/p>\n<pre class=\"\">error TS2339 Property 'key3' does not exist on type '{ key1: number; key2: number; }'<\/pre>\n<p>The reason for this is that our object literal used to initialize the &#8216;obj&#8217; variable also creates a &#8216;structural type&#8217; consisting of two properties: &#8216;key1&#8217; and &#8216;key2&#8217;. When we try to assign the property &#8216;key3&#8217;, the compiler rightfully complains that this type has no property &#8216;key3&#8217;.<\/p>\n<p>There are two possible solutions for this. We can add this property to the structural type:<\/p>\n<pre class=\"\">var obj = {\r\n    key1: 1,\r\n    key2: 2,\r\n    key3, undefined\r\n};<\/pre>\n<p>&nbsp;<\/p>\n<p>Or, we declare the variable &#8216;obj&#8217;\u00a0with the &#8216;any type&#8217;:<\/p>\n<pre class=\"\">var obj: any = {\r\n    key1: 1,\r\n    key2: 2\r\n};<\/pre>\n<p>&nbsp;<\/p>\n<p>If possible, I recommend to always choose the first solution.<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Converting JavaScript code to TypeScript Here at theCodeCampus and w11k we are using TypeScript in web projects for 12 months now. Our experiences with TypeScript are overwhelming\u00a0so far! Using TypeScript is a huge improvement over a JavaScript-based development, especially in larger projects. One of TypeScript&#8217;s advantages and often communicated characteristic is that its syntax is [&#8230;]<br \/><a class=\"meta-big\" href=\"https:\/\/www.thecodecampus.de\/blog\/converting-javascript-to-typescript-common-problems\/\"> READ MORE<\/a><\/p>\n","protected":false},"author":5,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[60],"tags":[61,75],"class_list":["post-512","post","type-post","status-publish","format-standard","hentry","category-typescript","tag-javascript","tag-typescript"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.8 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>Converting JavaScript to TypeScript - Common Problems - Web Development Blog<\/title>\n<meta name=\"description\" content=\"Elevate coding fluency! Overcome common pitfalls converting JavaScript to TypeScript. Streamline your transition and boost development efficiency now!\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.thecodecampus.de\/blog\/converting-javascript-to-typescript-common-problems\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Converting JavaScript to TypeScript - Common Problems - Web Development Blog\" \/>\n<meta property=\"og:description\" content=\"Elevate coding fluency! Overcome common pitfalls converting JavaScript to TypeScript. Streamline your transition and boost development efficiency now!\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.thecodecampus.de\/blog\/converting-javascript-to-typescript-common-problems\/\" \/>\n<meta property=\"og:site_name\" content=\"Web Development tips and tricks - theCodeCampus Blog\" \/>\n<meta property=\"article:published_time\" content=\"2015-09-16T13:35:55+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2023-12-20T18:47:15+00:00\" \/>\n<meta name=\"author\" content=\"Roman Roelofsen\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Roman Roelofsen\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"2 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\\\/\\\/www.thecodecampus.de\\\/blog\\\/converting-javascript-to-typescript-common-problems\\\/#article\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.thecodecampus.de\\\/blog\\\/converting-javascript-to-typescript-common-problems\\\/\"},\"author\":{\"name\":\"Roman Roelofsen\",\"@id\":\"https:\\\/\\\/www.thecodecampus.de\\\/blog\\\/#\\\/schema\\\/person\\\/941e9a6169bf8da015ed1f348177a22d\"},\"headline\":\"Converting JavaScript to TypeScript &#8211; Common Problems\",\"datePublished\":\"2015-09-16T13:35:55+00:00\",\"dateModified\":\"2023-12-20T18:47:15+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/www.thecodecampus.de\\\/blog\\\/converting-javascript-to-typescript-common-problems\\\/\"},\"wordCount\":449,\"commentCount\":0,\"publisher\":{\"@id\":\"https:\\\/\\\/www.thecodecampus.de\\\/blog\\\/#organization\"},\"keywords\":[\"JavaScript\",\"TypeScript\"],\"articleSection\":[\"TypeScript\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\\\/\\\/www.thecodecampus.de\\\/blog\\\/converting-javascript-to-typescript-common-problems\\\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/www.thecodecampus.de\\\/blog\\\/converting-javascript-to-typescript-common-problems\\\/\",\"url\":\"https:\\\/\\\/www.thecodecampus.de\\\/blog\\\/converting-javascript-to-typescript-common-problems\\\/\",\"name\":\"Converting JavaScript to TypeScript - Common Problems - Web Development Blog\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.thecodecampus.de\\\/blog\\\/#website\"},\"datePublished\":\"2015-09-16T13:35:55+00:00\",\"dateModified\":\"2023-12-20T18:47:15+00:00\",\"description\":\"Elevate coding fluency! Overcome common pitfalls converting JavaScript to TypeScript. Streamline your transition and boost development efficiency now!\",\"breadcrumb\":{\"@id\":\"https:\\\/\\\/www.thecodecampus.de\\\/blog\\\/converting-javascript-to-typescript-common-problems\\\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/www.thecodecampus.de\\\/blog\\\/converting-javascript-to-typescript-common-problems\\\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/www.thecodecampus.de\\\/blog\\\/converting-javascript-to-typescript-common-problems\\\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\\\/\\\/www.thecodecampus.de\\\/blog\\\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Converting JavaScript to TypeScript &#8211; Common Problems\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/www.thecodecampus.de\\\/blog\\\/#website\",\"url\":\"https:\\\/\\\/www.thecodecampus.de\\\/blog\\\/\",\"name\":\"Web Development tips and tricks - theCodeCampus Blog\",\"description\":\"Tips, tricks, and experiences about developing web and mobile applications with Angular, TypeScript, and Testing.\",\"publisher\":{\"@id\":\"https:\\\/\\\/www.thecodecampus.de\\\/blog\\\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/www.thecodecampus.de\\\/blog\\\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"https:\\\/\\\/www.thecodecampus.de\\\/blog\\\/#organization\",\"name\":\"theCodeCampus\",\"url\":\"https:\\\/\\\/www.thecodecampus.de\\\/blog\\\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/www.thecodecampus.de\\\/blog\\\/#\\\/schema\\\/logo\\\/image\\\/\",\"url\":\"https:\\\/\\\/www.thecodecampus.de\\\/blog\\\/wp-content\\\/uploads\\\/2024\\\/01\\\/TCC-Logo-Bildmarke-quadratisch.jpg\",\"contentUrl\":\"https:\\\/\\\/www.thecodecampus.de\\\/blog\\\/wp-content\\\/uploads\\\/2024\\\/01\\\/TCC-Logo-Bildmarke-quadratisch.jpg\",\"width\":156,\"height\":156,\"caption\":\"theCodeCampus\"},\"image\":{\"@id\":\"https:\\\/\\\/www.thecodecampus.de\\\/blog\\\/#\\\/schema\\\/logo\\\/image\\\/\"}},{\"@type\":\"Person\",\"@id\":\"https:\\\/\\\/www.thecodecampus.de\\\/blog\\\/#\\\/schema\\\/person\\\/941e9a6169bf8da015ed1f348177a22d\",\"name\":\"Roman Roelofsen\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/www.thecodecampus.de\\\/blog\\\/wp-content\\\/uploads\\\/2024\\\/09\\\/roman-roelofsen-tcc-author-96x96.png\",\"url\":\"https:\\\/\\\/www.thecodecampus.de\\\/blog\\\/wp-content\\\/uploads\\\/2024\\\/09\\\/roman-roelofsen-tcc-author-96x96.png\",\"contentUrl\":\"https:\\\/\\\/www.thecodecampus.de\\\/blog\\\/wp-content\\\/uploads\\\/2024\\\/09\\\/roman-roelofsen-tcc-author-96x96.png\",\"caption\":\"Roman Roelofsen\"},\"description\":\"Roman Roelofsen is the technical managing director and responsible for the product development and technical direction of W11K GmbH. He is an expert in the development of complex web applications and has been working in the field of web engineering for 20 years. Roman supports our customers in the implementation of IT projects and has been working as a trainer for many years.\",\"sameAs\":[\"https:\\\/\\\/thecodecampus.de\\\/ueber-uns\\\/trainer\\\/roman-roelofsen\",\"https:\\\/\\\/www.linkedin.com\\\/in\\\/roman-roelofsen-91b2b35\\\/\"],\"url\":\"https:\\\/\\\/www.thecodecampus.de\\\/blog\\\/author\\\/rroelofsen\\\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Converting JavaScript to TypeScript - Common Problems - Web Development Blog","description":"Elevate coding fluency! Overcome common pitfalls converting JavaScript to TypeScript. Streamline your transition and boost development efficiency now!","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/www.thecodecampus.de\/blog\/converting-javascript-to-typescript-common-problems\/","og_locale":"en_US","og_type":"article","og_title":"Converting JavaScript to TypeScript - Common Problems - Web Development Blog","og_description":"Elevate coding fluency! Overcome common pitfalls converting JavaScript to TypeScript. Streamline your transition and boost development efficiency now!","og_url":"https:\/\/www.thecodecampus.de\/blog\/converting-javascript-to-typescript-common-problems\/","og_site_name":"Web Development tips and tricks - theCodeCampus Blog","article_published_time":"2015-09-16T13:35:55+00:00","article_modified_time":"2023-12-20T18:47:15+00:00","author":"Roman Roelofsen","twitter_card":"summary_large_image","twitter_misc":{"Written by":"Roman Roelofsen","Est. reading time":"2 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/www.thecodecampus.de\/blog\/converting-javascript-to-typescript-common-problems\/#article","isPartOf":{"@id":"https:\/\/www.thecodecampus.de\/blog\/converting-javascript-to-typescript-common-problems\/"},"author":{"name":"Roman Roelofsen","@id":"https:\/\/www.thecodecampus.de\/blog\/#\/schema\/person\/941e9a6169bf8da015ed1f348177a22d"},"headline":"Converting JavaScript to TypeScript &#8211; Common Problems","datePublished":"2015-09-16T13:35:55+00:00","dateModified":"2023-12-20T18:47:15+00:00","mainEntityOfPage":{"@id":"https:\/\/www.thecodecampus.de\/blog\/converting-javascript-to-typescript-common-problems\/"},"wordCount":449,"commentCount":0,"publisher":{"@id":"https:\/\/www.thecodecampus.de\/blog\/#organization"},"keywords":["JavaScript","TypeScript"],"articleSection":["TypeScript"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/www.thecodecampus.de\/blog\/converting-javascript-to-typescript-common-problems\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/www.thecodecampus.de\/blog\/converting-javascript-to-typescript-common-problems\/","url":"https:\/\/www.thecodecampus.de\/blog\/converting-javascript-to-typescript-common-problems\/","name":"Converting JavaScript to TypeScript - Common Problems - Web Development Blog","isPartOf":{"@id":"https:\/\/www.thecodecampus.de\/blog\/#website"},"datePublished":"2015-09-16T13:35:55+00:00","dateModified":"2023-12-20T18:47:15+00:00","description":"Elevate coding fluency! Overcome common pitfalls converting JavaScript to TypeScript. Streamline your transition and boost development efficiency now!","breadcrumb":{"@id":"https:\/\/www.thecodecampus.de\/blog\/converting-javascript-to-typescript-common-problems\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.thecodecampus.de\/blog\/converting-javascript-to-typescript-common-problems\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/www.thecodecampus.de\/blog\/converting-javascript-to-typescript-common-problems\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/www.thecodecampus.de\/blog\/"},{"@type":"ListItem","position":2,"name":"Converting JavaScript to TypeScript &#8211; Common Problems"}]},{"@type":"WebSite","@id":"https:\/\/www.thecodecampus.de\/blog\/#website","url":"https:\/\/www.thecodecampus.de\/blog\/","name":"Web Development tips and tricks - theCodeCampus Blog","description":"Tips, tricks, and experiences about developing web and mobile applications with Angular, TypeScript, and Testing.","publisher":{"@id":"https:\/\/www.thecodecampus.de\/blog\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.thecodecampus.de\/blog\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/www.thecodecampus.de\/blog\/#organization","name":"theCodeCampus","url":"https:\/\/www.thecodecampus.de\/blog\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.thecodecampus.de\/blog\/#\/schema\/logo\/image\/","url":"https:\/\/www.thecodecampus.de\/blog\/wp-content\/uploads\/2024\/01\/TCC-Logo-Bildmarke-quadratisch.jpg","contentUrl":"https:\/\/www.thecodecampus.de\/blog\/wp-content\/uploads\/2024\/01\/TCC-Logo-Bildmarke-quadratisch.jpg","width":156,"height":156,"caption":"theCodeCampus"},"image":{"@id":"https:\/\/www.thecodecampus.de\/blog\/#\/schema\/logo\/image\/"}},{"@type":"Person","@id":"https:\/\/www.thecodecampus.de\/blog\/#\/schema\/person\/941e9a6169bf8da015ed1f348177a22d","name":"Roman Roelofsen","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.thecodecampus.de\/blog\/wp-content\/uploads\/2024\/09\/roman-roelofsen-tcc-author-96x96.png","url":"https:\/\/www.thecodecampus.de\/blog\/wp-content\/uploads\/2024\/09\/roman-roelofsen-tcc-author-96x96.png","contentUrl":"https:\/\/www.thecodecampus.de\/blog\/wp-content\/uploads\/2024\/09\/roman-roelofsen-tcc-author-96x96.png","caption":"Roman Roelofsen"},"description":"Roman Roelofsen is the technical managing director and responsible for the product development and technical direction of W11K GmbH. He is an expert in the development of complex web applications and has been working in the field of web engineering for 20 years. Roman supports our customers in the implementation of IT projects and has been working as a trainer for many years.","sameAs":["https:\/\/thecodecampus.de\/ueber-uns\/trainer\/roman-roelofsen","https:\/\/www.linkedin.com\/in\/roman-roelofsen-91b2b35\/"],"url":"https:\/\/www.thecodecampus.de\/blog\/author\/rroelofsen\/"}]}},"_links":{"self":[{"href":"https:\/\/www.thecodecampus.de\/blog\/wp-json\/wp\/v2\/posts\/512","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.thecodecampus.de\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.thecodecampus.de\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.thecodecampus.de\/blog\/wp-json\/wp\/v2\/users\/5"}],"replies":[{"embeddable":true,"href":"https:\/\/www.thecodecampus.de\/blog\/wp-json\/wp\/v2\/comments?post=512"}],"version-history":[{"count":9,"href":"https:\/\/www.thecodecampus.de\/blog\/wp-json\/wp\/v2\/posts\/512\/revisions"}],"predecessor-version":[{"id":544,"href":"https:\/\/www.thecodecampus.de\/blog\/wp-json\/wp\/v2\/posts\/512\/revisions\/544"}],"wp:attachment":[{"href":"https:\/\/www.thecodecampus.de\/blog\/wp-json\/wp\/v2\/media?parent=512"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.thecodecampus.de\/blog\/wp-json\/wp\/v2\/categories?post=512"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.thecodecampus.de\/blog\/wp-json\/wp\/v2\/tags?post=512"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}