Javascript
Non-number values for enum in Typescript
· ☕ 1 min read
Use non-numbers in Typescript enums. enums have the following typical definition and usage - 1 2 3 4 5 6 7 8 9 enum Colors { red, blue, green } console.log(Colors[1]); // blue Or, the numbers can be explicitly specified.. 1 2 3 4 5 6 7 8 9 10 11 12 enum Colors { red = 1, blue = 2, green = 3 } console.

Decorators in Typescript
· ☕ 2 min read
Decorate a class, member or property to signify its specialty. Decorators are not available in Javascript but enable us to enhance our Typescript libraries and code. Decorators are experimental features in Typescript at this time. A decorator is a way to annotate or modify a class, properties, functions or parameters. Decorator is specified against a target class, and has a body that indicates the decorator’s function.

Type casting in Typescript
· ☕ 5 min read
“Typecast types in Typescript” : that’s three “types” in a sentence with three significant words, and for that, I should tap out (but I don’t). We see a lot about “type assertions” in to type conversations (or conversions!) in Typescript. Type assertions enable you to override default type inference for neutral types.

Typing collections in Typescript
· ☕ 2 min read
How are collections typed in Typescript? Primitives are fairly easy to type - we have seen this while discussing Typescript and its types. 1 2 const num: number = 0; const msg: string = "hello"; We have seen examples of typed arrays in the same post, but how about all the other collections?

Three quick ways to convert objects to arrays
· ☕ 2 min read
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.

A Different IIFE Syntax
· ☕ 1 min read
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 - 1 2 3 4 5 6 7 8 9 10 11 const x = 1, y = 9; var sum = 0; (function() { sum = x + y; console.

Access modifiers in Typescript
· ☕ 2 min read
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.

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.