了解TypeScript导入、模块和命名空间

了解TypeScript导入、模块和命名空间,typescript,import,module,namespaces,Typescript,Import,Module,Namespaces,我在尝试导入TypeScript项目中的类型时遇到问题。我不完全确定导入是如何工作的 文件foo.ts export class Bar { } import { Bar } from "./Foo"; // path needs to be relative here // if referencing files are in same folder use ./ to force the path to be relative 文件栏.ts export class Bar { }

我在尝试导入TypeScript项目中的类型时遇到问题。我不完全确定导入是如何工作的

文件foo.ts

export class Bar {
}
import { Bar } from "./Foo"; // path needs to be relative here
// if referencing files are in same folder use ./ to force the path to be relative
文件栏.ts

export class Bar {
}
import { Bar } from "./Foo"; // path needs to be relative here
// if referencing files are in same folder use ./ to force the path to be relative
找不到模块“Foo”

文件“/scripts/foo.ts”不是模块

那么,我在这里误解了什么?另外,我应该使用
模块
还是
命名空间
,有什么区别


注意
foo.ts
bar.ts
在同一位置…我可以省略
。/scripts/

您可能应该阅读一些文档。一个快速的谷歌让我看了一下,并试图更好地了解你到底需要什么,因为我无法通过你的帖子准确地判断,因为模块的语法似乎是
declare module“Foo”{…}
,根据我在那里看到的,名称空间似乎是你想要的。我不完全确定,但我觉得您的答案就在那一页上。

您缺少的是TypeScript支持两种类型的模块-和。内部模块在较新版本中被重命名为名称空间(这就是为什么有较旧的
模块
和较新的
名称空间
关键字-您可以在项目中随意使用它们/它们是相等的)

在bar.ts中使用
import
语法时,您试图使用外部模块

但是,在将
export
添加到根范围之前,foo.ts不是外部模块

我建议继续使用
import
,并使用如下外部模块:

Foo.ts

export class Bar {
}
import { Bar } from "./Foo"; // path needs to be relative here
// if referencing files are in same folder use ./ to force the path to be relative
Bar.ts

export class Bar {
}
import { Bar } from "./Foo"; // path needs to be relative here
// if referencing files are in same folder use ./ to force the path to be relative
在您想要将类包装到名称空间之前,不需要使用
模块
/
名称空间