TypeScript Syntax

Type Aliases, Union Types, Literal Types, Index Signature

You can find different usages of Type Aliases in the following demo code.

Type Aliases is defined with type word.

Union Types is defined with the | character to separate the different possible types.

Literal Types can be string, number or boolean

type SelectedEvent = "Click"; // string literal type

const buttonEvent: SelectedEvent = "Click";
type Color = "Red" | "Green" | "Blue"; // string literal type with union type

const buttonForeColor: Color = "Red";
type num = 1 | 3 | 5 | 7 | 9; // number literal type with union type
type bool = true | false; // boolean literal type with union type

type TRUE = true;

type FALSE = false;

Index Signature is defined as below

type Dictionary = {
    [index: string]: any;
};

const person: Dictionary = {
    "firstName": "Kenan",
    "lastName": "Hancer",
    "age": 36
};
interface StringArray {
    [index: number]: string;
}

const names: StringArray = ["Bob", "Fred"];

let name1: string = names[0];

Type Aliases can be used with Union Types and Literal Types as below

type obj = {success: true} | {success: false}; // object
type Result<T> = { success: true, value: T } | { success: false, error: string | number }; // object
type PersonCommonFields = { firstName: string, lastName: string };
type Person = PersonCommonFields & { isDeleted: true | false };
type Name = string; // simple type
type NameResolver = () => string; // function
type NameOrResolver = Name | NameResolver;

Demo1

Continue reading

JavaScript Object.assign(), Object.create()

Object.create()

The Object.create() method creates a new object, using an existing object

Demo1

Continue reading

JavaScript Object.entries(), Object.values(), Map, Set Usage

Demo1

Continue reading

Node.js Axios Usage

How to install Java with Jabba

Runtime version managers make developers life easier. So, I tested different Java version managers. But, Jabba is the easiest one for me.

You can read previously posted How to install java articles.

Installing Jabba

Click Jabba link to see more detailed information about Jabba

$ curl -sL https://github.com/shyiko/jabba/raw/master/install.sh | bash && . ~/.jabba/jabba.sh

Make sure to source jabba.sh in your environment if you skip it:

$ [ -s "$JABBA_HOME/jabba.sh" ] && source "$JABBA_HOME/jabba.sh"

If you don't have JAVA_HOME environment variable, run the following code:

$ echo 'export JAVA_HOME=$HOME/.jabba/jdk/openjdk@1.14.0-1/Contents/Home' >> ~/.bash_profile
$ echo 'export JAVA_HOME=$HOME/.jabba/jdk/openjdk@1.14.0-1/Contents/Home' >> ~/.bashrc
$ echo 'export JAVA_HOME=$HOME/.jabba/jdk/openjdk@1.14.0-1/Contents/Home' >> ~/.zshrc
Continue reading

How to install Java with brew and jenv

jenv is a tool that helps you switch between different java versions in your development machine. But, Jenv doesn't install Java for you.

Use your platform appropriate package manager to install Java. On macOS, brew is recommended.

Install Java with Homebrew

First, ensure that you have Homebrew installed in your system. Homebrew is a package manager for Mac OS.

I also posted how to install Java JDK manually in the following post.

Actually I already mentioned how to install Java JDK in the above post, but I will shortly show one more time

$ brew tap adoptopenjdk/openjdk
$ brew search jdk

==> Formulae
openjdk                                                                              openjdk@11

==> Casks
adoptopenjdk
adoptopenjdk12
adoptopenjdk13-openj9
adoptopenjdk14-openj9-jre-large
adoptopenjdk9
adoptopenjdk10
adoptopenjdk12-jre
adoptopenjdk13-openj9-jre
adoptopenjdk14-openj9-large
jdk-mission-control
adoptopenjdk11
adoptopenjdk12-openj9
adoptopenjdk13-openj9-jre-large
adoptopenjdk8
oracle-jdk
adoptopenjdk11-jre
adoptopenjdk12-openj9-jre
adoptopenjdk13-openj9-large
adoptopenjdk8-jre
oracle-jdk-javadoc
adoptopenjdk11-openj9
adoptopenjdk12-openj9-jre-large
adoptopenjdk14
adoptopenjdk8-openj9
sapmachine-jdk
adoptopenjdk11-openj9-jre
adoptopenjdk12-openj9-large
adoptopenjdk14-jre
adoptopenjdk8-openj9-jre
adoptopenjdk11-openj9-jre-large
adoptopenjdk13
adoptopenjdk14-openj9
adoptopenjdk8-openj9-jre-large
adoptopenjdk11-openj9-large
adoptopenjdk13-jre
adoptopenjdk14-openj9-jre
adoptopenjdk8-openj9-large
$ brew cask install adoptopenjdk
$ java --version

openjdk 14.0.1 2020-04-14
OpenJDK Runtime Environment AdoptOpenJDK (build 14.0.1+7)
OpenJDK 64-Bit Server VM AdoptOpenJDK (build 14.0.1+7, mixed mode, sharing)
Continue reading

How to install Ansible with brew or pip

You can install Ansible quickly in your system with Homebrew( or PIP(Package manager for Python)

Install Ansible with Homebrew

If you are using macOS, then you can use Homebrew.

First, ensure that you have Homebrew installed in your system. Homebrew is a package manager for Mac OS.

$ brew update
$ brew install ansible

Checking Ansible version

$ ansible --version
ansible 2.9.7
  config file = None
  configured module search path = ['/Users/kenanhancer/.ansible/plugins/modules', '/usr/share/ansible/plugins/modules']
  ansible python module location = /Users/kenanhancer/.pyenv/versions/3.8.2/lib/python3.8/site-packages/ansible
  executable location = /Users/kenanhancer/.pyenv/versions/3.8.2/bin/ansible
  python version = 3.8.2 (default, May  6 2020, 12:47:50) [Clang 11.0.3 (clang-1103.0.32.59)]

Install Ansible with PIP

Ensure that you have Python and PIP in your system. Check the following page about Python installation.

$ pip install ansible
$ ansible --version

Updating Ansible

$ pip install --upgrade ansible

Installing specific Ansible version

$ pip install ansible==2.2.2.0
$ pip install ansible==2.0.1.0

How to install Python with pyenv version manager

I am using different programming languages like C#, Java, Node.js etc.

So, I definitely need to switch between runtime environments.

For example, there are some package managers in Node.js community like nvm or n. I am very happy to use both of two package managers 🙂

Let's say I work for one Node.js project in dev machine with Node v8.0.0 but other project needs Node v6.0.0

In order to handle, different versions in same machine, we may use package managers.

I just want to find a way to switch between different Python version so that found pyenv package manager.

There are also others like asdf, gvmnvmrbenv package managers.

Pyenv installation

Click pyenv link for more detailed installation.

I just want to show Homebrew(click for installation). Homebrew is a Package Manager for macOS

Installing pyenv with Homebrew

Run the following commands in your terminal. But, just copy commands without $ sign.

$ brew update
$ brew install pyenv
$ echo -e 'if command -v pyenv 1>/dev/null 2>&1; then\n  eval "$(pyenv init -)"\nfi' >> ~/.bash_profile

and restart your terminal.

Upgrading pyenv

$ brew upgrade pyenv

Uninstall pyenv

$ brew uninstall pyenv

Pyenv commands

List all Python versions installed in system

$ pyenv versions
  system
  3.7.3
* 3.8.2 (set by /Users/kenanhancer/.pyenv/version)

Show current active Python version in system

$ pyenv version
3.8.2 (set by /Users/kenanhancer/.pyenv/version)
Continue reading