{"id":197,"date":"2024-09-10T08:30:00","date_gmt":"2024-09-10T12:30:00","guid":{"rendered":"\/blog\/?p=197"},"modified":"2024-09-08T17:40:53","modified_gmt":"2024-09-08T21:40:53","slug":"how-to-start-a-typescript-project-from-scratch","status":"publish","type":"post","link":"\/blog\/how-to-start-a-typescript-project-from-scratch","title":{"rendered":"How to start a TypeScript project from scratch"},"content":{"rendered":"\n<p>Starting a TypeScript project from scratch involves several steps, including setting up the environment, initializing the project, and configuring TypeScript. Here\u2019s a step-by-step guide to help you get started:<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">1. Install Node.js and npm<\/h3>\n\n\n\n<p>Before you start, make sure you have <strong>Node.js<\/strong> and <strong>npm<\/strong> (Node Package Manager) installed on your system. You can check if they are installed by running the following commands in your terminal:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"bash\" class=\"language-bash\">node -v\nnpm -v<\/code><\/pre>\n\n\n\n<p>If not, download and install Node.js from the <a href=\"https:\/\/nodejs.org\/\">official website<\/a>. npm comes bundled with Node.js.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">2. Set Up a New Directory for Your Project<\/h3>\n\n\n\n<p>Create a new directory for your TypeScript project and navigate into it:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"bash\" class=\"language-bash\">mkdir my-typescript-project\ncd my-typescript-project<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">3. Initialize the Project<\/h3>\n\n\n\n<p>Use npm to initialize a new project. This will generate a <code>package.json<\/code> file:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"bash\" class=\"language-bash\">npm init -y<\/code><\/pre>\n\n\n\n<p>This will set up a basic <code>package.json<\/code> file with default values. You can manually edit this file later.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">4. Install TypeScript<\/h3>\n\n\n\n<p>Next, install TypeScript as a development dependency:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"bash\" class=\"language-bash\">npm install typescript --save-dev<\/code><\/pre>\n\n\n\n<p>This will install TypeScript locally in your project.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">5. Create a TypeScript Configuration File<\/h3>\n\n\n\n<p>Generate a <code>tsconfig.json<\/code> file, which will be used to configure the TypeScript compiler:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"bash\" class=\"language-bash\">npx tsc --init<\/code><\/pre>\n\n\n\n<p>This will create a <code>tsconfig.json<\/code> file with default options. You can customize it according to your project&#8217;s needs. Some common options to adjust:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"json\" class=\"language-json line-numbers\">{\n  \"compilerOptions\": {\n    \"target\": \"ES6\",\n    \"module\": \"commonjs\",\n    \"outDir\": \".\/dist\",\n    \"rootDir\": \".\/src\",\n    \"strict\": true,\n    \"esModuleInterop\": true\n  },\n  \"include\": [\"src\"],\n  \"exclude\": [\"node_modules\"]\n}<\/code><\/pre>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>target<\/code>: Specifies the JavaScript version to compile to (e.g., ES5, ES6).<\/li>\n\n\n\n<li><code>module<\/code>: Defines the module system (e.g., CommonJS for Node.js projects).<\/li>\n\n\n\n<li><code>outDir<\/code>: Specifies where the compiled JavaScript files should go.<\/li>\n\n\n\n<li><code>rootDir<\/code>: The folder that contains your TypeScript source files.<\/li>\n\n\n\n<li><code>strict<\/code>: Enables strict type-checking options.<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\">6. Create a <code>src<\/code> Directory<\/h3>\n\n\n\n<p>Create a folder for your TypeScript source files:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"bash\" class=\"language-bash\">mkdir src<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">7. Write a TypeScript File<\/h3>\n\n\n\n<p>Create a simple TypeScript file in the <code>src<\/code> directory, for example, <code>index.ts<\/code>:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"typescript\" class=\"language-typescript line-numbers\">\/\/ src\/index.ts\nconst greeting: string = \"Hello, TypeScript!\";\nconsole.log(greeting);<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">8. Compile TypeScript to JavaScript<\/h3>\n\n\n\n<p>To compile your TypeScript code into JavaScript, run:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"bash\" class=\"language-bash\">npx tsc<\/code><\/pre>\n\n\n\n<p>This will compile the TypeScript files in your <code>src<\/code> folder and output JavaScript files in the <code>dist<\/code> folder (as specified in <code>tsconfig.json<\/code>).<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">9. Run the Compiled JavaScript<\/h3>\n\n\n\n<p>Run the generated JavaScript file with Node.js:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"bash\" class=\"language-bash\">node dist\/index.js<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">10. Automate Compilation (Optional)<\/h3>\n\n\n\n<p>You can add a script in your <code>package.json<\/code> to compile TypeScript easily:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"json\" class=\"language-json\">{\n  \"scripts\": {\n    \"build\": \"tsc\",\n    \"start\": \"node dist\/index.js\"\n  }\n}<\/code><\/pre>\n\n\n\n<p>Now, you can compile TypeScript and run the project with:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"bash\" class=\"language-bash\">npm run build\nnpm start<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">11. Optional: Install ts-node for Development<\/h3>\n\n\n\n<p>For quicker development, you can install <code>ts-node<\/code>, which allows you to run TypeScript files directly without compiling them:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"bash\" class=\"language-bash\">npm install ts-node --save-dev<\/code><\/pre>\n\n\n\n<p>Then, you can run your TypeScript files directly using:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"bash\" class=\"language-bash\">npx ts-node src\/index.ts<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">12. Optional: Install ESLint for Linting<\/h3>\n\n\n\n<p>You can install ESLint to ensure your code follows best practices:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"bash\" class=\"language-bash\">npm install eslint --save-dev\nnpx eslint --init<\/code><\/pre>\n\n\n\n<p>You can configure it to work with TypeScript during the setup.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Summary<\/h3>\n\n\n\n<ol class=\"wp-block-list\">\n<li>Install Node.js and npm.<\/li>\n\n\n\n<li>Create a project directory and initialize it with <code>npm init<\/code>.<\/li>\n\n\n\n<li>Install TypeScript as a development dependency.<\/li>\n\n\n\n<li>Configure TypeScript using <code>tsconfig.json<\/code>.<\/li>\n\n\n\n<li>Write TypeScript code in the <code>src<\/code> directory.<\/li>\n\n\n\n<li>Compile TypeScript to JavaScript using <code>npx tsc<\/code>.<\/li>\n\n\n\n<li>Run the JavaScript output with Node.js.<\/li>\n<\/ol>\n\n\n\n<p>Feature photo by <a href=\"https:\/\/unsplash.com\/@afgprogrammer?utm_content=creditCopyText&amp;utm_medium=referral&amp;utm_source=unsplash\">Mohammad Rahmani<\/a> on <a href=\"https:\/\/unsplash.com\/photos\/black-flat-screen-computer-monitor-8qEB0fTe9Vw?utm_content=creditCopyText&amp;utm_medium=referral&amp;utm_source=unsplash\">Unsplash<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Starting a TypeScript project from scratch involves several steps, including setting up the environment, initializing the project, and configuring TypeScript. Here\u2019s a step-by-step guide to help you get started: 1. Install Node.js and npm Before you start, make sure you have Node.js and npm (Node Package Manager) installed on your system. You can check if [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":198,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[16],"tags":[18,19,17],"class_list":["post-197","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-programming","tag-programming","tag-setup","tag-typescript"],"_links":{"self":[{"href":"\/blog\/wp-json\/wp\/v2\/posts\/197","targetHints":{"allow":["GET"]}}],"collection":[{"href":"\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"\/blog\/wp-json\/wp\/v2\/comments?post=197"}],"version-history":[{"count":1,"href":"\/blog\/wp-json\/wp\/v2\/posts\/197\/revisions"}],"predecessor-version":[{"id":199,"href":"\/blog\/wp-json\/wp\/v2\/posts\/197\/revisions\/199"}],"wp:featuredmedia":[{"embeddable":true,"href":"\/blog\/wp-json\/wp\/v2\/media\/198"}],"wp:attachment":[{"href":"\/blog\/wp-json\/wp\/v2\/media?parent=197"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"\/blog\/wp-json\/wp\/v2\/categories?post=197"},{"taxonomy":"post_tag","embeddable":true,"href":"\/blog\/wp-json\/wp\/v2\/tags?post=197"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}