{"id":2173,"date":"2017-03-20T10:18:13","date_gmt":"2017-03-20T10:18:13","guid":{"rendered":"http:\/\/myprojects.advchaweb.com\/?p=2173"},"modified":"2017-03-20T10:50:13","modified_gmt":"2017-03-20T10:50:13","slug":"typescript","status":"publish","type":"post","link":"https:\/\/myprojects.advchaweb.com\/index.php\/2017\/03\/20\/typescript\/","title":{"rendered":"TypeScript"},"content":{"rendered":"<p>Ref: https:\/\/www.typescriptlang.org\/docs\/tutorial.html<br \/>\nInstallation:<\/p>\n<ol>\n<li>Install from npm\n<pre class=\"lang:default decode:true \">teddy@teddy-K43SJ:~$ sudo npm install -g typescript\r\n[sudo] password for teddy: \r\n\/usr\/local\/bin\/tsc -&gt; \/usr\/local\/lib\/node_modules\/typescript\/bin\/tsc\r\n\/usr\/local\/bin\/tsserver -&gt; \/usr\/local\/lib\/node_modules\/typescript\/bin\/tsserver\r\n\/usr\/local\/lib\r\n\u2514\u2500\u2500 typescript@2.2.1<\/pre>\n<p>Check the typescript version:<\/p>\n<pre class=\"lang:default decode:true \">teddy@teddy-K43SJ:~$ tsc --version\r\nVersion 2.2.1<\/pre>\n<p>&nbsp;<\/li>\n<li>Try it via Visual Studio Code.<br \/>\nCreate a new file &#8216;greeter.ts&#8217; and here is the content:<\/p>\n<pre class=\"lang:default decode:true \">function greeter(person) {\r\n    return \"Hello, \" + person;\r\n}\r\n\r\nvar user = \"Jane User\";\r\n\r\ndocument.body.innerHTML = greeter(user);<\/pre>\n<p>Then compile it via<\/p>\n<pre class=\"lang:default decode:true \">teddy@teddy-K43SJ:~\/Documents\/node\/typescript$ tsc greeter.ts<\/pre>\n<p>It&#8217;d create a new js file: greeter.js<a href=\"http:\/\/myprojects.advchaweb.com\/wp-content\/uploads\/2017\/03\/ts-1.jpg\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-2176\" src=\"http:\/\/myprojects.advchaweb.com\/wp-content\/uploads\/2017\/03\/ts-1.jpg\" alt=\"\" width=\"631\" height=\"681\" srcset=\"https:\/\/myprojects.advchaweb.com\/wp-content\/uploads\/2017\/03\/ts-1.jpg 631w, https:\/\/myprojects.advchaweb.com\/wp-content\/uploads\/2017\/03\/ts-1-278x300.jpg 278w\" sizes=\"auto, (max-width: 709px) 85vw, (max-width: 909px) 67vw, (max-width: 984px) 61vw, (max-width: 1362px) 45vw, 600px\" \/><\/a>greeter.js has same content with greeter.ts.<\/li>\n<li>TypeScript Interface<br \/>\nCreate a new file &#8216;greeter-interface.ts&#8217;:<\/p>\n<pre class=\"lang:default decode:true \">interface Person{\r\n    firstName: string;\r\n    lastName: string;\r\n}\r\n\r\nfunction greeter(person: Person){\r\n    return \"Hello, \" + person.firstName + \" \" + person.lastName;\r\n}\r\n\r\nvar user = {firstName: \"Jane\", lastName: \"Doe\"};\r\n\r\ndocument.body.innerHTML = greeter(user);<\/pre>\n<p>Then compile it<\/p>\n<pre class=\"lang:default decode:true\">teddy@teddy-K43SJ:~\/Documents\/node\/typescript$ tsc greeter-interface.ts<\/pre>\n<p>Here is the output &#8216;greeter-interface.js&#8217;<\/p>\n<pre class=\"lang:default decode:true \">function greeter(person) {\r\n    return \"Hello, \" + person.firstName + \" \" + person.lastName;\r\n}\r\nvar user = { firstName: \"Jane\", lastName: \"Doe\" };\r\ndocument.body.innerHTML = greeter(user);<\/pre>\n<p><a href=\"http:\/\/myprojects.advchaweb.com\/wp-content\/uploads\/2017\/03\/Screenshot-from-2017-03-20-173652.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-large wp-image-2177\" src=\"http:\/\/myprojects.advchaweb.com\/wp-content\/uploads\/2017\/03\/Screenshot-from-2017-03-20-173652-1024x576.png\" alt=\"\" width=\"840\" height=\"473\" srcset=\"https:\/\/myprojects.advchaweb.com\/wp-content\/uploads\/2017\/03\/Screenshot-from-2017-03-20-173652-1024x576.png 1024w, https:\/\/myprojects.advchaweb.com\/wp-content\/uploads\/2017\/03\/Screenshot-from-2017-03-20-173652-300x169.png 300w, https:\/\/myprojects.advchaweb.com\/wp-content\/uploads\/2017\/03\/Screenshot-from-2017-03-20-173652-768x432.png 768w, https:\/\/myprojects.advchaweb.com\/wp-content\/uploads\/2017\/03\/Screenshot-from-2017-03-20-173652-1200x675.png 1200w, https:\/\/myprojects.advchaweb.com\/wp-content\/uploads\/2017\/03\/Screenshot-from-2017-03-20-173652.png 1366w\" sizes=\"auto, (max-width: 709px) 85vw, (max-width: 909px) 67vw, (max-width: 1362px) 62vw, 840px\" \/><\/a><\/li>\n<li>TypeScript Class<br \/>\nCreate a new file &#8216;greeter-class.ts&#8217;:<\/p>\n<pre class=\"lang:default decode:true \">class Student {\r\n    fullName: string;\r\n    constructor(public firstName, public middleInitial, public lastName){\r\n        this.fullName = firstName + \" \" + middleInitial + \" \" + lastName;\r\n    }\r\n}\r\n\r\ninterface Person{\r\n    firstName: string;\r\n    lastName: string;\r\n}\r\n\r\nfunction greeter(person: Person){\r\n    return \"Hello, \" + person.firstName + \" \" + person.lastName;\r\n}\r\n\r\nvar user = new Student(\"Jane\", \"M.\", \"Doe\");\r\n\r\ndocument.body.innerHTML = greeter(user);<\/pre>\n<p>Then compile it<\/p>\n<pre class=\"lang:default decode:true \">teddy@teddy-K43SJ:~\/Documents\/node\/typescript$ tsc greeter-class.ts<\/pre>\n<p>Here is the output &#8216;greeter-class.js&#8217;<\/p>\n<pre class=\"lang:default decode:true \">var Student = (function () {\r\n    function Student(firstName, middleInitial, lastName) {\r\n        this.firstName = firstName;\r\n        this.middleInitial = middleInitial;\r\n        this.lastName = lastName;\r\n        this.fullName = firstName + \" \" + middleInitial + \" \" + lastName;\r\n    }\r\n    return Student;\r\n}());\r\nfunction greeter(person) {\r\n    return \"Hello, \" + person.firstName + \" \" + person.lastName;\r\n}\r\nvar user = new Student(\"Jane\", \"M.\", \"Doe\");\r\ndocument.body.innerHTML = greeter(user);<\/pre>\n<p><a href=\"http:\/\/myprojects.advchaweb.com\/wp-content\/uploads\/2017\/03\/Screenshot-from-2017-03-20-174355.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-large wp-image-2179\" src=\"http:\/\/myprojects.advchaweb.com\/wp-content\/uploads\/2017\/03\/Screenshot-from-2017-03-20-174355-1024x576.png\" alt=\"\" width=\"840\" height=\"473\" srcset=\"https:\/\/myprojects.advchaweb.com\/wp-content\/uploads\/2017\/03\/Screenshot-from-2017-03-20-174355-1024x576.png 1024w, https:\/\/myprojects.advchaweb.com\/wp-content\/uploads\/2017\/03\/Screenshot-from-2017-03-20-174355-300x169.png 300w, https:\/\/myprojects.advchaweb.com\/wp-content\/uploads\/2017\/03\/Screenshot-from-2017-03-20-174355-768x432.png 768w, https:\/\/myprojects.advchaweb.com\/wp-content\/uploads\/2017\/03\/Screenshot-from-2017-03-20-174355-1200x675.png 1200w, https:\/\/myprojects.advchaweb.com\/wp-content\/uploads\/2017\/03\/Screenshot-from-2017-03-20-174355.png 1366w\" sizes=\"auto, (max-width: 709px) 85vw, (max-width: 909px) 67vw, (max-width: 1362px) 62vw, 840px\" \/><\/a><\/li>\n<li>Open it on your web browser<br \/>\nCreate a new file &#8216;greeter.html&#8217;<\/p>\n<pre class=\"lang:default decode:true \">&lt;!DOCTYPE html&gt;\r\n&lt;html&gt;\r\n    &lt;head&gt;&lt;title&gt;TypeScript Greeter&lt;\/title&gt;&lt;\/head&gt;\r\n    &lt;body&gt;\r\n        &lt;script src=\"greeter-class.js\"&gt;&lt;\/script&gt;\r\n    &lt;\/body&gt;\r\n&lt;\/html&gt;<\/pre>\n<p>Then open the html file on your browser. It&#8217;d show like this:<\/p>\n<pre class=\"lang:default decode:true \">Hello, Jane Doe<\/pre>\n<p>&nbsp;<\/li>\n<\/ol>\n","protected":false},"excerpt":{"rendered":"<p>Ref: https:\/\/www.typescriptlang.org\/docs\/tutorial.html Installation: Install from npm teddy@teddy-K43SJ:~$ sudo npm install -g typescript [sudo] password for teddy: \/usr\/local\/bin\/tsc -&gt; \/usr\/local\/lib\/node_modules\/typescript\/bin\/tsc \/usr\/local\/bin\/tsserver -&gt; \/usr\/local\/lib\/node_modules\/typescript\/bin\/tsserver \/usr\/local\/lib \u2514\u2500\u2500 typescript@2.2.1 Check the typescript version: teddy@teddy-K43SJ:~$ tsc &#8211;version Version 2.2.1 &nbsp; Try it via Visual Studio Code. Create a new file &#8216;greeter.ts&#8217; and here is the content: function greeter(person) { return &hellip; <a href=\"https:\/\/myprojects.advchaweb.com\/index.php\/2017\/03\/20\/typescript\/\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &#8220;TypeScript&#8221;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[74],"tags":[],"class_list":["post-2173","post","type-post","status-publish","format-standard","hentry","category-typescript"],"_links":{"self":[{"href":"https:\/\/myprojects.advchaweb.com\/index.php\/wp-json\/wp\/v2\/posts\/2173","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/myprojects.advchaweb.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/myprojects.advchaweb.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/myprojects.advchaweb.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/myprojects.advchaweb.com\/index.php\/wp-json\/wp\/v2\/comments?post=2173"}],"version-history":[{"count":5,"href":"https:\/\/myprojects.advchaweb.com\/index.php\/wp-json\/wp\/v2\/posts\/2173\/revisions"}],"predecessor-version":[{"id":2181,"href":"https:\/\/myprojects.advchaweb.com\/index.php\/wp-json\/wp\/v2\/posts\/2173\/revisions\/2181"}],"wp:attachment":[{"href":"https:\/\/myprojects.advchaweb.com\/index.php\/wp-json\/wp\/v2\/media?parent=2173"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/myprojects.advchaweb.com\/index.php\/wp-json\/wp\/v2\/categories?post=2173"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/myprojects.advchaweb.com\/index.php\/wp-json\/wp\/v2\/tags?post=2173"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}