What is "DefinitelyTyped"?
I see "DefinitelyTyped" is something related to TypeScript/JavaScript development, but I couldn't understand its use or some code sample to understand about it.
I looked at http://definitelytyped.org/ and http://bartvds.github.io/projects/dt.html but couldn't understand it better.
Any pointers, article, Code sample is much appreciated.
Solution 1:
TypeScript allows you to have Declaration Files which are files that allow you to describe the shape of code that's written in (for example) plain JavaScript. So, by referencing one of these files you tell TypeScript exactly how that JavaScript code or library you are using should be assumed to be "typed". Of course, this means that the declaration file need to be carefully written and in sync with the JavaScript library you are using.
DefinitelyTyped is the most popular repository of Declaration Files for many many JavaScript libraries most of which do not provide their own declaration files (as they are not developed with TypeScript and are not committed to work with it). So it holds Declaration files maintained by the community.
By using DefinitelyTyped and the declaration files it contains you can use most of the popular JavaScript libraries as if they were TypeScript libraries in the sense that you will have type validation by the compiler (as the declaration file indicates). Also, being so popular, DefinitelyTyped will be curated by the community to contain valid declaration files (although, web development being something that moves really fast, you may end up finding a couple of issues specially in obscure libraries).
Solution 2:
The description above is quite clear, however, if you are like me, maybe the code below may help u get the big picture.
For example, you have lodash
package in your project, you import and use a method of it.
import random from 'lodash/random';
const result = random(????);
You may stop and wonder how many parameters this method may require? and what kind of data for each parameter? You have to search for the lodash
homepage, find the API documentation of random
to know how to use it. After doing a bunch of tasks, you may end up providing wrong the order of parameters, but nothing will show until you run your app and get the error.
This is where DefinitelyType
shows its power. After installing the DefinitelyType of lodash
yarn add @types/lodash
- when you start typing the
random
method, a tooltip will show and guide you how to use the parameters. - when you provide wrong data type of parameters, it will show the error in order to let you know and fix it immediately.
Does it make sense? if not, don't worry, turn off the music and listen to this video, I'm sure you will understand it.