There are a few ways to dynamically remap compound enums in Typescript while ensuring type safety:
Example:
enum OriginalEnum {
A = 'A',
B = 'B',
C = 'C',
}
enum MappedEnum {
X = 'X',
Y = 'Y',
Z = 'Z',
}
const mapping = {
[OriginalEnum.A]: MappedEnum.X,
[OriginalEnum.B]: MappedEnum.Y,
[OriginalEnum.C]: MappedEnum.Z,
};
function mapEnum(originalValue: OriginalEnum): MappedEnum {
return mapping[originalValue];
}
const originalValue = OriginalEnum.A;
const mappedValue = mapEnum(originalValue); // MappedEnum.X
// Type safety:
// mapEnum(OriginalEnum.A) // MappedEnum.X
// mapEnum(OriginalEnum.B) // MappedEnum.Y
// mapEnum(OriginalEnum.C) // MappedEnum.Z
// mapEnum('invalid') // compile error
Example:
abstract class BaseEnum {
abstract getName(): string;
}
class OriginalEnumValue extends BaseEnum {
private readonly _name: string;
constructor(name: string) {
super();
this._name = name;
}
getName(): string {
return this._name;
}
}
class MappedEnumValue extends BaseEnum {
private readonly _name: string;
constructor(name: string) {
super();
this._name = name;
}
getName(): string {
return this._name;
}
}
class OriginalEnum extends BaseEnum {
static readonly A = new OriginalEnumValue('A');
static readonly B = new OriginalEnumValue('B');
static readonly C = new OriginalEnumValue('C');
static mapTo(mappedEnum: typeof MappedEnum): MappedEnumValue {
switch (this) {
case OriginalEnum.A:
return mappedEnum.X;
case OriginalEnum.B:
return mappedEnum.Y;
case OriginalEnum.C:
return mappedEnum.Z;
default:
throw new Error(`Invalid OriginalEnum value: ${this.getName()}`);
}
}
getName(): string {
return this.constructor.name;
}
}
class MappedEnum extends BaseEnum {
static readonly X = new MappedEnumValue('X');
static readonly Y = new MappedEnumValue('Y');
static readonly Z = new MappedEnumValue('Z');
getName(): string {
return this.constructor.name;
}
}
const originalValue = OriginalEnum.A;
const mappedValue = OriginalEnum.mapTo(MappedEnum); // MappedEnum.X
// Type safety:
// OriginalEnum.A.mapTo(MappedEnum) // MappedEnum.X
// OriginalEnum.B.mapTo(MappedEnum) // MappedEnum.Y
// OriginalEnum.C.mapTo(MappedEnum) // MappedEnum.Z
// OriginalEnum.A.mapTo(OriginalEnum) // compile error
// OriginalEnum.D.mapTo(MappedEnum) // runtime error
Please start posting anonymously - your entry will be published after you log in or create a new account. This space is reserved only for answers. If you would like to engage in a discussion, please instead post a comment under the question or an answer that you would like to discuss
Asked: 2022-04-06 11:00:00 +0000
Seen: 9 times
Last updated: Nov 01 '22
How can one ensure that sub-classes have uniform method parameters in TypeScript?
Can a TypeScript definition for icon names be provided in MaterialCommunityIcons for React Native?
How can TypeScript be used to save a file in an Excel add-in?
What is the Angular Type script NG Zorro event for radio buttons?
What is the process for implementing a Many to Many relationship using NestJs Sequlize and Mysql2?
What is the method for defining a Fixed length Array in TypeScript?
What difficulties are encountered in the installation of react-router-dom using typescript?