简体   繁体   English

如何在不将其转换为模块的情况下在`d.ts` 文件中导入类型(从另一个文件)?

[英]How to import a type (from another file) inside a `d.ts` file without turning it into a module?

Just ran into a weird situation regarding d.ts files and namespaces.刚刚遇到了关于d.ts文件和命名空间的奇怪情况。

I have some d.ts files where I declare and merge a namespace named PROJECT .我有一些d.ts文件,我在其中声明并合并了一个名为PROJECTnamespace

See below how it's declared and automatically merged (across multile files):请参阅下面的声明和自动合并(跨多个文件):

file1.d.ts ----- file2.d.ts ----- file3.d.ts file1.d.ts ----- file2.d.ts ----- file3.d.ts

declare namespace PROJECT {

  interface SOME_INTERFACE {
    ...
  }

  type SOME_TYPE = SOME_UNION_TYPE

  // ETC
}

That PROJECT namespace is automatically accessible from every file of my project.PROJECT命名空间可从我项目的每个文件中自动访问。 Example:例子:

SomeComponent.tsx一些组件.tsx

const someVariable: PROJECT.SOME_INTERFACE = { 
  // ... 
};

This is all expected behavior.这都是预期的行为。

My problem started when I decided to declare another namespace.当我决定声明另一个命名空间时,我的问题就开始了。

ADMIN_BLOGPOST.d.ts ADMIN_BLOGPOST.d.ts

import type { ACTION_THUNK_GENERIC } from "@hooks/useReducerThunk";

declare namespace ADMIN_BLOGPOST {
  // HERE I DECLARE MULTIPLE TYPES
  // AND ONE OF THE TYPES USES THE `ACTION_THUNK_GENERIC` TYPE, WHICH IS BEING IMPORTED HERE

  type ACTION_THUNK = ACTION_THUNK_GENERIC<ACTION,GET_STATE>
}

And just because of that top-level import type of the ACTION_THUNK_GENERIC , now my namespace is not automatically available anymore.并且仅仅因为ACTION_THUNK_GENERIC顶级import type ,现在我的命名空间不再自动可用。

VSCode started to consider ADMIN_BLOGPOST.d.ts as a module and started making me have to import that namespace before using it, like: VSCode 开始将ADMIN_BLOGPOST.d.ts视为一个模块,并开始让我在使用它之前必须import该命名空间,例如:

SomeComponent.tsx一些组件.tsx

import type { ADMIN_BLOGPOST } from "@src/types/ADMIN_BLOGPOST";

const someVariable: ADMIN_BLOGPOST.ACTION_THUNK

Note that if I remove that top-level import (the one that imports ACTION_THUNK_GENERIC ) from the ADMIN_BLOGPOST.d.ts file, then my ADMIN_BLOGPOST namespace becomes available without the need to import it first.请注意,如果我从ADMIN_BLOGPOST.d.ts文件中删除该顶级导入(导入ACTION_THUNK_GENERIC那个),那么我的ADMIN_BLOGPOST命名空间变得可用,而无需先import它。

But I really need that import.但我真的需要那个进口。 Because I need the ACTION_THUNK_GENERIC type so I can build the non-generic ACTION_THUNK type.因为我需要ACTION_THUNK_GENERIC类型,所以我可以构建非通用ACTION_THUNK类型。

Is there a different way to do this so my ADMIN_BLOGPOST namespace is available without having to import it before using?有没有不同的方法来做到这一点,以便我的ADMIN_BLOGPOST命名空间可用,而无需在使用前导入它? Ie: How to import inside a d.ts file without turning it into a module?即:如何在不将其转换为模块的情况下import d.ts文件?

Use declare global to work in a global namespace irrespective of module imports.无论模块导入如何,都可以使用declare global在全局命名空间中工作。 For example:例如:

import type { ACTION_THUNK_GENERIC } from "@hooks/useReducerThunk";
declare global {
  declare namespace ADMIN_BLOGPOST {
    type ACTION_THUNK = ACTION_THUNK_GENERIC<ACTION,GET_STATE>
  }
}

Alternatively you can use a dynamic import:或者,您可以使用动态导入:

declare namespace ADMIN_BLOGPOST {
  type ACTION_THUNK = import("@hooks/useReducerThunk").ACTION_THUNK_GENERIC<ACTION,GET_STATE>
}

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

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