<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>Forem: Sean Johnson</title>
    <description>The latest articles on Forem by Sean Johnson (@sjohnsonaz).</description>
    <link>https://forem.com/sjohnsonaz</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F427184%2Fcae22dfe-4e29-4431-92fc-67d31b003faa.jpeg</url>
      <title>Forem: Sean Johnson</title>
      <link>https://forem.com/sjohnsonaz</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://forem.com/feed/sjohnsonaz"/>
    <language>en</language>
    <item>
      <title>Create a TypeScript project</title>
      <dc:creator>Sean Johnson</dc:creator>
      <pubDate>Wed, 09 Nov 2022 18:03:22 +0000</pubDate>
      <link>https://forem.com/sjohnsonaz/create-a-typescript-project-mco</link>
      <guid>https://forem.com/sjohnsonaz/create-a-typescript-project-mco</guid>
      <description>&lt;p&gt;Creating a TypeScript project is easy!&lt;/p&gt;

&lt;p&gt;Let's get up and running with build and testing scripts!&lt;/p&gt;

&lt;p&gt;You will need a few things first:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Install &lt;strong&gt;Node.js&lt;/strong&gt; from &lt;a href="https://nodejs.org/" rel="noopener noreferrer"&gt;nodejs.org&lt;/a&gt;.&lt;/li&gt;
&lt;li&gt;Install an editor, for example &lt;strong&gt;Visual Studio Code&lt;/strong&gt; from &lt;a href="https://code.visualstudio.com/" rel="noopener noreferrer"&gt;code.visualstudio.com&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;Open a &lt;strong&gt;terminal&lt;/strong&gt;, and get ready!&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Create your project folder
&lt;/h2&gt;

&lt;p&gt;Create a folder with your project's name in &lt;code&gt;[[Project Name]]&lt;/code&gt;.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;NOTE:&lt;/strong&gt; Node.js convention is to use &lt;code&gt;kebab-case&lt;/code&gt;. All letters are lower case, and dashes are used for spaces.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Run the following command:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nb"&gt;mkdir&lt;/span&gt; &lt;span class="o"&gt;[[&lt;/span&gt;Project Name]]
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Open with an Editor
&lt;/h3&gt;

&lt;p&gt;The following example is for Visual Studio Code. It will likely be similar for other editors.&lt;/p&gt;

&lt;p&gt;Open your folder in Visual Studio Code. Open Visual Studio Code, then navigate to &lt;strong&gt;File&lt;/strong&gt; -&amp;gt; &lt;strong&gt;Open Folder...&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;Alternatively, if you have the shortcut set up, you can run the following command:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;code &lt;span class="o"&gt;[[&lt;/span&gt;Project Name]]
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Once in the editor, open the &lt;strong&gt;Integrated Terminal&lt;/strong&gt; to continue. Navigate to &lt;strong&gt;View&lt;/strong&gt; -&amp;gt; &lt;strong&gt;Terminal&lt;/strong&gt;, or use the Keyboard Shortcut &lt;code&gt;Ctrl + `&lt;/code&gt; or &lt;code&gt;Cmd + `&lt;/code&gt;.&lt;/p&gt;

&lt;h3&gt;
  
  
  Continue in the system terminal
&lt;/h3&gt;

&lt;p&gt;Alternatively, change directory into the folder in the current terminal:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nb"&gt;cd&lt;/span&gt; &lt;span class="o"&gt;[[&lt;/span&gt;Project Name]]
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Install Dependencies
&lt;/h2&gt;

&lt;p&gt;Create your &lt;code&gt;package.json&lt;/code&gt;, which stores &lt;strong&gt;NPM&lt;/strong&gt; configuration.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;NOTE:&lt;/strong&gt; When prompted, it is HIGHLY recommended to start with version &lt;code&gt;0.0.0&lt;/code&gt; or &lt;code&gt;0.0.1&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;This implies that the project is in development. Starting with &lt;code&gt;1.0.0&lt;/code&gt; implies the project is &lt;strong&gt;stable&lt;/strong&gt;.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Run the following command:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;npm init
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Install &lt;strong&gt;TypeScript&lt;/strong&gt; and &lt;strong&gt;Jest&lt;/strong&gt; as &lt;code&gt;"devDependencies"&lt;/code&gt; in &lt;code&gt;package.json&lt;/code&gt;. This will also create a &lt;code&gt;package-lock.json&lt;/code&gt; file, which stores specific package version information.&lt;/p&gt;

&lt;p&gt;Run the following command:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;npm i &lt;span class="nt"&gt;-D&lt;/span&gt; typescript jest ts-jest @types/jest
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Configure TypeScript
&lt;/h2&gt;

&lt;p&gt;Creates your &lt;code&gt;tsconfig.json&lt;/code&gt; file, which stores TypeScript configuration.&lt;/p&gt;

&lt;p&gt;Run the following command:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;npx tsc &lt;span class="nt"&gt;--init&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Creates your &lt;code&gt;src/index.ts&lt;/code&gt; file.&lt;/p&gt;

