如何在Typescript中以其他名称重新导出枚举?

我想在我的库(在React应用程序中)中为另一个库(在这种情况下为monaco编辑器)的导出成员提供一个更简单的接口。为此,我创建了一个index.ts文件,该文件确实重新导出了许多类型,并在其中删除了内部名称空间。这很好用:

import { languages } from "monaco-editor/esm/vs/editor/editor.api";

export type FoldingRange = languages.FoldingRange;
export type FoldingContext = languages.FoldingContext;
export type FoldingRangeKind = languages.FoldingRangeKind;

except for the last line, because FoldingRangeKind is an enum. I cannot use export enum here as that expects a full enum definition.

另一种选择是:

export import FoldingRangeKind = languages.FoldingRangeKind;

但不幸的是,通天塔不接受。它导致此错误:

import =is not supported by @babel/plugin-transform-typescript Please consider usingimport from '';` alongside Typescript's --allowSyntheticDefaultImports option.

默认导入在这里无济于事,所以我想知道该怎么做。

评论
  • Saul
    Saul 回复

    枚举不是类型,而是对象。它的值也是值,因此您需要将它们分配给常量。

    enum languages {
        FoldingRange,
        FoldingContext,
        FoldingRangeKind
    }
    
    export const FoldingRange = languages.FoldingRange;
    export const FoldingContext = languages.FoldingContext;
    export const FoldingRangeKind = languages.FoldingRangeKind;
    

    This is what is then compiled (playground). Hopefully this clears it up for you.

    var languages;
    (function (languages) {
        languages[languages["FoldingRange"] = 0] = "FoldingRange";
        languages[languages["FoldingContext"] = 1] = "FoldingContext";
        languages[languages["FoldingRangeKind"] = 2] = "FoldingRangeKind";
    })(languages || (languages = {}));
    
    export const FoldingRange = languages.FoldingRange;
    export const FoldingContext = languages.FoldingContext;
    export const FoldingRangeKind = languages.FoldingRangeKind;