What is the main usage of index.d.ts in Typescript?

后端 未结 1 1904
星月不相逢
星月不相逢 2020-12-28 12:30

I have seen some projects declaring all the types in index.d.ts. So that the programmer do not need to explicitly import the type from other files.



        
相关标签:
1条回答
  • 2020-12-28 13:02

    *.d.ts files are used to provide typescript type information about a module that's written in JavaScript, for example underscore / lodash / aws-sdk.

    This will allow you to use the javascript modules without the need to convert them to ts without getting any type error on you code.

    for example if you have a folder myAwesomeLib, with index.js and index.d.ts files

    on your code you will be able to import the code with

    import { SomeMethod } from './myAwesomeLib';
    

    or

    import { SomeMethod } from './myAwesomeLib/index';
    

    your typescript will rely on the .d.ts file to find the correct types for the SomeMethod

    Edit: More about Declaration files https://basarat.gitbooks.io/typescript/docs/types/ambient/d.ts.html

    0 讨论(0)
提交回复
热议问题