reactjs - typescript 找不到在全局 .d.ts 文件中声明的命名空间

标签 reactjs typescript typescript-typings

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

/src/typings/content.d.ts

import * as something from 'something';

declare namespace Content {

...

    type Object = internal.Object;
}
我想将此命名空间用于 TSX 文件中的接口(interface):
import * as React from 'react';

interface ExampleComponentProps {
    example: Content.Object;
}

export const ExampleComponent: React.FunctionComponent<ExampleComponentProps> = ({ example }) => {
    return <div>{example}</div>;
};
现在 typescript 告诉我: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/**/*"
    ]
}

最佳答案

查看此 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.

关于reactjs - typescript 找不到在全局 .d.ts 文件中声明的命名空间,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65792106/

相关文章:

javascript - 在 typescript 中循环遍历文本文件

javascript - 我如何在 Jest 中比较高阶函数

javascript - jsx中如何写if语句来判断是否显示某个属性

reactjs - TS 表达式产生的联合类型太复杂,无法用 Material-UI 和 @react-three/fiber 表示

javascript - 加载选择控件时如何删除预选选项?

typescript - 为什么某些 Typescript 模块名称以字符串形式给出?

typescript - 您正在使用遗留实现。请更新您的代码 : use createWrapper() and wrapper. useWrappedStore()。 nextjs 还原?

javascript - TypeScript 中 React StatelessComponent 的 DefaultProps

typescript - 模块和接口(interface)可以同名吗?

typescript - 如何通过 TypeScript 中的映射类型删除属性