{"id":7763,"date":"2019-12-11T20:37:27","date_gmt":"2019-12-11T20:37:27","guid":{"rendered":"http:\/\/putridparrot.com\/blog\/?p=7763"},"modified":"2019-12-11T20:37:27","modified_gmt":"2019-12-11T20:37:27","slug":"adding-typescript-to-electron","status":"publish","type":"post","link":"https:\/\/putridparrot.com\/blog\/adding-typescript-to-electron\/","title":{"rendered":"Adding TypeScript to Electron"},"content":{"rendered":"<p>So we&#8217;ve seen that Electron is basically a browser window with integrations which allows us to use JavaScript to interact with it, but as somebody who prefers the type safety features that come with TypeScript, obviously I&#8217;d want to integrate TypeScript as well.<\/p>\n<p>If you&#8217;ve not got TypeScript installed globally then run <\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\r\nnpm install --save-dev typescript\r\n<\/pre>\n<p>If we take our application from the <a href=\"http:\/\/putridparrot.com\/blog\/getting-started-with-electron\/\" rel=\"noopener noreferrer\" target=\"_blank\">Getting Started<\/a> post, lets simply started by adding a tsconfig, just run the following from your project&#8217;s root folder<\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\r\ntsc --init\r\n<\/pre>\n<p>Now change our main.js to main.ts. We&#8217;ll obviously need a build step to transpile the TypeScript to JavaScript which can then be uses via Electron, so add the following to the scripts section in package.json<\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\r\n&quot;build&quot;: &quot;tsc&quot;\r\n<\/pre>\n<p>you might also like to either rename the start script or add another script to both build\/transpile and run the application, i.e.<\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\r\n&quot;go&quot;: &quot;tsc &amp;&amp; electron .&quot;\r\n<\/pre>\n<p>Obviously this will litter your code base with generated .js files, so it&#8217;s best to transpile our code to a folder of it&#8217;s own, in this case we&#8217;ll call it src. Just add the following to the tsconfig.json<\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\r\n&quot;outDir&quot;: &quot;.\/src&quot;,\r\n<\/pre>\n<p>Then change package.json &#8220;main&#8221; to the following<\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\r\n&quot;main&quot;: &quot;.\/src\/main.js&quot;,\r\n<\/pre>\n<p>That&#8217;s all there is to it, now we can add some type checking to our code and write TypeScript.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>So we&#8217;ve seen that Electron is basically a browser window with integrations which allows us to use JavaScript to interact with it, but as somebody who prefers the type safety features that come with TypeScript, obviously I&#8217;d want to integrate TypeScript as well. If you&#8217;ve not got TypeScript installed globally then run npm install &#8211;save-dev [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[276,46],"tags":[],"class_list":["post-7763","post","type-post","status-publish","format-standard","hentry","category-electron","category-typescript"],"jetpack_sharing_enabled":true,"jetpack_featured_media_url":"","_links":{"self":[{"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/posts\/7763","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/comments?post=7763"}],"version-history":[{"count":3,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/posts\/7763\/revisions"}],"predecessor-version":[{"id":7788,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/posts\/7763\/revisions\/7788"}],"wp:attachment":[{"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/media?parent=7763"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/categories?post=7763"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/tags?post=7763"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}