Code Monkey home page Code Monkey logo

typescript-and-nodejs's Introduction

Typescript-and-Nodejs

Setup configurations for nodejs and typescript

  • Initialize npm and add your details

npm init -y
  • Add typescript, ts-node and @types/node definitions to dev dependencies.

$ npm i --save-dev typescript
$ npm i --save-dev ts-node
$ npm i --sav-dev @types/node
  • Add the default TypeScript configuration file

$ npx tsc --init 

Common output directory names include dist, build, prod, and server.

This config file is also where you can tell TypeScript how strict it should be while checking your code and what to ignore. If you're moving a project to TypeScript, you can gracefully integrate TS by working with the settings in this config file.

NPX and Creating your package.json Script

To use TypeScript, you need to add a script to your package.json file to compile TypeScript to JavaScript. This is generally called your "build" script but could be named anything.

  • Add a build script to your package.json file

"scripts": {
    "build": "npx tsc"
  },

To execute your "build" script use the following:

$ npm run build

More TypeScript Configurations

Video Demonstration

Helpful configurations to note:

{
  "compilerOptions": {
    "target": "es5",                          
    "module": "commonjs",                     
    "lib": ["ES2018", "DOM"], 
    "outDir": "./build",                        
    "strict": true,                           
    "noImplicitAny": true,                 
  },
  "exclude": ["node_modules", "./build", "*/__test__/**"]
}

You will see many more options available than what is above. Your application may require additional settings to be configured, but these are typically the main settings to start with.

  • target - sets what version of JS TypeScript will be transpiled to.
  • module - sets what module system will be used when transpiling. Node.js uses the common.js module system by default.
  • lib - is used to say what libraries your code is using. In this case, ES2018 and the DOM API.
  • outDir - where you want your src code to output to. Often named build, prod, or server (when using it serverside).
  • strict - enable strict typing.
  • noImplicitAny - disallow the "any" type (covered in TypeScript Basics).
  • exclude - directories to exclude in compiling.

Further Reading

  • Catch up on ES6 modules if you haven't had the opportunity to work with them yet.
  • Official documentation from Microsoft on installing TypeScript.
  • Explicit instructions from Microsoft on Installing TypeScript and working with NPM and a text editor.

typescript-and-nodejs's People

Contributors

grand-rick avatar

Stargazers

Jeff Snow avatar  avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.