Typescript. Declaration d.ts files

Source

Also good article is here

This feature is to support vanilla js code in typescript for popular libs like jquery, loadash, etc.

How to use ?

Declaration files (.d.ts files) are a fundamental part of using existing JavaScript libraries in TypeScript, but getting them has always been a place where we’ve known there was room for improvement. As we get closer to TypeScript 2.0, we’re very excited to show off a sneak peak of our plan to simplify things. Getting type declarations in TypeScript 2.0 will require no tools apart from npm.

As an example, getting the declarations for a library like lodash will be just an npm command away:

npm install --save @types/lodash
// or
npm install -S @types/foo-bar
From there you’ll be able to use lodash in your TypeScript code with no fuss. This works for both modules and global code.

For example, once you’ve npm install-ed your type declarations, then you can use imports and write

import * as _ from "lodash";
_.padStart("Hello TypeScript!", 20, " ");

or if you’re not using modules, you can just use the global variable _ if you have a tsconfig.json around.

_.padStart("Hello TypeScript!", 20, " ");
Looking for more than just lodash? Well, we’re also making it extremely easy to figure out which packages have the type declarations you need. We now have a type search at https://aka.ms/types where you can find the package for your favorite library.

Best of all, this all works today with our nightly builds. Just run npm install -g typescript@next, and give it a shot. This is all you need to know to start experiencing the future of type acquisition.

This entry was posted in Без рубрики. Bookmark the permalink.