{"id":4383,"date":"2016-06-06T12:12:18","date_gmt":"2016-06-06T12:12:18","guid":{"rendered":"http:\/\/www.garysieling.com\/blog\/?p=4383"},"modified":"2016-06-06T12:12:18","modified_gmt":"2016-06-06T12:12:18","slug":"running-typescript-within-node","status":"publish","type":"post","link":"https:\/\/www.garysieling.com\/blog\/running-typescript-within-node\/","title":{"rendered":"Running Typescript from within Node"},"content":{"rendered":"<p>To run Typescript in Node, you can either compile the code in advance, or at runtime. Doing this at runtime is convenient, since you probably would be compiling + running the code at the same time every time you run your application.<\/p>\n<p>The easiest way I&#8217;ve found to do this is to install ts-node:<\/p>\n<pre lang=\"bash\">\nnpm install ts-node\n<\/pre>\n<p>Then make a small index.js script, which functions as a shim. All the Express.js code can then be moved into a Typescript file.<\/p>\n<p>index.js:<\/p>\n<pre lang=\"javascript\">\n\/\/ Fix incompatibility between React 15 and different Webkit versions\ndelete Object.assign;\nObject.assign = require(\"object.assign\").shim();\n\n\/\/ Allow Node to read JSX files\nrequire(\"node-jsx\").install({extension: \".jsx\"});\n\n\/\/ Allow Node to read TS + TSX files\nrequire(\"ts-node\").register();\n\nrequire(\".\/app.ts\")[\"init\"]();\n<\/pre>\n<p>In the app.ts file, you can add all of the regular Express initialization things, as you&#8217;d expect, but now you can use types:<\/p>\n<pre lang=\"javascript\">\nfunction init() {\n const express = require(\"express\");\n const router = express.Router();\n\n router.use(function (req,res,next) {\n    next();\n  });\n}\n\nexport { init as init };\n<\/pre>\n<p>I wrapped the entire thing in a function, which is exported and called by the main index.js script.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Running Typescript from within Node<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"om_disable_all_campaigns":false,"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"footnotes":""},"categories":[4],"tags":[302,387],"aioseo_notices":[],"amp_enabled":true,"_links":{"self":[{"href":"https:\/\/www.garysieling.com\/blog\/wp-json\/wp\/v2\/posts\/4383"}],"collection":[{"href":"https:\/\/www.garysieling.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.garysieling.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.garysieling.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.garysieling.com\/blog\/wp-json\/wp\/v2\/comments?post=4383"}],"version-history":[{"count":0,"href":"https:\/\/www.garysieling.com\/blog\/wp-json\/wp\/v2\/posts\/4383\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.garysieling.com\/blog\/wp-json\/wp\/v2\/media?parent=4383"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.garysieling.com\/blog\/wp-json\/wp\/v2\/categories?post=4383"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.garysieling.com\/blog\/wp-json\/wp\/v2\/tags?post=4383"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}