sách gpt4 ăn đã đi

nhập - Nhập TypeScript * không tạo bí danh

In lại 作者:搜寻专家 更新时间:2023-10-30 20:44:07 34 4
mua khóa gpt4 giày nike

在 TypeScript 中,如何在不创建任何别名的情况下从文件“导入 *”?

例如我有一个包含顶级导出函数的文件“utils”,我想导入所有这些函数而不为每个函数重新创建别名。

Như thế này:

import * from "utils";

Điều này có thể thực hiện được không?

câu trả lời hay nhất

您不能自动生成名称,您必须为其指定一个文件本地名称。这是为每个文件提供自己的命名上下文而设计的。

// import the default export (something exported with "export default")
import Stuff from "./Stuff";

// import specific things only; aliases in this style are optional
import { ItemA as AliasA, ItemB as AliasB } from "./Stuff";
import { ItemA, ItemB } from "./Stuff";

// import everything at once, grouped under a common name
import * as Stuff from "./Stuff";

I ... want to import all them without recreating the aliases for each function.

听起来您想要上面的第三个选项。

but with this syntax creates an alias

我认为这个想法是,如果您可以导入所有内容,同时采用定义的名称,那么您就会遇到命名冲突。这样做的方式是,您被迫为每个导入选择一个名称,将其留给您选择的名称以避免冲突,而不是让导入相互破坏。

我认为您可以做类似的事情,但只能使用 .d.ts tài liệu. jquery.d.ts 做到了,但我并不是 100% 了解它的工作原理。你可以简单地说:

// where the file is really Stuff.d.ts
import "./Stuff";

关于import - TypeScript import * 不创建别名,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31625206/

34 4 0
Chứng chỉ ICP Bắc Kinh số 000000
Hợp tác quảng cáo: 1813099741@qq.com 6ren.com
Xem sitemap của VNExpress