TypeScript const assertions vs satisfies operator

I compare as const assertions and satisfies operator in this post.

as const comes with TypeScript 3.4 and generates literal values for more precise and type-safe code.

satisfies comes with TypeScript 4.9 and generates more specific type and validates given object type so that it can catch possible error.

Notice that as const generates a type with readonly fields. So it is an immutable type.

Use case

Notice that satisfies just generates a type from palette object. As you can see below code, red field type is [number, number, number]

As you can see below code, red field type became string instead of [number, number, number]

Use case

satisfies operator catches unlisted field name usage, or wrong field type.

TypeScript const modifier on type parameters

It is a really good feature coming with TypeScript 5.0. We could infer type of object as general as shown in line 8 in below code, so, to infer more-specific type, we had to add as const as shown in line 11.

TypeScript 5.0 makes it easier with adding const in front of type parameter declaration in line 13 in below code.

Use case

Assume that Person type has hobbies , and i just want to infer passed values from hobbies field.

TypeScript Union Type and Distributive Conditional Types Part 2

I show the reason Distributive Conditional Type needs to be used for Union Type in this post.

If generic type T is passed as Union Type like string | number, then array item will be Union Type as below.

If you need to iterate on each type in Union Type, then Distributive Conditional Type (with extends keywod) needs to be used.

To avoid distributive behaviour in Distibutive Conditional Type, surround each side of extends keyword with square brackets.

Continue reading

How to install Python packages with pipenv, venv or virtualenv package manager

You can find GitHub repository in https://github.com/python-projects-kenanhancer/python_demo

  • Pyenv – Python Runtime Version Manager
  • Pipenv – Python Package Manager and Virtual environment manager
  • Venv – Virtual environment manager
  • Virtualenv – Virtual environment manager

Installing Python Runtime Versions via Pyenv

In order to test different Python Runtime versions, I have installed some versions.

$ pyenv install 3.10.2

python-build: use openssl@1.1 from homebrew
python-build: use readline from homebrew
Downloading Python-3.10.2.tar.xz...
-> https://www.python.org/ftp/python/3.10.2/Python-3.10.2.tar.xz
Installing Python-3.10.2...
python-build: use tcl-tk from homebrew
python-build: use readline from homebrew
python-build: use zlib from xcode sdk
Installed Python-3.10.2 to /Users/kenanhancer/.pyenv/versions/3.10.2

I have 4 Python runtime versions(3.10.2, 3.10.3, 3.10.4, 3.10.5) in my machine.

$ pyenv versions

  system
  3.10.2
  3.10.3
  3.10.4
* 3.10.5 (set by /Users/kenanhancer/.pyenv/version)
Continue reading

Docker

Docker is written in the Go programming language.

The Docker daemon

The Docker daemon ( dockerd cli ) listens for Docker API requests and manages Docker objects such as images, containers, networks, and volumes.

A docker daemon can also communicate with other docker daemons to manage Docker services.

The Docker client

The Docker client ( docker cli ) is the primary way to interact with Docker.

When you use commands such as docker run, docker build, docker pull , the Docker client sends these commands to dockerd .

The docker command uses the Docker API.

The Docker client ( docker cli ) can communicate with more than one Docker daemon.

Continue reading

Linux Terminal Check stdin, stdout and stderr

When we want to check whether current terminal is attached to stdin, stdout and stderr streams or not, we can use the below codes. I tried with piping, and redirection so both works well.

As seen in below 6 usages, stdin is not listed in output of commands except first one. Because stdin is used by piping or redirection.

$  ([ -t 0 ] && echo stdin; [ -t 1 ] && echo stdout; [ -t 2 ] && echo stderr; echo hello)

stdin
stdout
stderr
hello
$ echo hello | ([ -t 0 ] && echo stdin; [ -t 1 ] && echo stdout; [ -t 2 ] && echo stderr; cat)

stdout
stderr
hello
$ ([ -t 0 ] && echo stdin; [ -t 1 ] && echo stdout; [ -t 2 ] && echo stderr; cat) <<<"hello"

stdout
stderr
hello
Continue reading

Docker: MySQL, MariaDB, PostgreSQL

docker-compose.yml

version: '3.4'

