TypeScript typings in npm @types org packages

后端 未结 3 937
醉酒成梦
醉酒成梦 2020-11-29 17:48

I\'ve noticed there\'s an npm organization @types, which contains typing packages, but can\'t find any documentation on it. How are these meant to be used?

Is it me

相关标签:
3条回答
  • 2020-11-29 18:32

    As of TypeScript 2.0, typings is no longer required. The npm organization is an entity to setup a developers team. I believe Microsoft setup the @types organization in npm and added the TypeScript developer team to the organization. Packages on under the @types organization are published automatically from DefinitelyTyped using the types-publisher tool as per the docs.

    In addition, to there is another way to add types to your packages:

    In your package.json

    If your package has a main .js file, you will need to indicate the main declaration file in your package.json file as well. Set the types property to point to your bundled declaration file. For example:

    {
        "name": "awesome",
        "author": "Vandelay Industries",
        "version": "1.0.0",
        "main": "./lib/main.js",
        "types": "./lib/main.d.ts"
    }
    

    Note that the "typings" field is synonymous with "types", and could be used as well.

    Also note that if your main declaration file is named index.d.ts and lives at the root of the package (next to index.js) you do not need to mark the "types" property, though it is advisable to do so.

    Regarding searching types

    For the most part, type declaration packages should always have the same name as the package name on npm, but prefixed with @types/, but if you need, you can check out https://aka.ms/types to find the package for your favorite library.

    From - http://www.typescriptlang.org/docs/handbook/declaration-files/consumption.html

    But when I did npm search @types/openlayers, I did not get any results. But doing the search from the web interface did return me the results. So I guess npm search does not search across organizations.

    0 讨论(0)
  • 2020-11-29 18:44

    This is going to be a feature that is rolled out in Typescript 2.0. This provides type support for UMD Modules/Libraries and their respective definitions.

    See (Built-in support for UMD module definitions) to get a better understanding of the issues currently with ambient typings.

    0 讨论(0)
  • 2020-11-29 18:47

    Announcement on the TypeScript blog answers this: The Future of Declaration Files

    Summary:

    The @types npm organization is for obtaining type definitions with npm. Using these type definitions is a feature is coming in TypeScript 2.0.

    This will replace the current projects/tools such as typings and tsd, though these will continue to be supported for some time.

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