Use Type Definitions for Popular Projects on npm with @types

John Lindquist
InstructorJohn Lindquist
Share this video with your friends

Social Share Links

Send Tweet

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.

Instructor: [00:00] 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.

[00:18] 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.

[00:35] 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.

[00:53] 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.

[01:07] 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. 

[01:26] 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.