DEV Community

Cover image for Quick tip about TypeScript and why to avoid these types
Benjamin🦸‍♂️Auzanneau™
Benjamin🦸‍♂️Auzanneau™

Posted on • Edited on

2 2

Quick tip about TypeScript and why to avoid these types

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
Enter fullscreen mode Exit fullscreen mode

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

Do your career a big favor. Join DEV. (The website you're on right now)

It takes one minute, it's free, and is worth it for your career.

Get started

Community matters

Top comments (0)

A Workflow Copilot. Tailored to You.

Pieces.app image

Our desktop app, with its intelligent copilot, streamlines coding by generating snippets, extracting code from screenshots, and accelerating problem-solving.

Read the docs

👋 Kindness is contagious

Immerse yourself in a wealth of knowledge with this piece, supported by the inclusive DEV Community—every developer, no matter where they are in their journey, is invited to contribute to our collective wisdom.

A simple “thank you” goes a long way—express your gratitude below in the comments!

Gathering insights enriches our journey on DEV and fortifies our community ties. Did you find this article valuable? Taking a moment to thank the author can have a significant impact.

Okay