相对导入无法解析为环境模块

adr*_*rhc 5 typescript

根据打字稿文档(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"
Run Code Online (Sandbox Code Playgroud)

在我看来,/ root/src/moduleB.d.ts行是一个环境模块声明,用于解析相对导入“ ./moduleB”->正是文档否认它所做的。

我在这里遗漏了什么东西还是文档错误?

Sha*_*tin 3

简答

\n\n
\n

在我看来, /root/src/moduleB.d.ts 行似乎是一个环境模块声明......我在这里遗漏了一些东西还是文档有误?

\n
\n\n

你在这里遗漏了一些东西。moduleB.d.ts不是环境模块声明。下面是一个包含环境模块声明的文件示例moduleB

\n\n
// someFile.d.ts \n\ndeclare module "moduleB" {\n    export class b { }\n}\n
Run Code Online (Sandbox Code Playgroud)\n\n

declare关键字指定环境声明。

\n\n

一些细节

\n\n

关于环境这个词,文档说

\n\n
\n

我们将不\xe2\x80\x99t 定义实现的声明称为\xe2\x80\x9cambient\xe2\x80\x9d。通常,这些是在 .d.ts 文件中定义的。

\n
\n\n

环境声明包括但不限于环境模块声明。包含环境声明的 .d.ts 文件不是环境模块声明,也不一定包含环境模块声明。

\n\n

例如,以下greeter.d.ts 文件包含环境类声明,但它不是环境模块声明。

\n\n
// greeter.d.ts\n\ndeclare class Greeter {\n    constructor(greeting: string);\n    greeting: string;\n}\n
Run Code Online (Sandbox Code Playgroud)\n\n

以下foobar.d.ts文件包含两个环境模块声明“foo”和“bar”,但该文件本身不是环境模块声明。

\n\n
// foobar.d.ts\n\ndeclare module "foo" {       \n    export function doFoo(foo: string): string;\n}\n\ndeclare module "bar" {\n    export function doBar(bar: string): string;\n}\n
Run Code Online (Sandbox Code Playgroud)\n\n

您最初引用的文档指出,相对导入"./foo"无法解析为该模块的上述环境声明。

\n\n

更多细节

\n\n

请参阅: https: //www.typescriptlang.org/docs/handbook/modules.html

\n\n

另请参阅: https: //github.com/Microsoft/TypeScript-Handbook/issues/180

\n\n

另请参阅: https: //www.typescriptlang.org/docs/handbook/declaration-files/by-example.html

\n