"types" 是否等同于 TypeScript 项目的 "main"?
Is "types" the equivalent of "main" for TypeScript projects?
Similarly, a non-relative import will follow the Node.js resolution
logic, first looking up a file, then looking up an applicable folder.
So import { b } from "moduleB"
in source file /root/src/moduleA.ts
would result in the following lookups:
/root/src/node_modules/moduleB.ts
/root/src/node_modules/moduleB.tsx
/root/src/node_modules/moduleB.d.ts
/root/src/node_modules/moduleB/package.json
(if it specifies a "types"
property)
/root/src/node_modules/@types/moduleB.d.ts
/root/src/node_modules/moduleB/index.ts
/root/src/node_modules/moduleB/index.tsx
/root/src/node_modules/moduleB/index.d.ts
所以 TypeScript 似乎没有考虑 "main" 属性(如 Node.js does),但它确实会寻找 "types" .它们是等效的,还是后者只导入 *.d.ts 文件?
我认为您是在谈论 package.json
文件的属性。如果是这种情况,正如您所猜测的那样,types
是 TypeScript 用来加载模块的 typing(并且仅是 typing)的条目。 main
条目仍然需要加载模块的 JavaScript 端。
Similarly, a non-relative import will follow the Node.js resolution logic, first looking up a file, then looking up an applicable folder. So
import { b } from "moduleB"
in source file/root/src/moduleA.ts
would result in the following lookups:
/root/src/node_modules/moduleB.ts
/root/src/node_modules/moduleB.tsx
/root/src/node_modules/moduleB.d.ts
/root/src/node_modules/moduleB/package.json
(if it specifies a"types"
property)/root/src/node_modules/@types/moduleB.d.ts
/root/src/node_modules/moduleB/index.ts
/root/src/node_modules/moduleB/index.tsx
/root/src/node_modules/moduleB/index.d.ts
所以 TypeScript 似乎没有考虑 "main" 属性(如 Node.js does),但它确实会寻找 "types" .它们是等效的,还是后者只导入 *.d.ts 文件?
我认为您是在谈论 package.json
文件的属性。如果是这种情况,正如您所猜测的那样,types
是 TypeScript 用来加载模块的 typing(并且仅是 typing)的条目。 main
条目仍然需要加载模块的 JavaScript 端。