Tuesday, February 1, 2022

An Introduction to TypeScript

An Introduction to TypeScript

The front-end React developer world is all abuzz with the fondness of using and preferring TypeScript over JavaScript. Although it’s not recommended for all types of projects it strongly overcomes many shortcomings of JavaScript and improves over it. 


In this beginner-friendly article, we will get to know what exactly TypeScript is, how is it a strongly-typed language, how it compares to JavaScript along with some of its highlighting features. Of course, we will be writing our first .ts code too!

What is TypeScript?

TypeScript is a strongly typed programming language that builds on JavaScript giving you better tooling at any scale. It’s a free and open-sourced project created by Microsoft.


It is a ‘superset of JavaScript’, which means that you can continue to use the JavaScript skills you’ve already developed and add certain features that were previously unavailable to you. When compared to JavaScript, it’s a strongly typed language as opposed to JS which is a loosely typed language. You can consider this like JavaScript with superpowers!


Now here’s where this language actually shines…remember the term ‘strongly typed’ we used above? What does it mean in this context? Well, this means that the data types of variables/functions and other primitives must be pre-defined. This is one of the most important features of TypeScript (that’s why it focuses so much on the ‘type’).


Under the hood, it compiles to JavaScript, giving you the benefit of the JavaScript platform plus the intended advantages of types. 

Top features of TypeScript

Now that you know a bit about this language, it’s time to see all the important and useful features it provides to the developer. Here are a few of them:


1. JavaScript and more: TypeScript adds additional syntactic sugar to your JavaScript code to support a tighter integration with your editor. 


2. Runs anywhere where JavaScript does: TypeScript code converts to JavaScript which can then be run in a browser, on Node.js or Deno, and in your apps.


3. Safety with scalability: it uses type inference to give you great tooling without writing any additional code.


4. Editor support: most of the modern IDEs and code editors like VS Code come with built-in support for TypeScript files. You get autocompletion and auto-import support in VS Code out of the box.


5. Unique language features: here are some of the features which you will only find in a TypeScript code; Interfaces, Namespaces, Generics, Abstract classes, Data modifiers, and more!


6. Gradual adoption rate: you can apply the types to any previous JavaScript projects or codebase incrementally. With great editor support, TypeScript catches errors right inside your editor!


7. Easy to describe the data: it’s really easy to describe the shape of objects and functions in your code. This makes it possible to see documentation and issues in your editor.

All of this should give you a general idea of what TypeScript is and what are its features, it’s time to write our first TypeScript code and see how to use it with JavaScript gradually.

From JavaScript to TypeScript

We won’t be diving straight into a TypeScript code. Instead, we want you to get familiar with an already existing knowledge of JavaScript and use it to convert a small JS code to TS code. 


Let’s say we have the following JavaScript code: 

// @ts-check
function compact (arr) {
  if (orr. length > 10)
    return arr. trim(0, 10)
  return arr
}

Now you will see an error like “Cannot find name ‘orr‘.” Next, let’s say we do another mistake like using

trim instead of slice on an array:

function compact (arr: string[]) {
  if (arr.length > 10)
    return arr.slice(0, 10)
  return arr
}

We add a type of string[] (String array) for the arr parameter so it should always accept a string-based array and nothing else. Hence, we call TypeScript ‘strongly-typed’.

Install and Setup TypeScript

Time to write some TS code locally on our machine! You can install TypeScript globally via the following NPM command:

npm install -g typescript

Next, you can confirm the installation by running tsc –v to check the version of TypeScript installed in your system.
Note that after you write a TypeScript code and want to run it, simply running tsc with file, the name won’t work as tsc is just a TypeScript compiler. We need Node.js to get the actual log output. We can do it by running this command for a “Hello World” program:  

tsc hello.ts && node hello.js 

Your first“Hello World!” in TypeScript
After you installed TypeScript globally on your machine. You can open a suitable code editor like VS Code which has excellent support for the TypeScript tooling.

  1. Create a new TypeScript file called helloWorld.ts. Then simply write a console log statement as you would do in JavaScript:
console.log("Hello World!");

2. Open your command prompt or Terminal window and run tsc helloWorld.ts. You will see nothing will happen as there are no types assigned here hence no type errors.

3. Instead you will see the TypeScript compiler generates a new helloWorld.js file in the same directory. This is the same TS code but it is the generated JS file output.

4. Time to make our console statement better. Let’s say we want to log the person’s name and date by asking the user to enter it through a greeter function:

 function greet(person, date) {
  console.log(`Hello ${person}, today is ${date}!`);
}

greet('Brendan');

Notice the way we are calling the greet function. If you run this you will get this error because we passed only 1 argument instead of the expected 2:

// TS ERROR: Expected 2 arguments, but got 1.

The parameters to the greet() function don’t have any explicitly defined types so TS will give it any type. 

  1. Let’s fix our function with the following valid code:
// "greet() takes a person of type string, and a date of type Date"
function greet(person: string, date: Date) {
  console.log(`Hello ${person}, today is ${date.toDateString()}`);
}

