Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

在新建目录下创建d.ts文件无法找到namespace🧐[问题 | question] #10842

Closed
1275727217 opened this issue Jul 14, 2023 · 3 comments
Closed
Labels

Comments

@1275727217
Copy link

🧐 问题描述 | Problem description

在services下创建一个新的目录,并定义d.ts 文件,使用的时候提示找不到命名空间,但是如果把d.ts 文件移动到ant-design-pro 或者直接在services目录下就可以找到

💻 示例代码 | Sample code

image
image

🚑 其他信息 | Other information

OS:

Node:

浏览器 | browser:

@github-actions
Copy link

以下的 Issues 可能会帮助到你 / The following issues may help you

@chenshuai2144
Copy link
Collaborator

TAutomatically replying with ChatGPT can be attempted, but it cannot be guaranteed to be completely accurate and may not fully address all issues. Please feel free to reply if you have any further questions or concerns.
此回复基于 ChatGPT 自动生成,可以尝试下方案,官方人员会在一定时间后继续继续处理。

🧐 问题描述 | Problem description

用户在创建新的目录并在其中定义了一个 .d.ts 文件,但是在使用时无法找到命名空间。然而,如果将 .d.ts 文件移动到 ant-design-pro 目录下或直接放在 services 目录中,则可以找到。

💻 示例代码 | Sample code

// src/services/myNamespace.d.ts

declare namespace myNamespace {
  interface MyInterface {
    // 这里定义接口内容
  }
}

export default myNamespace;

🚑 其他信息 | Other information

OS:
Node:
浏览器 | browser:

@chenshuai2144
Copy link
Collaborator

在新建目录下创建.d.ts文件时无法找到命名空间的问题可能是因为引用方式不够规范导致的。

umi@3中,通过import会自动找到同名的.d.ts文件,但在umi@4中会发生报错。为了解决这个问题,你可以使用更加规范的引用方式。

例如,假设你在services目录下创建了一个新的目录,并定义了d.ts文件,现在你想在其他地方使用该命名空间,你可以按照以下方式引用:

- import { SomeNamespace } from './path/to/d';
+ import type { SomeNamespace } from './path/to/d';

通过添加type关键词来引用类型,这样就可以避免报错了。

另外需要注意的是,确保.d.ts文件与要引用的文件在同一目录下或者在正确的路径下,这样才能正确找到命名空间。如果你将.d.ts文件移动到ant-design-pro目录或直接放在services目录下就可以找到命名空间了。

希望我的回答能帮到你!如果还有其他问题,请随时提问。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants