TypeScript 2:使用命名空间而不参考Triple-Slash指令

时间:2017-01-22 15:10:07

标签: reactjs typescript namespaces typescript2.0

我有一个TypeScript React项目,我目前正在为我拥有的每个文件使用模块导入。 在这个项目中,我有一个“容器”组件,其中包含多个“内容”组件。 目前,Container会像这样导入内容:

import {Content} from "./Content";

现在我想将我的项目更改为TypeScript命名空间,因此我不必指定我正在使用的每个文件的相对路径。

我已经指定了我的tsconfig.json来包含项目文件夹中的每个文件:

{
"compilerOptions": {
    "sourceMap": true,
    "noImplicitAny": false,
    "module": "system",
    "moduleResolution": "Node",
    "target": "es6",
    "jsx": "react",
    "experimentalDecorators": true,
    "rootDir": "./src/",
    "locale": "en-us",
    "outFile": "./dist/bundle.js"
}
}

src文件夹包含我的所有打字稿类。

现在我已经重写了我的文件:

Content.tsx:

namespace ContainerSpace {
    export class Content extends React.Component<any, any> {
    ...
    }
}

Container.tsx:

import Content = ContainerSpace.Content;

namespace ContainerSpace {
    export class Container extends React.Component<any, any> {
    ...
    render() {
        return <Content/>
    }
    ...
}

但是当我编译时,我得到一个像这样的错误:

error TS2339: Property 'Content' does not exist on type 'typeof ContainerSpace'.

如何在不使用Reference指令的情况下在多个文件中使用命名空间?我已经读过可以使用tsconfig.json,但从未真正找到某人解释如何配置tsconfig,所以我不必使用引用注释。

0 个答案:

没有答案