Three quick ways to convert objects to arrays

Easily convert objects to arrays and iterate to your heart’s content. There are multiple ways of converting an object to an array (which, also is an object but a different one at that.) Method 1: A simple Object.values() method The first idea that comes to mind is to just use Object.keys(), Object.values() or Object.entries() to generate an array. const numO = { a: 1, b: 2, c: 3, d: 4, e: 5 }; console.log(Object.values(numO)); // [ 1, 2, 3, 4, 5 ] Method 2: Apply transformation logic during object to array conversion If you are going fancy about the conversion and want to include some logic therein - ...

A Different IIFE Syntax

Here’s a different syntax for our favourite IIFE’s. Immediately invoked function expressions enable us to write functions without using “functions”. Well - just kidding. They are functions but with a different syntax. A typical IIFE looks like this - const x = 1, y = 9; var sum = 0; (function() { sum = x + y; console.log("sum: ", sum); }(); /* output sum: 10 */ There is another interesting syntax that is lesser known. At least it was not known at all to me-self until I came across the syntax in some library. ...

Access modifiers in Typescript

Typescript supports public, private and protected modifiers that affect the accessibility of class in different ways. Access modifiers further a core concept of object oriented programming - ’encapsulation’. The accessibility of variables and members of a class are driven by modifiers and therefore controlled in the programming flow. Let’s start with a simple class example. class MathOp { public x:number; private y: number; protected z: number; } Public modifier All variables and members are public by default in Typescript. ...

Static variables and methods in Typescript

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 - class Human { static chaos: boolean = true; } The variable value is accessible using the class. console.log(Human.chaos); // true .. but not against the object. const joe = new Human(); console.log(joe.chaos); // error TS2576: Property 'chaos' is a static member of type 'Human' If you need a value in the object no matter the costs, you can always have the two variables with same names. ...

Type Assertion in Typescript

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. Take note of one important thing though - since all Typescript code is compiled to Javascript, any and all assertion is applied at compile time. There is no runtime type assertion. ...

Conditionally copy props to new object in Javascript

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) - const planet = { name: "earth", position: 3 }; const newPlanet = { ...planet }; console.log(newPlanet); // { name: 'earth', position: 3 } But, what do you do if you want to copy only name but not position? A common way to do that will be - const planet = { name: "earth", position: 3 }; const newPlanet = {}; Object.keys(planet).forEach(key => { key == "position" ? "" : (newPlanet[key] = key); }); console.log(newPlanet); // { name: 'name' } Not bad - the code is readable and we have accomplished the task in two lines. ...

Initialize empty object and add props

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. const planet = {}; Then, we add the props - planet.name = "earth"; The above initialization + assignment combination is perfectly valid in Javascript. But Typescript does not agree with planet.name since the object was declared as type {} (an empty object). ...

A simpler curry for Javascript

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 - 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 .. function add(x) { return function(y) { return y + x; }; } const addEm = add(1); console.log(addEm(2)); // 3 But, there is a simpler way to get the same result. We just use arrow functions to collect arguments at different times. ...

Readonly Properties in Typescript

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 - class Fruit { readonly name = "apple"; color = "red"; } In the code block we have specified the prop name as read-only. We can very much change the color of an object derived from Fruit. const apple = new Fruit(); console.log(apple); // Fruit { name: 'apple', color: 'red' } apple.color = "blue"; console.log(apple); // Fruit { name: 'apple', color: 'blue' } But, we cannot change the name - ...

Typeguards in Typescript

Typeguards enable us to process function arguments based on their types. Consider this example - 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 - function join(a: string | number): string | number { return a; } console.log(join("hello")); // hello console.log(join(1)); // 1 But, a real world problem would lie in processing the input arguments - not just returning them. Also, the methods and properties of the argument will change within a function based on their types. Typescript solves this problem using Typeguard. ...