根据打字稿文档(https://www.typescriptlang.org/docs/handbook/module-resolution.html):
相对导入相对于导入文件已解析,并且无法解析为环境模块声明。
但是也:
例如,从/root/src/moduleA.ts中的“ ./moduleB”导入类似import {b}的导入语句将导致尝试以下位置来查找“ ./moduleB”:
/root/src/moduleB.ts
/root/src/moduleB.tsx
/root/src/moduleB.d.ts
/root/src/moduleB/package.json (if it specifies a "typings" property)
/root/src/moduleB/index.ts
/root/src/moduleB/index.tsx
/root/src/moduleB/index.d.ts"
在我看来,/ root / src / moduleB.d.ts行是一个环境模块声明,用于解析相对导入的“ ./moduleB”->文档完全否认了它的所作所为。
我在这里遗漏了什么东西还是文档错误?
在我看来,/root/src/moduleB.d.ts行是环境模块声明...我在这里遗漏了一些东西吗,或者文档是错误的?
您在这里缺少什么。moduleB.d.ts
不是环境模块声明。这是一个包含环境模块声明为的文件示例moduleB
。
// someFile.d.ts
declare module "moduleB" {
export class b { }
}
所述declare
关键字指定的环境声明。
关于环境一词,文档说:
我们将没有定义实现的声明称为“环境”。通常,这些文件在.d.ts文件中定义。
环境声明包括但不限于环境模块声明。包含环境声明的.d.ts文件不是环境模块声明,并且不一定包含环境模块声明。
例如,以下greeter.d.ts文件包含一个环境类声明,但不是环境模块声明。
// greeter.d.ts
declare class Greeter {
constructor(greeting: string);
greeting: string;
}
以下foobar.d.ts
文件包含两个环境模块声明“ foo”和“ bar”,但是该文件本身不是环境模块声明。
// foobar.d.ts
declare module "foo" {
export function doFoo(foo: string): string;
}
declare module "bar" {
export function doBar(bar: string): string;
}
您最初引用的文档指出,相对导入"./foo"
无法解决该模块的上述环境声明。
请参阅:https://www.typescriptlang.org/docs/handbook/modules.html
另请参阅:https : //github.com/Microsoft/TypeScript-Handbook/issues/180
另请参阅:https : //www.typescriptlang.org/docs/handbook/declaration-files/by-example.html
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句