Tuples in Typescript

Gagandeep Singh
Tuples is a type that exists in Typescript which is similar to Array type but has fixed length and defined what kind of element (either same or different types) will be there at a certain position. // Array example const a: number[] = [1,2,3]; // Tuple example const t: [string, number, boolean] = ['2', 1, true]; Since Tuples are just arrays, so it will allow to push a new element only of type either string or number as defined in the tupleType below.

Using pre-commit hook in package.json

Gagandeep Singh
One of the traits of someone who follows or moving towards a good coding practice environment is making sure that you are not pushing the code which may have linting issues and/or failing unit tests. Adding a pre-commit hook forces to check the lint and run unit tests (or any other script) to be sure that everything is good before committing changes. npm i --save-dev pre-commit Next step is to update the package.

undefined and null in Javascript

Gagandeep Singh
undefined In Javascript undefined (a primitive type in Javascript) gets assigned to a variable that we declare but don’t assign a value to. It’s Javascript’s way of telling that the value of this variable is not defined. Even a function in Javascript which does not return anything, will return undefined by default. So, to avoid any error we should never assign undefined to a variable manually. typeof undefined outputs: undefined null Unlike undefined, null (another primitive type in Javascript) is used when we want to manually or intentionally set the value of a variable to be empty.

Docker - useful commands

Gagandeep Singh
To run a container using an image docker run <image name> example: docker run hello-world Override the default command for a container docker run <image name> command example: docker run busybox ls Show running containers docker ps docker ps --all This will list all the containers that were ever created on your machine. Restart stopped containers docker start <container id> example: run docker ps –all to get the list of containers

Using useState hook with the previous state

Gagandeep Singh
Let’s take a very basic use case of the useState() hook; updating the count variable on each click. The code look perfectly fine and will work as expected in almost every time. BUT the issue with this code is that setCount() doesn’t guarantee that the previous count that it’s going to use to either increment and decrement to get the next state will be the latest one. To make sure that we always use the latest previous state for calculating the new state, we need to pass a callback function in the setCount rather than directly doing computation inside it.