<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>gts - Hard Wired</title>
	<atom:link href="https://www.hardwired.dev/tag/gts/feed/" rel="self" type="application/rss+xml" />
	<link>https://www.hardwired.dev</link>
	<description></description>
	<lastBuildDate>Sat, 25 May 2024 13:59:43 +0000</lastBuildDate>
	<language>cs</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=6.9</generator>

<image>
	<url>https://www.hardwired.dev/wp-content/uploads/2022/10/android-chrome-256x256-1-150x150.png</url>
	<title>gts - Hard Wired</title>
	<link>https://www.hardwired.dev</link>
	<width>32</width>
	<height>32</height>
</image> 
	<item>
		<title>How to create a new TypeScript project</title>
		<link>https://www.hardwired.dev/2024/05/25/how-to-create-a-new-typescript-project/</link>
		
		<dc:creator><![CDATA[John Doe]]></dc:creator>
		<pubDate>Sat, 25 May 2024 06:01:43 +0000</pubDate>
				<category><![CDATA[Development]]></category>
		<category><![CDATA[coding]]></category>
		<category><![CDATA[development]]></category>
		<category><![CDATA[gts]]></category>
		<category><![CDATA[javascript]]></category>
		<category><![CDATA[nodejs]]></category>
		<category><![CDATA[programming]]></category>
		<category><![CDATA[typescript]]></category>
		<guid isPermaLink="false">https://www.hardwired.dev/?p=2197</guid>

					<description><![CDATA[<p>TypeScript compared to JavaScript is a little bit more complex. If you do server-side JavaScript, you just call node your-script.js &#62;&#62;&#62;</p>
<p>The post <a href="https://www.hardwired.dev/2024/05/25/how-to-create-a-new-typescript-project/">How to create a new TypeScript project</a> first appeared on <a href="https://www.hardwired.dev">Hard Wired</a>.</p>]]></description>
										<content:encoded><![CDATA[<div id="bsf_rt_marker"></div><p>TypeScript compared to JavaScript is a little bit more complex. If you do server-side JavaScript, you just call <code>node your-script.js</code> and it runs. When you want to use TypeScript, you need to install a few things and do a few steps to make your code runnable.</p>
<blockquote>
<p>Make sure you have installed the latest version of <code>nodejs</code> on your system.</p>
</blockquote>
<p>Create your project folder, change the current working directory, and install <code>typescript</code> as your development dependency.</p>
<pre><code class="language-bash">npm i -D typescript</code></pre>
<p>Initialize your <code>nodejs</code> project.</p>
<pre><code class="language-bash">npm init</code></pre>
<p>Then use <code>npx</code> to initialize a new <code>typescript</code> project.</p>
<pre><code class="language-bash">npx tsc --init</code></pre>
<p>A bunch of files should be in your project:</p>
<ul>
<li>package.json</li>
<li>package-lock.json</li>
<li>tsconfig.json</li>
</ul>
<p>Edit <code>tsconfig.json</code>, find <code>outDir</code>, and change it if you do not want compiled files in your root directory. Change it to the <code>build</code> folder.</p>
<pre><code class="language-json">...
outDir&quot;: &quot;./build&quot;,   /* Specify an output folder for all emitted files. */
...</code></pre>
<p>Now it's time to create your first TypeScript code.</p>
<p>Create a file <code>index.ts</code> and just put some random code.</p>
<pre><code class="language-typescript">async function main() {
    const i: number = 1;

    console.log(&#039;Random stuff&#039;, i);
}

main()</code></pre>
<p>Save it and then you can do a manual compilation.</p>
<pre><code class="language-bash">npx tsc</code></pre>
<p>This compiles your project and the output will be stored in the <code>build</code> folder. You will find the file <code>index.js</code> there. Do a test run with <code>node ./build/index.js</code>.</p>
<p>In the console, you should see:</p>
<pre><code class="language-bash">Random stuff 1</code></pre>
<p>You can use <code>npx tsc -w</code> to activate watch mode. When you change anything in your project, it will be recompiled automatically.</p>
<p>To make it a little bit easier, you can edit the <code>scripts</code> property in <code>package.json</code> to merge compilation and run into one command.</p>
<pre><code class="language-json">...
&quot;scripts&quot;: {
        &quot;start&quot;: &quot;npx tsc &amp;&amp; node ./build/index.js&quot;
},
...</code></pre>
<p>When you call <code>npm run start</code>, your project will be compiled and run.</p>
<p>During code development, you should use a linter to check your code using certain rules to avoid code inconsistency. An easy way is to use <code>Google TypeScript Style</code>.</p>
<p>Install <code>gts</code> as a development dependency.</p>
<pre><code class="language-bash">npm i -D gts</code></pre>
<p>Then initialize <code>gts</code> with <code>npx</code>.</p>
<pre><code class="language-bash">npx gts init</code></pre>
<p>It will ask you some questions. If your <code>typescript</code> version is higher than suggested, just say no. Then you will be asked about updating <code>tsconfig.json</code>, say yes.</p>
<p>Some big changes will be made.</p>
<p>The <code>src</code> folder will be created with an <code>index.ts</code> file in it. Remove your old <code>index.ts</code> from the root folder.</p>
<p>In <code>package.json</code>, update your start script.</p>
<pre><code class="language-json">...
&quot;start&quot;: &quot;npx tsc &amp;&amp; node ./build/src/index.js&quot;,
...</code></pre>
<blockquote>
<p>Make sure you have <code>eslint</code> support activated in your editor</p>
</blockquote>
<p>You will get an <code>index.ts</code> example file in the <code>src</code> directory. It is all wrong. If you have <code>eslint</code> support activated in your editor, it should scream at you with lots of errors.</p>
<pre><code class="language-typescript">console.log(&quot;Try npm run lint/fix!&quot;);

const longString = &#039;Lorem ipsum dolor sit amet, consectetur adipiscing elit. Integer ut aliquet diam.&#039;;

const trailing = &#039;Semicolon&#039;

            const why={am:&#039;I tabbed?&#039;};

const iWish = &quot;I didn&#039;t have a trailing space...&quot;; 

const sicilian = true;;

const vizzini = (!!sicilian) ? !!!sicilian : sicilian;

const re = /foo   bar/;

export function doSomeStuff(withThis: string, andThat: string, andThose: string[]) {
    //function on one line
    if(!Boolean(andThose.length)) {return false;}
    console.log(withThis);
    console.log(andThat);
    console.dir(andThose);
    console.log(longString, trailing, why, iWish, vizzini, re);
    return;
}
// TODO: more examples</code></pre>
<p>Just run <code>npm run fix</code> to let <code>gts</code> fix your code. You will get this:</p>
<pre><code class="language-typescript">console.log(&#039;Try npm run lint/fix!&#039;);

const longString =
  &#039;Lorem ipsum dolor sit amet, consectetur adipiscing elit. Integer ut aliquet diam.&#039;;

const trailing = &#039;Semicolon&#039;;

const why = {am: &#039;I tabbed?&#039;};

const iWish = &quot;I didn&#039;t have a trailing space...&quot;;

const sicilian = true;

const vizzini = sicilian ? !sicilian : sicilian;

const re = /foo {3}bar/;

export function doSomeStuff(
  withThis: string,
  andThat: string,
  andThose: string[]
) {
  //function on one line
  if (!andThose.length) {
    return false;
  }
  console.log(withThis);
  console.log(andThat);
  console.dir(andThose);
  console.log(longString, trailing, why, iWish, vizzini, re);
  return;
}
// TODO: more examples</code></pre>
<p>If it is working, you have just created your <code>typescript</code> project with a linter. It is a good thing to configure your code editor to perform <code>npm run fix</code> with every save.</p>
<p>Enjoy TypeScript coding.</p>

<div class="twitter-share"><a href="https://twitter.com/intent/tweet?url=https%3A%2F%2Fwww.hardwired.dev%2F2024%2F05%2F25%2Fhow-to-create-a-new-typescript-project%2F&#038;via=hessevalentino" class="twitter-share-button">Tweet</a></div><p>The post <a href="https://www.hardwired.dev/2024/05/25/how-to-create-a-new-typescript-project/">How to create a new TypeScript project</a> first appeared on <a href="https://www.hardwired.dev">Hard Wired</a>.</p>]]></content:encoded>
					
		
		
			</item>
	</channel>
</rss>
