Static variables and methods in Typescript
· ☕ 2 min read
Typescript allows use of static variables and methods just like modern Javascript. Static variables and methods are declared with a keyword static. Static variables Static variables exist within the class context, and are not carried forward to the object of the class. Define a static variable like so - 1 2 3 class Human { static chaos: boolean = true; } The variable value is accessible using the class.

Type Assertion in Typescript
· ☕ 2 min read
Typescript’s type assertion features help us to override type inference and indicate a specific type to a variable. Type assertion may not be the most common of the features you employ, but it is invaluable when you are migrating from Javascript. Or, when you have to interact with Javascript libraries or data from external systems in your Typescript code.

Conditionally copy props to new object in Javascript
· ☕ 2 min read
Here’s a shorter, readable way to copy props from one object to another subject to defined conditions. The shortest way to copy object (when you don’t quite care about copying sub objects by reference) - 1 2 3 4 5 const planet = { name: "earth", position: 3 }; const newPlanet = { .

Initialize empty object and add props
· ☕ 1 min read
There are situations when you need to initialize object without props. How do you manage that it Typescript? Let’s consider the example below. First, we declare the object and initialize it to an empty object. 1 const planet = {}; Then, we add the props - 1 planet.

A simpler curry for Javascript
· ☕ 1 min read
A simpler way to do curry functions in Javascript. We have previously seen currying in Javascript. A simple form and application of that concept is demonstrated below - 1 2 3 const addThem = add.curry(2); const addTotal = addThem(1); console.log("addTotal: ", addTotal); // 3 Or, we could avoid a external function or library and curry using bindings .

Readonly Properties in Typescript
· ☕ 2 min read
Typescript provides a way to indicate props in an object / type, or an entire type, as read only. Props in an object Consider this example - 1 2 3 4 class Fruit { readonly name = "apple"; color = "red"; } In the code block we have specified the prop name as read-only.

Typeguards in Typescript
· ☕ 3 min read
Typeguards enable us to process function arguments based on their types. Consider this example - 1 2 3 4 5 function join(a: string): string { return a; } console.log(join("hello")); // hello What if we want to use join for strings or numbers? We can do a union type like so -

Mix and match types in a Typescript function
· ☕ 2 min read
Typescript provides a convenient way to accept different types of arguments for a function, or to combine multiple types into one super type. Consider this example - 1 2 3 4 5 function printThis(a: string) { return typeof a; } console.log(printThis("hello")); // string This function is fine if we want to return the input string for some strange reason.

Function overloading for optional arguments in Typescript
· ☕ 2 min read
Overload functions to change signatures and provide optional arguments in Typescript. We have previously seen an option to provide optional arguments to a function in Typescript. But, there is more to that to function overloads. Building on our previous example - 1 2 3 4 5 6 7 8 function getSum(i: number, j?

Duck typing in Typescript
· ☕ 3 min read
Let us discuss everyone’s favourite topic since Typescript was invented - ‘duck typing’. Duck typing is type safety checks for complex types. It gets its name following the adage - If it walks like a duck and quacks like a duck, it must be a duck. Typescript modifies it slightly to -

Allow more or less no. of arguments in Typescript functions
· ☕ 2 min read
Allow additional arguments or optional arguments for function calls in Typescript. Consider the below example of a function that adds two numbers. 1 2 3 4 5 6 function getSum(i: number, j: number): number { return i + j; } console.log(getSum(1, 2)); // 3 This works fine and as expected, but what happens when there is a discrepancy in number of arguments.

Implicit return gotcha for arrow functions
· ☕ 2 min read
Beware of using implicit returns in arrow functions. Consider the below example of a simple arrow function. It returns a simple variable. In the absence of any other statement in the function body, a simple mention of x will return the value of x. 1 2 3 const getStuff = x => x; console.

Public and Private Variables in Typescript
· ☕ 2 min read
Are public and private variables really public or private? Consider the below example - 1 2 3 4 5 6 7 class MathOp { public x: number; private y: number; } const op = new MathOp(); op.x; // no problem If you try to access the private variable.