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

SurveyJS custom survey software

JavaScript UI Libraries for Surveys and Forms

SurveyJS lets you build a JSON-based form management system that integrates with any backend, giving you full control over your data and no user limits. Includes support for custom question types, skip logic, integrated CCS editor, PDF export, real-time analytics & more.

Learn more

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