默认情况下,模块查找方式有两种:classic、nodejs 的模块查找规则。typescript 的模块查找规则中模仿了 nodejs 的模块查找规则。
对于使用相对路径进行导入的查找规则: 在 /root/src/moduleA.ts 中 import { b } from “./moduleB”,查找顺序如下
- /root/src/moduleB.ts
- /root/src/moduleB.tsx
- /root/src/moduleB.d.ts
- /root/src/moduleB/package.json (if it specifies a “types” property)
- /root/src/moduleB/index.ts
- /root/src/moduleB/index.tsx
- /root/src/moduleB/index.d.ts
对于使用绝对路径进行导入的查找规则:在 /root/src/moduleA.ts 中 import { b } from “moduleB”,查找顺序如下