Skip to main content

Quick tip about TypeScript and why to avoid these types

· One min read
Benjamin Auzanneau

Avoid the non-primitive Number, String, Boolean, Object, and Symbol types in TypeScript.

All of them refer to non-primitive reference types.

Instead, prefer the corresponding primitive types.

let textToCheck: String = "text";
console.log(typeof textToCheck); // 'string'
console.log(textToCheck === "text"); // true

textToCheck = new String("text");
console.log(typeof textToCheck); // 'object'
console.log(textToCheck === "text"); // false

It's easy to failed your equality check with them.

There is also a performance part that I will not detail here. I recommend this article from mozilla which makes a performance comparison between a literal string and its object version in JavaScript.

That's it, make good use of it !


I'm not a native English speaker so, thanks in advance if you want to improve my article with correct syntax/grammar/sentences.

I can accept all kind remarks :)

Cover by JC Dela Cuesta on Unsplash