<?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>coding - Hard Wired</title>
	<atom:link href="https://www.hardwired.dev/tag/coding/feed/" rel="self" type="application/rss+xml" />
	<link>https://www.hardwired.dev</link>
	<description></description>
	<lastBuildDate>Sat, 25 May 2024 14:00:55 +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>coding - Hard Wired</title>
	<link>https://www.hardwired.dev</link>
	<width>32</width>
	<height>32</height>
</image> 
	<item>
		<title>How to create TypeScript NPM package project</title>
		<link>https://www.hardwired.dev/2024/05/26/how-to-create-typescript-npm-package-project/</link>
		
		<dc:creator><![CDATA[John Doe]]></dc:creator>
		<pubDate>Sun, 26 May 2024 13:46:32 +0000</pubDate>
				<category><![CDATA[Development]]></category>
		<category><![CDATA[coding]]></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=2200</guid>

					<description><![CDATA[<p>Sometimes you want to create your own npm package. Here is the way to set up an npm package in &#62;&#62;&#62;</p>
<p>The post <a href="https://www.hardwired.dev/2024/05/26/how-to-create-typescript-npm-package-project/">How to create TypeScript NPM package 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>Sometimes you want to create your own <a href="https://www.npmjs.com/"><code>npm</code></a> package. Here is the way to set up an <code>npm</code> package in <a href="https://www.typescriptlang.org/"><code>TypeScript</code></a>.</p>
<p>In your project directory, initialize the <code>npm</code> object.</p>
<pre><code class="language-bash">npm init</code></pre>
<p>Install development dependencies.</p>
<pre><code class="language-bash">npm install --save-dev typescript ts-node</code></pre>
<p><code>ts-node</code> enables you to directly execute <code>TypeScript</code> on <a href="https://nodejs.org/en"><code>Node.js</code></a>. See more on the <a href="https://www.npmjs.com/package/ts-node">package website</a>.</p>
<p>Set up the <code>TypeScript</code> project.</p>
<pre><code class="language-bash">npx tsc --init</code></pre>
<p>In the generated <code>tsconfig.json</code>, set <code>outDir</code> to <code>dist</code>.</p>
<pre><code class="language-json">...
&quot;outDir&quot;: &quot;./dist&quot;, /* Specify an output folder for all emitted files. */
...</code></pre>
<p>In the root directory, create the <code>src</code> folder and in the folder <code>index.ts</code> which will be the entry point in your package.</p>
<pre><code class="language-bash">mkdir src
touch ./src/index.ts</code></pre>
<p>Create a <code>.gitignore</code> file.</p>
<pre><code class="language-ini">/node_modules
# Ignore test-related files
/coverage.data
/coverage/
# Build files
/dist</code></pre>
<p>Install the <a href="https://www.npmjs.com/package/tsup"><code>tsup</code></a> package. It is a <code>bundler</code> and is powered by <a href="https://github.com/evanw/esbuild"><code>esbuild</code></a>.</p>
<blockquote>
<p>In the context of JavaScript, a <strong>bundler</strong> is a tool that takes multiple JavaScript files and their dependencies and combines them into a single file or a few files, which can then be included in a web application. The main purpose of a bundler is to manage and optimize the delivery of code to the browser, enhancing performance and simplifying development.</p>
</blockquote>
<pre><code class="language-bash">
npm install --save-dev tsup</code></pre>
<p>In the root directory, create the <code>tsup.config.ts</code> file.</p>
<pre><code class="language-typescript">import { defineConfig } from &quot;tsup&quot;;
export default defineConfig({
entry: [&quot;src/index.ts&quot;],
format: [&quot;cjs&quot;, &quot;esm&quot;], // Build for commonJS and ESmodules
dts: true, // Generate declaration file (.d.ts)
splitting: false,
sourcemap: true,
clean: true,
});</code></pre>
<p>Update these properties in <code>package.json</code></p>
<pre><code class="language-json">...
&quot;main&quot;: &quot;./dist/index.js&quot;,
&quot;module&quot;: &quot;./dist/index.mjs&quot;,
&quot;types&quot;: &quot;./dist/index.d.ts&quot;,
&quot;files&quot;: [
    &quot;dist&quot;
],
&quot;scripts&quot;: {
    &quot;build&quot;: &quot;tsup&quot;
},
...</code></pre>
<p>In your <code>index.ts</code>, create a trivial function you will export in this package.</p>
<pre><code class="language-typescript">export function add(a: number, b: number): number {
  return a + b;
}</code></pre>
<p>Build your package with <code>npm run build</code>. In the <code>dist</code> directory, you will see the build of your project.</p>
<p>TADA! Now you have a base project for your <code>npm</code> package.</p>

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