TypeScript Setup

TypeScript Setup

Below are the tools you need to set up with the environment to start writing TypeScript code:

  • Node.js – Node.js is the environment in which you will run the TypeScript compiler.
  • TypeScript compiler – a Node.js module that compiles TypeScript into JavaScript. If you use JavaScript for node.js, you can install the ts-node module. It is a TypeScript execution and REPL for node.js
  • Visual Studio Code – It is a code editor that supports TypeScript. However, you can use your favourite editor.

If you use VS Code, you can install the following extension to speed up the development process:

  • Live Server – It allows you to launch a development local Server with the hot reload feature.

Install Node.js

Installing node.js is very simple. You just need to follow the below steps -

  • Download the latest node.js version from the official website. 
  • Execute the downloaded package and follow the steps.
  • Verify the installation from your terminal. by executing the command node -v. If you see the version you downloaded, it means the installation is successful.

Install TypeScript

Once you install the Node, you can install the typescript compiler using the following command from the command prompt

npm install -g typescript

Install Visual Studio Code

Next, we will need a Code editor. You have many choices here. For this tutorial, we will use Visual Studio Code. You can download it from the official  https://code.visualstudio.com/download

Visual Studio Code

Configure Visual Studio Code (IDE) for JavaScript Development

You can read my previous post to know how you can set up your Visual Studio Code for JavaScript development. It is valid for TypeScript as well.

How to configure Visual Studio Code (IDE) for JavaScript Development

Install Live Server 

To install the Live Server extension, you follow these steps:

  • Click the Extensions tab to find the extensions for VS Code.
  • Type the live server to search for it.
  • Click the install button to install the extension.

Comments

Introduction to TypeScript

Introduction to TypeScript

What is TypeScript

A superset of JavaScript is TypeScript. The foundation for TypeScript is JavaScript. You begin by writing the TypeScript code. The TypeScript code is then converted into standard JavaScript code using a TypeScript compiler.

Once you have the basic JavaScript code, you can use it anywhere JavaScript is supported.

TypeScript files use the .ts extension rather than the .js extension of JavaScript files.

In addition to using JavaScript syntaxes, TypeScript also introduces new syntaxes to support types.

A TypeScript program is one that is written in JavaScript and doesn't have any syntax errors. It means that all JavaScript programs are TypeScript programs. If you're converting an existing JavaScript codebase to TypeScript, this is really beneficial.

Why do we need TypeScript

Below are the main goals of TypeScript:

  • Introduce optional types to JavaScript.
  • Implement planned features of future JavaScript, a.k.a. ECMAScript Next or ES Next to the current JavaScript.
Optional Type System

TypeScript provides the static type system which provides great help in catching programming errors at compile time. Javascript is a dynamically typed system. The variables can hold any values. The type of variable is determined on the fly. The javascript implicitly converts types for example string to a number. This is ok for a small app, but large apps this can be a lot of headaches. It is difficult to test to see if the proper types are passed and errors always happen at runtime.

Intellisense & syntax checking

The static Type system contributes to greater IDE tools support. Among the many advantages of the tools support are intellisense, syntax checking, and code completion. This shortens the development period and guarantees that the programmers make fewer typographical errors. The tooling support for Typescript is present in all of the major editors, including VSCode, Atom, and Sublime Text.

Maintainable code

Modules, Types, Classes, and Interfaces are provided by Typescript. It improves the code's scalability and maintainability. Organizing Typescript code is much simpler than organising Javascript code.

Components Of TypeScript

The architecture of TypeScript is neatly organized in different layers as shown in the image below. The three major layers are

TypeScript Components

The main components are -

  • Language what developer writes.
  • Typescript Standalone Compiler
  • Typescript Language Services

TypeScript Is The Future of JavaScript

The current JavaScript engines are supported by TypeScript for the upcoming features contained in ES Next. You can use the new JavaScript features before other environments or web browsers completely support them, in other words.

Every year, TC39 releases several new features for ECMAScript, which is the standard of JavaScript. The feature proposals typically go through five stages:

  • Stage 0: Strawperson
  • Stage 1: Proposal
  • Stage 2: Draft
  • Stage 3: Candidate
  • Stage 4: Finished

And TypeScript generally supports features that are in the stage 3.

Study Reference

Comments

Understand CSS functional pseudo-class selectors :is() and :where()

Understand CSS functional pseudo-class selectors :is() and :where()