greet('Maddison', new Date());

Now we have explicitly defined all the data types and we can happily see the log statement printing the exact output we need. 


Just in case you are wondering the equivalent JS code of this will be:

// "greet() takes a person of type string, and a date of type Date"
function greet(person, date) {
    console.log("Hello " + person + ", today is " + date.toDateString() + "!");
}
greet('Maddison', new Date());

With that, we have covered the bare-minimum basics you need to know of the TypeScript language. As you saw, it’s very much close to JavaScript so if you were already working with JavaScript then it should be easy to learn and migrate your projects to TypeScript. To make your work easy, we’ve created some dashboard templates. Check out now!

Wednesday, January 19, 2022

Top 10 IDEs for React.js Developers in 2021

Are you a React developer feeling frustrated by using that same old code editor every day and now want to explore some new and unheard editors? 
Using a code editor or an IDE that has a sufficient amount of features you need and that fits perfectly into your own workflow is important for the entire work. In this article, we have compiled the top 10 IDEs and editors on which you can get your hands as a React developer with ample support for the JavaScript ecosystem and the features they provide.
 Enjoy the read!

1. Visual Studio Code (VS Code)

What is it?

  • Visual Studio Code(VS Code) is a free source-code editor made by Microsoft for Windows, Linux and macOS.

It has integrated Git version control and Terminal. It has a very large plugin ecosystem where you can find thousands of helper tools that work best for your tech stack and project.
Chances are you are already using and loving this editor. In the Stack Overflow 2021 Developer Survey, VS Code was ranked the most popular developer environment tool.

Top features:

  1. IntelliSense: it provides you with a better and smart code completions based on variable types, function definitions, and imported modules.
  1. Debugging: you can directly launch the debugger with break points, call stacks, and more without ever leaving the editor.
  1. Git integration: you can easily review diffs, stage files, and make commits right from the editor. 
  1. Extensible and customizable: with its extensions gallery you can add new languages, themes, debuggers, and connect to additional services. 

2. WebStorm

What is it?

  • WebStorm is a full-blown IDE made by JetBrains for web, JavaScript and TypeScript development.

With WebStorm, you can expect everything and more of what an IDE should provide to a React developer. It runs dozens of code inspections as you type your code and detects potential problems in it.
It has smart code completion, on-the-fly error detection, powerful navigation, and refactoring. This comes with built-in support for all web-related technologies like JavaScript, TypeScript, React, Vue, Angular, Node.js, HTML, or style sheets, etc.

Top features:

  1. Smart code refactoring: it autocompletes your code, detects and suggests fixes for errors and redundancies.
  1. Powerful dev tools: it comes with all the linters, build tools, terminal, and HTTP client to test and debug your web applications.
  1. Code navigation: in just one place you can look for files, classes, or symbols, and review all the code  matches.
  1. Collaboration support: you can easily onboard you team members and other developers. WebStorm supports real-time code collaboration with sharing code styles, settings and even joining on a call!

3. Atom

What is it?

  • Atom is a free and open-source ‘hackable’ code editor for customising almost anything without touching its config file. It was made by GitHub.

It has a highly customizable environment and ease of installation. So if you are someone who wants to quickly set up a new React project without worrying about multiple steps of installation etc, then Atom may be a good choice.

Top features:

  1. Teletype: this is one of the highlight features of Atom as it allows you to share your entire workspace and edit code together in real time.
  1. Full GitHub support: as it’s already bundled so you get to create new branches, stage and commit, push and pull, resolve merge conflicts, view pull requests etc right out of the box!
  1. Built-in package manager: whether it’s about searching for your favourite package for that code library or if you want to be a pro by making your own, Atom has it all!
  1. File system browser: with this, it becomes easy to open your main file while browsing all of the existing ones from a single window.

4. Sublime Text

What is it?

  • Sublime Text is a popular commercial code editor which natively supports many programming languages.

No code editor talk can be finished without the mention of Sublime Text. It’s one of the most used editors in the world thanks to its slick interface, amazing features, and top-notch performance.
All the projects in Sublime Text capture the full contents of the workspace, including modified and unsaved files. 

Top features:

  1. Split panes and navigation: use a simple modifier when performing actions thatwill split the interface to show multiple tabs at once. 
  1. Code definitions: it comes with features like Goto DefinitionGoto Reference and Goto Symbol by which you can explore the full definition in a small popup.
  1. Multiple selections: use keyboard shortcuts like ⌘+D to select the next occurrence of the current word, ⌘+K, ⌘+D to skip an occurence.
  1. React/JS file support: TypeScript support comes by default with syntax-based features for all React and JS/JSX files.

5. Reactide

What is it?

  • Reactide(or React-IDE) is the first dedicated IDE for React web application development.

It’s a cross-platform desktop application that offers a custom simulator, making build-tool and server configuration unnecessary. Now you can simply reply on a single window for all of your code, browser preview, and more.
If you get carried out while writing the React JSX code along with multiple browser windows then Reactide is here to help. It combines everything in one single place so that all the focus is on writing and reviewing the code.

