How does Typescript get the type information of a npm package?

When import <pkg> in typescript, typescript will analysis package.json of the imported package, and look for types field. types field specify the type definition file of the package. So, when you create your own package with types, don't forget to specify types field in package.json.
As for exports.types field in package.json, it's not used by Typescript yet. So, if you specify exports.types instead of types, Typescript will report cannot find module xxx error.

  "main": "./lib/cjs/index.js",
  "module": "./lib/esm/index.js",
  "types": "./lib/cjs/types/index.d.ts", // ->  type definition file specified for Typescript
  "exports": {
    ".": {
      "import": "./lib/esm/index.js",
      "require": "./lib/cjs/index.js",
      "types": "./lib/cjs/types/index.d.ts", // -> not used by Typescript yet
      "node": "./lib/cjs/index.js"
    }
  },
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容