在同一 TypeScript 文件中同时导入和导出的目的是什么?

huangapple go评论65阅读模式
英文:

What's the point of importing and exporting at the same time in the same typescript file

问题

我阅读了chart.js的源文件,发现以下几行代码:

export * from './controllers/index.js'';
export * from './core/index.js'';
export * from './elements/index.js'';
export * from './platform/index.js'';
export * from './plugins/index.js'';
export * from './scales/index.js'';

import * as controllers from './controllers/index.js';
import * as elements from './elements/index.js';
import * as plugins from './plugins/index.js';
import * as scales from './scales/index.js';

export {
  controllers,
  elements,
  plugins,
  scales,
};

我稍微理解了这段代码,但对于其中的这行代码import * as controllers from './controllers/index.js',然后将其导出为不同的简单名称controllers,我不太确定。

但为什么要这样做export * from './controllers/index.js'; 好像重复导入/导出了相同的包。

我搜索了一下,发现许多TypeScript脚本都这样做。有人可以解释一下这段代码的好处/原因吗?

这不会导致任何依赖循环问题吗?

我明白了。我看到很多示例都这样做。我想这是基于TypeScript的某种约定。

英文:

I read chart.js source files found lines below:

export * from './controllers/index.js';
export * from './core/index.js';
export * from './elements/index.js';
export * from './platform/index.js';
export * from './plugins/index.js';
export * from './scales/index.js';

import * as controllers from './controllers/index.js';
import * as elements from './elements/index.js';
import * as plugins from './plugins/index.js';
import * as scales from './scales/index.js';

export {
  controllers,
  elements,
  plugins,
  scales,
};

I understand slightly but not sure enough with the line import * as controllers from './controllers/index.js'; and then export it as a different simple name controllers.

But why export * from './controllers/index.js'; It seems do duplicate things as import/export the same package.

I search that many ts script is doing like this. Can someone please give some explanation for the benefit/reason of this piece of code?

Won't this cause any dependecy loop issue?

Understand. I saw multiple samples doing this. I thought it based some convention of TS.

答案1

得分: 1

这会重复导出相同的内容,但方式略有不同

这个

export * from './controllers/index.js';

会将其他文件中的所有导出内容重新导出,保持它们原始的导出名称。例如,如果该文件有 export const foo = 'foo';,上面的代码将创建一个名为 foo 的具名导出。它允许通过此文件使用 controllers,如下所示:

import foo from './thepath.js';
console.log(foo);

但这个

import * as controllers from './controllers/index.js';
export {
  controllers,

将所有导入内容放入一个单一的具名导出中,这个导出是一个名为 controllers 的命名空间对象。它允许通过此文件使用 controllers,如下所示:

import { controllers } from './thepath.js';
console.log(controllers.foo);

因此,它会重复导出内容,但这可能是有意为之,以便模块的使用者有更多选择。这种重复并不是必需的,只是他们决定采用的设计选择。

这不会导致任何依赖循环吗?

不会,没有循环依赖。

英文:

They do import/export the same thing, but in slightly different ways.

This

export * from './controllers/index.js';

will re-export all exports from that other file under their original export names. For example, if that file has export const foo = 'foo';, that above line will create a named export named foo. It allows the use of controllers through this file with:

import foo from './thepath.js';
console.log(foo);

But this

import * as controllers from './controllers/index.js';
export {
  controllers,

instead takes all the imports and puts them into a single named export, a namespace object called controllers. It allows the use of controllers through this file with:

import { controllers } from './thepath.js';
console.log(controllers.foo);

So it is duplicating exports, but that may well be deliberate so that consumers of the module find it handy to have more options. Such duplication isn't necessary, just a design choice they decided to go with.

>Won't this cause any dependency loop?

No, there's no circular dependency.

huangapple
  • 本文由 发表于 2023年3月1日 13:47:07
  • 转载请务必保留本文链接:https://go.coder-hub.com/75599985.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定