Skip to content

A guide/cheat repo for TypeScript which includes all basic and common concepts, practices, and examples of TypeScript. Feel free to clone or fork this repo to your organization/account to refer to the guide as and when required during development.

Notifications You must be signed in to change notification settings

premraval-pr/typescript-guide

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

56 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

TypeScript Guide

A repo in progress, soon available with all the basic guidance and contents to code TypeScript. This repo is highly insipired from the Udemy Course Understanding TypeScript - 2021 Edition by Maximilian Schwarzmüller. This Repo is made as my own practice during this course.

Structure

Understanding Compilation

Traditionally, we create a .ts file and then execute tsc filename.js to create a filename.js file which can be used in the script tags or wherever required in the project. But that is so much tedious, hence better solutions are listed below:

  1. If you are dealing with onlt 1 ts file then you can use the --watch option with tsc command to instruct the compiler to have a continuous look at the file and recompile it to .js every time you hit ctrl+s.
tsc filename.ts --watch
or
tsc filename.ts -w
  1. If you are working on a big project and you have multiple files and you need the compiler to have a constant look on all the files, then you need to initialize a TypeScript project in the root folder of your project, it will create a tsconfig.json file which will point to all the .ts files from the root folder to any number of sub-folders thereafter. And then you can run the tsc command without pointing it to any file with --watch option, so on ctrl+s it can recompile all the .ts files to js.
tsc --init // On the root folder to creare tsconfig.json

tsc --watch
or
tsc -w

Understanding tsconfig.json

tsconfig.json is the file created when we initialize a TypeScript project and it can be used to view all the .ts files in the project. But there might be cases where we want to exclude some files and don't want to compile or only include some files. In tscongif.json we can do the following to achieve that.

tsconfig.json
-------------

{
    "compilerOptions": {
        ...
    },
    "exclude": [
        "./data-types",
        ... [any other TypeScript Directories]
        "./node_modules",
        "./using-ts.ts"
    ]
}

This is what is done in this project to make sure that the directories only has .ts files and is not recreating .js every time you edit something. Alternatively we can also do something like:

tsconfig.json
-------------

{
    "compilerOptions": {
        ...
    },
    "include": [
        "app.ts"
    ]
}

which will only recompile app.ts and no other file. Exclude has been done in this project to make a smooth and easy project setup so, if you want to use the project by cloning or even by forking it in your organization for reference on basics, you can easily just code in app.ts and it will only create app.js which is already added in index.html < script > tag and you can go deeper in the directories for conceptual references.

compilerOptions

Though by default the tsconfig.js has a very clean explanation of the compilerOptions commented out for developers but still, the below snippet has some important and common options explained;

tsconfig.js
-----------

{
    "compilerOptions":{
        "target": "es5"             // Can be set to multiple versions of js like 'es6', 'es3', etc. The older the version, the more browser compatibility.
        "lib": [
            "dom",
            "es5",
            "dom.iterable",
            "scripthost"
        ]                           // By default it is commented out, but commented out has defaults added, the basis js functionalities. Although you can add specific libraries as done above. These 4 are the exact libraries that you get if it is commented and added by default.
        "allowJs": true             // Compiles JS files as well
        "checkJS": true             // Doesn't compile but still checks and reports errors in js files (like syntax)
        "jsx": "preserve"           // Could be set to preserve/react/react-native. Is helpful when working with TypeScript in React projects
        "sourceMap": true           // If set to true we get the .ts option in Browser Dev Tools like console and sources and we can debug the code from the browser itself i.e. put breakpoints in .ts files, etc
        "outDir": "./"              // Specifies the location where you want to place the compiled js files. Makes the project organized.
        "rootDir": "./"             // Specifies the root folder for all the .ts files i.e. src (commonly). Makes the project organized.
        "removeComments": true      // Removes the comments after compilation of .ts files and no comments are added in .js files
        "noEmit": true              // If you only want to check the .ts error reports and not generate .js files. Fast for devlopment process.
        "downlevelIteration": true  // Provided iterative support for very old compability browsers/versions, hence if you have loops and are facing some issue after compiling, you may want to think about turning this option on.

        /* IMP and not menioned in the tsconfig.json by default */
        "noEmitOnError": true       // If you set it to true, then it won't create .js files if there are any errors in .ts files, which is super helpful

        /* Strict Type-Checking Options */
        "strict": true              // By default makes all the below options true, so if you need to enable only certain options, make this false.
        "noImplicitsAny": true      // Checks that you have to be clear on the TYPE of a parameters/function etc.
        "strictNullChecks": true    // It checks for strict Nulls, something has to have a value/element. For variables that might be null.
        "strictFunctionTypes": true // It checks the type of functions very strictly. It comes with classes and interfaces
        "strictBindCallApply": true // It checks the method parameters with bind(), call(), and apply() method.
        "alwaysStrict": true        // Makes sure that all the compiled .js files uses ('use strict') mode.

        /* Code Quality Parameters */
        "noUsedLocals": true        // Reports errors on unused Locals
        "noUsedParameteres": true   // Reports errors on unused parameters
        "noImplicitReturns": true   // Reports errors when a function sometimes returns something and sometimes it doesn't return
    }
}
const devQuote = 'Developers are never far from a new "Hello World!"';
const quoteAuthor = "Prem Raval";

About

A guide/cheat repo for TypeScript which includes all basic and common concepts, practices, and examples of TypeScript. Feel free to clone or fork this repo to your organization/account to refer to the guide as and when required during development.

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published