I'll be discussing the newest JavaScript/Typescript tips which also includes ES2020 additions & Typescript's new type related additions in this post
Optional Function Call
There are often time you wanna callback a function inside a function. The callback function is most of the time optional. So you've to check whether its defined or not to avoid <function name> is not callable kind of errors. This is where optional function call comes to play
_ separator for unreadable numbers
Often times bigger numbers create readability problems. At this situations you can use _ to separate numbers

Use Array.entries to get the index in for_of loop
JavaScript's for_of loop is awesome. Its much readable than ugly forEach higher order function. But many times we need the index of the current element. Which is not provided by default in for_of loop. There Array.entries comes to play. It converts array of elements to array of index, elements
But this readability puts performance at stake. As
Array.entrieshas to take an extra iteration to map out the index. So you can expect it 20-30% slower than regularforEach/forloop. Thanks to @lukeshiru for the correction❤️
[Typescript] template literal types
Its hard to do string validation in JavaScript/Typescript. Checking each type of string combination is hard. In Typescript union | helped but its repetitive. So template literal types were introduced

[Typescript] override keyword
Overriding parent class methods aren't new thing. This is available in all OOP language. But in JS, you can do anything, sometimes unwillingly. But Typescript 4.3 beta introduced override keyword for making method overriding safer. You've to use override keyword before the method name you're willing to override
You've to set noImplicitOverride true in tsconfig.json to make this feature work

+ operator as an alternative to parseInt & parseFloat
Know about parseInt or parseFloat method for parsing numeric string, right?
You can also use the + operator in front of any numeric string to parse it as a number
It will return NaN if the string isn't numeric
Its not recommended to replace
parseIntorparseFloat. As this can cause unknown bugs & behaviors as+is an arithmetic operator too & it converts empty string""to0which is not good. So don't use it always. It also harms the readability. Mentioned it as its a doable thing too.... Again thanks to @lukeshiru for pointing out the problems❤️
[Typescript] Type shadowing⚡💪🏻
May be your function accepts multiple types of arguments & parses/validates them safely & returns different types/shapes of result based of the arguments passed. In this case type shadowing comes handy. You can declare same function multiple times with different sets & types of arguments with desired outcome. Type shadowing works for other types too




Latest comments (24)
I first knew Type Shadowing concept. I didn't found this concept in TS official docs. Is it functions overloads?
For functions yeah, it is known as function overloading too
Vlogmo.com has the cheapest courses
not sure, why i would do this
```const moods = ['good', 'better', 'best']
for ([idx, mood] of moods.entries()) {
console.log(
${index}. ${mood})}```
In my opinion looks a little bit more complicated
Type shadowing is a function overloading
I'm still on the edge of converting to parseInt parseFloat sect. But their behavior to convert '23 somestring' to 23 still off putting to me.
Looks like a possible silent failure for edge cases
Edit: I found an actual case, bennadel.com/blog/3803-i-prefer-th...
I am not saying + is better overall, but it has better behavior in this case
I've updated the post to point out the potential problems that can occur for using
+instead ofparseInt/parseFloat. Also mentioned the performance penalty forfor_ofloop withArray.entriesso that everyone stays informed...Thanks to all of you, who've corrected this out. You guys rock💪
For sure! Not much point in using
for...ofin this use case when aforwill do just fine, but I'd also never do afor...ofwitharr.entries()so there's that too 😂It always bothered me that the override keyword wasn't required, I'm definitely adding this noImplicitOverride: true setting to our project now that I know it exists.
Yeah, that one isn't. But outside arithmetic it's ok.
It just need common sense to know mixing + and arithmetic is bad. Well, lots of people has no common sense, haha.
I agree,
+to convert something to a number is a hack. It also converts empty string to 0, which can lead to bugs. Better to useNumber.parseInt()(or parseFloat) and then do a NaN check.