How to use JavaScript libraries in Angular 2+ apps

Mohammad Kermani
Jun 29, 2017 · 3 min read
Image for post
Image for post

1. Create a new project using Angular CLI

If you already don’t have CLI installed on your machine, install it, after installation, create a new project (if you already don’t have one)

ng new learning

2. Install the package into your project

Go to the project we just made:

cd learning
npm install --save underscore

3. Import the library into Angular (TypeScript)

We are writing codes in TypeScript, and we should follow its rules. TypeScript needs to understand underscore.js

npm install --save @types/underscore

4. Import type declaration into Angular app

Let’s say you’re going to use underscore in your app.component.ts file, open the app.component.ts by your IDE and add the following code in top of the file:

import * as _ from 'underscore';/**
* OR simply:
* import 'underscore';
*/

Question: How to use a library which does not have type definition (*.d.ts) in TypeScript and Angular?

Create if the src/typings.d.ts does not exist, otherwise open it, and add your package to it:

declare var yourLibrary: any;
import * as yourPreferedName from 'yourLibrary';
yourPreferedName.method();

Conclusion

As conclusion let’s make a simple example to see a working example of _. Open app.component.ts and inside the appComponent class write a constructor which returns the last item of an array using underscore _.last() function:

...
import * as _ from 'underscore';
...
export class AppComponent {
constructor() {
const myArray: number[] = [9, 1, 5];
const lastItem: number = _.last(myArray); //Using underscore
console.log(lastItem); //5
}
}

HackerNoon.com

#BlackLivesMatter

Sign up for Get Better Tech Emails via HackerNoon.com

By HackerNoon.com

how hackers start their afternoons. the real shit is on hackernoon.com. Take a look

By signing up, you will create a Medium account if you don’t already have one. Review our Privacy Policy for more information about our privacy practices.

Check your inbox
Medium sent you an email at to complete your subscription.

Mohammad Kermani

Written by

✏️ Web, Node, JavaScript, Typescript, and front-end frameworks and libraries. ❤️ Love open-source, PWAs, and the latest web technologies.

HackerNoon.com

Elijah McClain, George Floyd, Eric Garner, Breonna Taylor, Ahmaud Arbery, Michael Brown, Oscar Grant, Atatiana Jefferson, Tamir Rice, Bettie Jones, Botham Jean

Mohammad Kermani

Written by

✏️ Web, Node, JavaScript, Typescript, and front-end frameworks and libraries. ❤️ Love open-source, PWAs, and the latest web technologies.

HackerNoon.com

Elijah McClain, George Floyd, Eric Garner, Breonna Taylor, Ahmaud Arbery, Michael Brown, Oscar Grant, Atatiana Jefferson, Tamir Rice, Bettie Jones, Botham Jean

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch

Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore

Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store