Join egghead, unlock knowledge.

Want more egghead?

This lesson is for members. Join us? Get access to all 3,000+ tutorials + a community with expert developers around the world.

Unlock This Lesson
1×
Become a member
to unlock all features

Level Up!

Access all courses & lessons on egghead today and lock-in your price for life.

Autoplay

    Use Type Definitions for Popular Projects on npm with @types

    John LindquistJohn Lindquist
    typescriptTypeScript
    ^3.2.1

    Not all projects on npm ship with their own Type definitions. For example, neither lodash or jquery include them. Thankfully, VS Code can automatically find and install type definitions for you or you can simply bring them in with npm i -D @types/lodash from the @types organization on npm.

    Code

    Code

    Become a Member to view code

    You must be a Member to view code

    Access all courses and lessons, track your progress, gain confidence and expertise.

    Become a Member
    and unlock code for this lesson
    Discuss

    Discuss

    Transcript

    Transcript

    Instructor: I'm going to import _from Lodash, and as soon as I use it, I'll type the underscore. Hit save and you'll see Parcel install Lodash for me. Then I'll use .unique to take an array and filter out any duplicates.

    If I just console.log the result with this, then you'll see the result is just one, two, because it filtered out one of the ones. I didn't get any type information from Unique or anything off of Lodash, so not a lot of help there.

    That's because Lodash does not ship with types itself. What you can do is take this suggested action from VS Code. It says you don't have the types here, so install @types/Lodash. If that light bulb doesn't show up, and you see those three dots, you can just hit command-period.

    I'll hit enter here, that will install those types for me, and you'll see them added to your package.json, under the dev dependencies. Now, off of _., I can hit auto-complete and you'll see all the different methods I have.

    If I pick one -- I'll pick camelCase -- and you can see camelCase takes a string. It gives me all that information. I can pick a different one. I'll pick add, and you can see it takes two numbers to add together, and I get tons of information just from installing those types. 

    If your package doesn't ship with types, for example again, jQuery, we can import $ from jQuery, and then I'll use the $, let Parcel install it. This doesn't give me any help here. If I go up to here, hit command-period, say please install the types, now VS Code can give me all this information off of jQuery simply by installing those types.