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"
}
},