简体   繁体   English

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

[英]Typescript Cannot find namespace declared in global .d.ts file

I have declared namespace in adts which should be globally available like so:我已经在 adts 中声明了应该全局可用的命名空间,如下所示:

/src/typings/content.d.ts

import * as something from 'something';

declare namespace Content {

...

    type Object = internal.Object;
}

I want to use this namespace for a Interface in a TSX file:我想将此命名空间用于 TSX 文件中的接口:

import * as React from 'react';

interface ExampleComponentProps {
    example: Content.Object;
}

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

Typescript now tells me: Cannot find namespace 'Content' . Typescript 现在告诉我: Cannot find namespace 'Content'

My tsconfig looks like this:我的 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/**/*"
    ]
}

Checkout this other issue签出这个其他问题

If your file has top-level import or export statement it is considered as a module.如果您的文件具有顶级导入或导出语句,则将其视为模块。 All its' content (types, interfaces, etc.) you are interested in needs to be exported explicitly.您感兴趣的所有内容(类型、接口等)都需要显式导出。

The solution to this problem is pretty straightforward.这个问题的解决方案非常简单。 Don't use an import at the top of the d.ts-File.不要在 d.ts 文件的顶部使用导入。

This StackOverflow answer goes into great detail about how to handle such a thing这个 StackOverflow 答案非常详细地介绍了如何处理这样的事情

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM