在另一个文件中扩展导入的枚举并使用它

Ano*_*Dev 6 enums module extending typescript

我正在尝试在不同的文件中扩展导入的枚举,并在另一个不同的文件中使用该扩展的枚举。

一般情况

base.enum.ts

export enum MyEnum {
    a = "Foo"
}
Run Code Online (Sandbox Code Playgroud)

扩展.enum.ts

import { MyEnum } from './base.enum';

declare module './base.enum' {
    export enum MyEnum {
        b = "Bar"
    }
}
Run Code Online (Sandbox Code Playgroud)

在 index.ts 中使用

import { MyEnum } from './base.enum';
import './extended.enum'; // side-effects import (no usage of export)

console.log(MyEnum.a); // prints "Foo" as expected
console.log(MyEnum.b); // prints undefined, instead of the expected "Bar"
Run Code Online (Sandbox Code Playgroud)

(我在支持字符串值枚举的 TypeScript 2.4.2 中这样做)

我已经使用这个这个SO 问题作为参考,并在 GitHub 中阅读了以下TypeScript 问题,但仍然找不到解决我的问题的方法。


类似于我的用法示例

在animals/base/animal-types.enum.ts 中的基本枚举AnimalTypes:

export enum AnimalTypes { }
Run Code Online (Sandbox Code Playgroud)

在animals/base/animal.ts中的基本接口Animal:

import { AnimalTypes } from './animal-types';
export interface Animal {
    type: AnimalTypes;
}
Run Code Online (Sandbox Code Playgroud)

动物/基地/index.ts:

export * from './animal-types.enum';
export * from './animal';
Run Code Online (Sandbox Code Playgroud)

在animals/animal-types.enum.ts 中扩展枚举AnimalTypes:

import { AnimalTypes } from './base/';
declare module './base/animal-types.enum' {
    export enum AnimalTypes {
        Cat = "cat",
        Dog = "dog"/*,
        ...*/
    }
}
Run Code Online (Sandbox Code Playgroud)

动物/cat.ts 中的具体类 Cat:

import { Animal, AnimalTypes } from './base/';
import './animal-types.enum';
export class Cat implements Animal {
    public type: AnimalTypes = AnimalTypes.Cat; // Usage of AnimalTypes with extended value
}
Run Code Online (Sandbox Code Playgroud)

Animals/dog.ts 中的具体类 Dog:

import { Animal, AnimalTypes } from './base/';
import './animal-types.enum';
export class Dog implements Animal {
    public type: AnimalTypes = AnimalTypes.Dog; // Usage of AnimalTypes with extended value
}
Run Code Online (Sandbox Code Playgroud)

动物/index.ts:

export * from './cat';
export * from './dog';
//...
Run Code Online (Sandbox Code Playgroud)

在animals/animals-manager.ts 中的最终用法:

import { Animal, AnimalTypes} from './base/';
import { Cat, Dog/*, ...*/ } from '.';
import './animal-types'; // side-effects import (no usage of export)

export class AnimalsManager {
    public animals: { [animal: string]: Animal } = {}; // Animal dictionary (I would use AnimalTypes as key type but that isn't supported yet as far as I know).
    constructor() {
        this.animals[AnimalTypes.Cat] = new Cat();
        this.animals[AnimalTypes.Dog] = new Dog();
    }
    //...
}
Run Code Online (Sandbox Code Playgroud)

当尝试使用 AnimalManager's 时,animals[AnimalTypes.Cat]我会得到animals[AnimalTypes.Dog]因为两者AnimalTypes.CatAnimalTypes.Dogreturn的值undefined(这意味着animals[AnimalTypes.Cat]被设置覆盖animals[AnimalTypes.Dog])。


那么,目前是否有一种方法可以像上面描述的那样在 TypeScript 中扩展导入的枚举,或者我是否必须绕过我的方式来获得支持这种扩展的自定义枚举?谢谢。

art*_*tem 3

TypeScript 只允许扩展 中的类型信息declare module,不会从这些声明中执行任何代码。事实上,内部包含代码declare module通常是不允许的,例如将带有主体的函数放在那里会产生An implementation cannot be declared in ambient contexts错误。

因此,您为MyEnum枚举类型声明了附加成员,但该成员未在任何地方初始化,因此其值是undefined在运行时的。要解决这个问题,您可以自己初始化它:

扩展.enum.ts

import { MyEnum } from './base.enum';

declare module './base.enum' {
    export enum MyEnum {
        b = "Bar"
    }
}

const myEnumValues = MyEnum as any;
myEnumValues["b"] = "Bar";
Run Code Online (Sandbox Code Playgroud)

您必须首先进行强制转换MyEnumany因为不允许直接初始化:

MyEnum["b"] = "Bar";
Run Code Online (Sandbox Code Playgroud)

编译时没有错误

error TS2540:Cannot assign to 'b' because it is a constant or a read-only property.
Run Code Online (Sandbox Code Playgroud)