Handy Shortcuts for working in Typescript and Javascript

Mika Heares
3 min readOct 5, 2020

Sometimes it's just a little bit too hard to write that extra line. In this article, I describe some ways I avoid excessive code.

Photo by Stefan Steinbauer on Unsplash

Class Constructors with too many parameters

I had an issue defining class constructors in typescript. Some of them needed so many options that I couldn't leave out. Writing new Models to behave as the argument kind of worked but would still be far more verbose than I think it needed to be.

I had used some libraries that handled this really well so I peeked under the covers to understand how to use them for myself.

Constructor Styles for Optional Parameters

I found that while being a little more verbose, it allows you to skip the nulls or fields you don't want to initialise.

In this case, I have a class with three properties initiated in the constructor. A and C need to be set in this example but B can be ignored.

In the original example, B must be passed a null to be able to initiate C, making the parameters harder to follow.

I'm in the process of using this more often in my application as it is much easier to understand and read. The only issue I have found is maintaining backwards compatibility with the original Long Constructor style is very messy.

String Templating to display logs or other kinds of text

To Reduce writing extra code, the shortest way I found was to use `` to declare strings within Javascript and Typescript.

String Template Demo from my Game

When in the string using ${} To Declare the variable. This keeps the variable being used within the code that is being used.

It can also be used multiline so putting together HTML is super simple within the string.

Map, Some, Every, and filter methods on arrays to manage data

I have a C# background and use LINQ all the time. these methods work similarly to Select, Any, All, and Where methods.

filteris used to remove items that fail the check and filter them out. It is useful if you need to get a subset of items from an array. It does not remove the items from the original array.

mapis used to modify each and every time in the array in place. handy if you need to manipulate items in an array. It doesn't modify items from the original array.

somechecks that any of the checks return true on an array.

everychecks that all the items in an array pass a check.

These methods are handy to perform operations on arrays that are clear and easy to understand. When used it makes it easy to work with arrays and avoids having to use long for loops.

I use most of these language features and packages every time I work on a javascript project. It keeps the code very short and concise, preventing excess code from appearing to be something more than it's not. Using these patterns also makes sure to reduce errors that could come from writing the extended patterns and leaving in a silly mistake.

--

--

Mika Heares

I write about C# and Full Stack Development. I try to uncover weird complexities about the languages I work with.