javascript - Simple example of the construction and application of a .d.ts file? -


i'm reading through documentation trying understand purpose of typescript .d.ts files or declaration files:

when using external javascript library, or new host api, you’ll need use declaration file (.d.ts) describe shape of library.

can please provide simple example of means , how apply it? document it, seems there's simpler way explain it. example if wanted use moment.js typescript need following momentjs.d.ts file...

but why need file? in other words why can't import , use momentjs without *.d.ts file?

for moment specifically, have run typings install --global dt~moment ("install global declaration file of moment source typed").

the reason need declaration file, because external library not written in typescript. result, have define api of library looks like, typescript knows available.

the easiest declaration file this:

custom-typings/moment.d.ts

declare module 'moment' 

this allow use moment library wherever want, won't have of juicy autocomplete or typechecking available of functions of library. however, typechecking external module extremely helpful, aren't familiar api, compared code wrote yourself.


Comments