TypeScript can be thought of as an enhanced version of JavaScript. Its main purpose is to:
- Enable the use of types to enhance code predictability and catch errors
- Translate certain modern JS features, like the
?.
and ??=
operators, into older JavaScript that all browsers can comprehend - similar to what babel does
Contrary to popular belief, TypeScript doesn't introduce any new objects that aren't already present in JavaScript. For instance, if Map
exists in JS, then it also exists in TS; the same goes for Set
. TypeScript's primary aim isn't to add new functionalities but rather to incorporate types in order to reduce coding errors. Following compilation, all TypeScript type annotations are removed since browsers cannot interpret them.
However, TypeScript may include additional code in your project, such as the regenerator runtime which facilitates the usage of modern JS features like async
functions and for...of
loops on browsers lacking native support. This augmentation serves solely to bridge the gap between modern JS and legacy browser compatibility, not to introduce novel capabilities.
In instances where you require a data structure absent in JS, like a LinkedList
or Queue
, you can either craft these structures along with corresponding type definitions yourself or opt to install a package like mnemonist
as highlighted earlier. The latter option, complete with provided type definitions (.d.ts
files) by packages such as mnemonist
, alleviates concerns about typing. If a package lacks inherent type definitions (e.g., express
), users commonly resort to obtaining type definitions from repositories like DefinitelyTyped, such as @types/express
. In cases where no pre-existing type definitions are available for a desired package, creating them manually necessitates extra effort. Fortunately, for your scenario, simply installing mnemonist
suffices without further complications.