&lt;p&gt;Run the following commands:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nb"&gt;mkdir &lt;/span&gt;src
&lt;span class="nb"&gt;touch&lt;/span&gt; ./src/index.ts
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In &lt;code&gt;tsconfig.json&lt;/code&gt;, update the following &lt;code&gt;"compilerOptions"&lt;/code&gt;. This configures TypeScript to use your source files, and generate all of the necessary declaration and map files. It also enabled ES Modules.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;  &lt;span class="s2"&gt;"module"&lt;/span&gt;: &lt;span class="s2"&gt;"ESNext"&lt;/span&gt;,
  &lt;span class="s2"&gt;"rootDir"&lt;/span&gt;: &lt;span class="s2"&gt;"./src"&lt;/span&gt;,
  &lt;span class="s2"&gt;"moduleResolution"&lt;/span&gt;: &lt;span class="s2"&gt;"node"&lt;/span&gt;,
  &lt;span class="s2"&gt;"declaration"&lt;/span&gt;: &lt;span class="nb"&gt;true&lt;/span&gt;,
  &lt;span class="s2"&gt;"declarationMap"&lt;/span&gt;: &lt;span class="nb"&gt;true&lt;/span&gt;,
  &lt;span class="s2"&gt;"sourceMap"&lt;/span&gt;: &lt;span class="nb"&gt;true&lt;/span&gt;,
  &lt;span class="s2"&gt;"outDir"&lt;/span&gt;: &lt;span class="s2"&gt;"./dist"&lt;/span&gt;,
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In &lt;code&gt;package.json&lt;/code&gt;, create the following properties. This configures your package to use the TypeScript generated files. It also enabled ES Modules.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;  &lt;span class="s2"&gt;"type"&lt;/span&gt;: &lt;span class="s2"&gt;"module"&lt;/span&gt;,
  &lt;span class="s2"&gt;"main"&lt;/span&gt;: &lt;span class="s2"&gt;"dist/index.js"&lt;/span&gt;,
  &lt;span class="s2"&gt;"types"&lt;/span&gt;: &lt;span class="s2"&gt;"dist/index.d.ts"&lt;/span&gt;,
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Configure Scripts
&lt;/h2&gt;

&lt;p&gt;In &lt;code&gt;package.json&lt;/code&gt;, create the following &lt;code&gt;"scripts"&lt;/code&gt;. These are common scripts for building and testing.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;  &lt;span class="s2"&gt;"scripts"&lt;/span&gt;: &lt;span class="o"&gt;{&lt;/span&gt;
    &lt;span class="s2"&gt;"test"&lt;/span&gt;: &lt;span class="s2"&gt;"jest"&lt;/span&gt;,
    &lt;span class="s2"&gt;"start"&lt;/span&gt;: &lt;span class="s2"&gt;"tsc --watch"&lt;/span&gt;,
    &lt;span class="s2"&gt;"build"&lt;/span&gt;: &lt;span class="s2"&gt;"tsc"&lt;/span&gt;,
    &lt;span class="s2"&gt;"clean"&lt;/span&gt;: &lt;span class="s2"&gt;"tsc --build --clean"&lt;/span&gt;,
    &lt;span class="s2"&gt;"prepublishOnly"&lt;/span&gt;: &lt;span class="s2"&gt;"tsc --build --clean &amp;amp;&amp;amp; tsc"&lt;/span&gt;
  &lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In &lt;code&gt;package.json&lt;/code&gt;, add the following section. This tells Jest how to find and run your TypeScript tests.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;  &lt;span class="s2"&gt;"jest"&lt;/span&gt;: &lt;span class="o"&gt;{&lt;/span&gt;
    &lt;span class="s2"&gt;"preset"&lt;/span&gt;: &lt;span class="s2"&gt;"ts-jest"&lt;/span&gt;,
    &lt;span class="s2"&gt;"testPathIgnorePatterns"&lt;/span&gt;: &lt;span class="o"&gt;[&lt;/span&gt;
      &lt;span class="s2"&gt;"&amp;lt;rootDir&amp;gt;/dist/"&lt;/span&gt;
    &lt;span class="o"&gt;]&lt;/span&gt;
  &lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Configure Git
&lt;/h2&gt;

&lt;p&gt;Create your Git configuration. It is important to check in only source files.&lt;/p&gt;

&lt;p&gt;Run the following commands:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;git init
&lt;span class="nb"&gt;touch&lt;/span&gt; .gitignore
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Add the following to &lt;code&gt;.gitignore&lt;/code&gt;. This will prevent checking in TypeScript generated files.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;node_modules/
dist/
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Configure NPM publish
&lt;/h2&gt;

&lt;p&gt;Create your NPM publish configuration. It is important to publish BOTH source files, and TypeScript generated files.&lt;/p&gt;

&lt;p&gt;Run the following command:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nb"&gt;touch&lt;/span&gt; .npmignore
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Add the following to &lt;code&gt;.npmignore&lt;/code&gt;. This will prevent checking in unnecessary files.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;NOTE:&lt;/strong&gt; Do not include &lt;code&gt;dist/&lt;/code&gt; or &lt;code&gt;src/&lt;/code&gt; in your &lt;code&gt;.npmignore&lt;/code&gt; file.&lt;br&gt;
&lt;/p&gt;
&lt;/blockquote&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;tsconfig.json
.gitignore
.npmignore
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



</description>
      <category>typescript</category>
      <category>javascript</category>
      <category>react</category>
      <category>node</category>
    </item>
  </channel>
</rss>
