Skip to content

Aksh-a1/my-typescript-notes

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

19 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Simple code snippets I made while studying typescript.

You'll notice you can find most of them on the typescript website 😛 Then why'd I do this? Well just for fun 🤷‍♂️ 😁


Prerequisites

  1. JavaScript
  2. NodeJS
  3. npm

Setup

src folder contains all the TS files and build folder contains the equivalent JS and declaration(*.d.ts) files.

Steps used to initialise a basic typescript node project :

  1. npm init -y
  2. npm install typescript nodemon @types/node --save-dev
  3. npx tsc --init --rootDir src --outDir build --lib es6,dom --module commonjs --allowJs true --esModuleInterop --resolveJsonModule --noImplicitAny true

Step 3 is totally your choice which options you choose to enable and what values you prefer.

Option Description
--rootDir Folder where we put our Typescript code /src. Only use to control the output directory structure with --outDir.
--outDir Typescript to JavaScript compiled code goes here.
--module Specify module system for JavaScript files used in project.
--esModuleInterop It instructs TypeScript to allow us to use an import like this import myModule from '../myModule' instead of import myModule = require('../myModule') even if your module system defines CommonJS. source ↗️
--lib If your target is es5 but you will be using es6 or esnext features then you can specify it in this option. source ↗️
--allowJs If you have JavaScript in your project, should it be compiled with Typescript.
--resolveJsonModule Include modules imported with .json extension.
--noImplicitAny Raise error on expressions and declarations with an implied any type.

Sources: Typescript Handbook | Understanding TypeScript Configuration Options

  1. Create a nodemon.json file as following (So that you don't have to compile again and again after making changes, nodemon takes care of that.) :
  {
    "watch": ["src"],
    "ext": ".ts,.js",
    "ignore": [],
    "exec": "npx tsc"
  }
  1. Change package.json as following :
  "scripts": {
    "start": "nodemon",
    "test": "echo \"Error: no test specified\" && exit 1"
  }
  1. npm start. Your all the .ts files get compiled to .js in build folder and then you can run them using node ./build/<your_compiled_file.js>

References:

  1. https://www.typescriptlang.org/docs/handbook/intro.html
  2. https://khalilstemmler.com/blogs/typescript/node-starter-project/
  3. https://medium.com/javascript-in-plain-english/typescript-configuration-options-tsconfig-json-561d4a2ad4b

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published