services:
  mysql:
    image: mysql:latest
    container_name: mysql
    command: --default-authentication-plugin=mysql_native_password
    restart: always
    environment:
      MYSQL_ROOT_PASSWORD: root
      MYSQL_DATABASE: test_db
      MYSQL_USER: test
      MYSQL_PASSWORD: test
    ports:
      - 3306:3306
    volumes:
      - mysql_db_data_container:/var/lib/mysql:rw
      - ${PWD}/docker/mysql/initdb_sql_scripts:/docker-entrypoint-initdb.d/:ro
      - ${PWD}/docker/mysql/mysql.conf:/etc/mysql/conf.d
    healthcheck:
      test: "/usr/bin/mysql --user=root --password=password --execute \"SHOW DATABASES;\""
      interval: 2s
      timeout: 20s
      retries: 10

  mariadb:
    image: mariadb:latest
    container_name: mariadb
    restart: unless-stopped
    environment:
      MYSQL_ROOT_PASSWORD: root
      MYSQL_DATABASE: test_db
      MYSQL_USER: test
      MYSQL_PASSWORD: test
    ports:
      - 3307:3306
    volumes:
      - mariadb_data_container:/var/lib/mysql:rw
      - ${PWD}/docker/mariadb/initdb_sql_scripts:/docker-entrypoint-initdb.d/:ro
      - ${PWD}/docker/mariadb/mariadb.conf:/etc/mysql/conf.d

  postgres:
    image: postgres:latest
    container_name: postgres
    restart: always
    environment:
      POSTGRES_DB: test_db
      POSTGRES_USER: test
      POSTGRES_PASSWORD: test
    ports:
      - 5432:5432
    volumes:
      - postgres_db_data_container:/var/lib/postgresql/data:rw
      - ${PWD}/docker/postgres/initdb_sql_scripts:/docker-entrypoint-initdb.d:ro
      - ${PWD}/docker/postgres/postgres.conf:/etc/postgresql/postgresql.conf

volumes:
  mysql_db_data_container:
  mariadb_data_container:
  postgres_db_data_container:
Continue reading

Azure DevOps REST API

When we need to create, retrieve, update or delete access to the Azure DevOps services's resources, we can use Azure DevOps REST API.

Find more Azure DevOps REST services in https://docs.microsoft.com/en-us/rest/api/azure/devops/?view=azure-devops-rest-7.1

Process REST API

Get Process Id in your organization

Find more details in https://docs.microsoft.com/en-us/rest/api/azure/devops/core/processes/list?view=azure-devops-rest-7.1

ORGANIZATION=$1

PROCESS_NAME=$2

PAT=$3

curl --silent --user :$PAT \
--request GET "https://dev.azure.com/$ORGANIZATION/_apis/process/processes?api-version=6.0" | jq -r '.value[] | select(.name=="'$PROCESS_NAME'") | .id'
$ . ./getProcessId.sh kenanhancer Agile blablabla

adcc42ab-9882-485e-a3ed-7678f01f66bc

Get Process in your organization

Find more details in https://docs.microsoft.com/en-us/rest/api/azure/devops/core/processes/get?view=azure-devops-rest-7.1

echo -n "Organization: " && read ORGANIZATION

echo -n "Process Name: " && read PROCESS_NAME

echo -n "PAT: " && read PAT

PROCESS_ID=$(. ./getProcessId.sh $ORGANIZATION $PROCESS_NAME $PAT)

curl --silent --user :$PAT \
--request GET "https://dev.azure.com/$ORGANIZATION/_apis/process/processes/$PROCESS_ID?api-version=6.0" | jq -r .
$ . ./getProcess.sh
Organization: kenanhancer
Process Name: Agile
PAT: blablabla

{
  "id": "adcc42ab-9882-485e-a3ed-7678f01f66bc",
  "description": "This template is flexible and will work great for most teams using Agile planning methods, including those practicing Scrum.",
  "isDefault": true,
  "_links": {
    "self": {
      "href": "https://dev.azure.com/kenanhancer/_apis/process/processes/adcc42ab-9882-485e-a3ed-7678f01f66bc"
    }
  },
  "type": "system",
  "url": "https://dev.azure.com/kenanhancer/_apis/process/processes/adcc42ab-9882-485e-a3ed-7678f01f66bc",
  "name": "Agile"
}
Continue reading