Top features:

  1. Intuitive interface: this is probably one of the biggest strength of Reactide when compared with others. It runs an integrated Node server and custom browser simulator and you can continually track changes through live reloading directly in the development environment.
  1. State flow visualization: it comes with a visual component tree that dynamically loads and changes based on components within the working directory while giving information about props and state at every component.
  1. Integrated Terminal: the built-in Terminal can be used for running commands in bin/bash for Unix, and cmd for Windows.
  1. Streamlined configurations: to start, just input your .js and .html entry points inside Reactide’s universal configuration and then run npm run reactide-server to kick off your project.

6. Emacs

What is it?

One of the very highly adopted editors in the GNU world, Emacs has an interpreter for Emacs Lisp, a dialect of the Lisp programming language with extensions to support text editing.
It supports a plethora of programming languages and other faculties of text editing. This also comes with a good and robust set of extensions and other features like Git integration, syntax highlighting, etc.

Top features:

  1. Content-aware editing modes: this includes syntax coloring, for many file types.
  1. More than code editing: you can use the project planner, mail and news reader, debugger interface, calendar, IRC client, and more.
  1. Extensive extension support: comes with a packaging system for downloading and installing extensions.

7. Rekit Studio

What is it?

  • Rekit is a toolkit for building scalable web applications with React, Redux and React-router. It’s an all-in-one solution for creating modern React apps.

It provides you with the capability for code generation, dependency diagraming, refactoring, building, unit tests, and a meaningful way to navigate code. 
Rekit creates applications bootstrapped by the Create React App tool and has the capability to scale, test, and maintain easily.

Top features:

  1. It helps you focus on business logic rather than dealing with massive libraries, patterns, configurations etc.
  1. Comes with powerful tooks like Rekit Studio which is the real IDE for React/Redux development and command line tools to create/rename/move/delete project elements like components, actions etc. 
  1. Rekit can do code generation, dependency diagraming, refactoring, building, unit tests, and a meaningful way to navigate code.
  1. It’s highly capable of recognising which files are components, which are actions, where routing rules are defined, and so on.

8. Vim

What is it?

  • Vim is a highly configurable text editor built to make creating and changing any kind of text very efficient. 

It’s a free and open-source, screen-based text editor program for Unix. The good part is Vim is designed for use both from a command-line interface and as a standalone application in a graphical user interface. 
Vim has 12 different editing modes, 6 of which are variants of the 6 basic modes. Some of the common ones are Normal, Visual, Insert, Cmdlibe, etc.

Top features:

  1. Key mappings: you can execute complex commands with “key mappings,” which can be customized and extended. 
  1. Recording: this allows for the creation of macros to automate sequences of keystrokes and call internal or user-defined functions and mappings. 
  1. Extensive: it comes with a persistent, multi-level undo tree along with an extensive plugin system.
  1. Support: Vim supports hundreds of programming languages and file formats. It can also be integrated into various other tools easily.

9. NetBeans

What is it?

  • Apache NetBeans is a development environment, tooling platform and an application framework.

NetBeans IDE allows applications to be developed from a set of modular software components called modules. It was originally used for making Java applications but now has extensive support for all major tools and technologies including PHP, C, C++, HTML5 and JavaScript.
The IDE provides editors, wizards, and templates to help you create applications in Java, PHP, and many other languages.

Top features:

  1. Fast and smart editing: it highlights source code both syntactically and semantically, lets you easily refactor code, with a range of handy and powerful tools.
  1. CSS editor: this comes with code completion for styles names, quick navigation through the navigator panel, displaying the CSS rule declaration in a List View and file structure in a Tree View.
  1. Modular: each module provides a well-defined function, such as support for editing, or support for the CVS versioning system.
  1. JavaScript editor:it has syntax highlighting, refactoring, code completion for native objects and functions, generation of JavaScript class skeletons, generation of Ajax callbacks from a template.

10. Notepad++

What is it?

  • Notepad++ is a free source code editor and Notepad replacement that supports several languages.

It’s written in C++ and uses pure Win32 API and STL which ensures a higher execution speed and smaller program size. It supports tabbed editing, which allows working with multiple open files in a single window. 
It features all the common editing tools like syntax highlighting, code folding and limited autocompletion for programming, scripting, and markup languages, but not intelligent code completion or syntax checking. 

Top features:

  1. Collaborative editing:this allows multiple developers to work on the same file simultaneously while on different computers.
  1. Selection methods: it has support for various methods for text selection like block selection, column selection , and non-linear selection.
  1. Macros: for recording a sequence of editing commands to be executed repeatedly. 
  1. Other notable features include; advanced find and replace, split-screen editing/viewing, support for bookmarks and a plugin system.

We hope you liked this set of IDEs and editors for React development. Let us know which one are you currently using and which one you will use after reading this article. Happy coding!