Typescript 找不到全局 .d.ts 文件中声明的命名空间

Mea*_*ear 7 typescript reactjs typescript-typings

我已经在 .d.ts 中声明了命名空间,它应该是全局可用的,如下所示:

/src/typings/content.d.ts

import * as something from 'something';

declare namespace Content {

...

    type Object = internal.Object;
}
Run Code Online (Sandbox Code Playgroud)

我想将此命名空间用于 TSX 文件中的接口:

import * as React from 'react';

interface ExampleComponentProps {
    example: Content.Object;
}

export const ExampleComponent: React.FunctionComponent<ExampleComponentProps> = ({ example }) => {
    return <div>{example}</div>;
};
Run Code Online (Sandbox Code Playgroud)

打字稿现在告诉我:Cannot find namespace 'Content'

我的 tsconfig 看起来像这样:

{
    "compilerOptions": {
        // General settings for code interpretation
        "target": "esnext",
        "module": "commonjs",
        "jsx": "react",
        "allowSyntheticDefaultImports": true,
        "experimentalDecorators": true,
        "noEmit": true,
        "noEmitOnError": true,
        "removeComments": false,
        "resolveJsonModule": true,

        "baseUrl": "./src",
        ...
    },
    "include": [
        "./src/typings/*.d.ts",
        "./src/**/*"
    ]
}
Run Code Online (Sandbox Code Playgroud)

小智 2

查看其他问题

如果您的文件具有顶级导入或导出语句,则它被视为一个模块。您感兴趣的所有内容(类型、接口等)都需要显式导出。