Long selector lists can occasionally result from developing CSS in order to target numerous items with the same style rules. A CSS pseudo-class is a keyword added to a selector that specifies a special state of the selected element(s).

Both :is() and :where() are pseudo-class functions that will help shorten and avoid repetition in creating the selectors. They both take an array of arguments of selectors (ids, classes, tags, etc..) and selects any element that can be selected in that list.

Read this blog post or watch the video below to learn more about the :is() and :where().

Problem Statement

When we write code involving css, there are situations where we ended up writing long selector lits to target multiple elements with the same style requirements. For example, if we want to color adjust any <p> tags inside a heading element, we can write code like below

h1 p, h2 p, h3 p {
  color: green;
}

With the above code, there are couple of disadvantages like -

    • This code will become difficult to read when you have more and more CSS like this.
    • With more and more CSS like this, it will become difficult to debug as well.
    • Finally if you make single mistake while writing the CSS, then rest of the code will be ignored.

Solution With :is() 

The above code can be written is simply way by using :is() pseudo-class function as shown below

:is(h1,h2,h3) p {
  color: red;
}

The above approach is much cleaner and easier to read. At the same time, if we make mistake in one of the tags, it will not ignore the rest of the tags.

Complex Example With :is() 

To start with, let's take the below HTML page.

<HTML>
<body BGCOLOR="FFFFFF">
  <header>
    <h1 class="HelloClass">Technical <span>Potpourri</span></h1>
    <h2 class="ByeClass"><span>Sudipta</span> Deb</h2>
  </header>
  <main>
    <h1 class="HelloClass">Most Popular Browsers</h1>
    <p class="ByeClass">Chrome, Firefox, and Edge are the most used browsers today.</p>
    <article>
      <h2>Google Chrome</h2>
      <p>Google Chrome is a web browser developed by Google, released in 2008. </p>
    </article>
    <article>
      <h2>Mozilla Firefox</h2>
      <p>Mozilla Firefox is an open-source web browser developed by Mozilla. </p>
    </article>
    <article>
      <h2>Microsoft Edge</h2>
      <p>Microsoft Edge is a web browser developed by Microsoft, released in 2015. </p>
    </article>
  </main>
  <footer>
    <p>Author: Sudipta Deb</p>
    <p><a href="mailto:sudipta.deb@gmail.com">sudipta.deb@gmail.com</a></p>
  </footer>
</body>
</HTML>

Example 1

If we want to change the color to green for h1 & h2 under header, h1 under main, h2 under article, we will write code like -

Without :is()
header h1,
header h2,
main h1,
article h2 {
   color: green;
}
With :is()
:is(header, main, article) :is(h1, h2) {
    color: green;
}

Example 2

If we want to change the color to red for span present inside header, we will write code like -

Without :is()
header h1 span,
header h2 span {
   color: red;
}
With :is()
header :is(h1, h2) span {
   color: red;
}

Example 3

If we want to change the color to green for h1 with class HelloClass, h2 with ByeClass, p with ByeClass, we will write code like -

Without :is()
h1.HelloClass,
h2.ByeClass,
p.ByeClass {
color: green;
}
With :is()
:is(h1, h2, p):is(.HelloClass, .ByeClass) {
color: green;
}

The Next Pseudo-Class :where() 

:where() pseudo class selector does exactly the same as :is() with one big difference i.e. the specificity. :where() has zero specificity, but :is() takes the specificity of its most specific selector in the argument list.

:where(header, main) h1 {
 color: green;
}

The above code will make the color green to h1 present under header and main.

Complex Example With Specificity

  • IDs — Specificity score of 100
  • Inline styles — Specificity score of 1000
  • elements & pseudo-classes — Specificity score of 1
  • Classes, pseudo-classes & attributes — Specificity score of 10

Now let's explore the below code with :is() and :where()

header :is(h1, h2) { //Score: 2
 color: green;
}
:where(h1, h2, p):where(.HelloClass, .ByeClass) { //Score: 0
 color: red;
}

Since :where() has zero specificity score, h1 and h2 under header will have the color green. Basically the :where() statement will be ignored, but if we replace :where() with :is() like below -

header :is(h1, h2) { //Score: 2
 color: green;
}
:where(h1, h2, p):is(.HelloClass, .ByeClass) { //Score: 20
 color: red;
}

This time it will take the specificity of two classes so the total specificity score will become 20, which is higher than the score of 2. So finally browser will make the color change to red.

Watch the video below to see the example.

Comments