import { Constructor } from '@polkadot/types/types'; import { Enum } from '@polkadot/types/codec'; import { EnumConstructor } from '@polkadot/types/codec/Enum'; export interface ExtendedEnum> extends Enum { isOfType: (type: keyof Types) => boolean; asType(type: TypeKey): InstanceType; }; export interface ExtendedEnumConstructor> extends EnumConstructor> { create(typeKey: TypeKey, value: InstanceType): ExtendedEnum; } // Helper for creating extended Enum type with TS-compatible isOfType and asType helpers export function JoyEnum>(types: Types): ExtendedEnumConstructor { // Unique values check if (Object.values(types).some((val, i) => Object.values(types).indexOf(val, i + 1) !== -1)) { throw new Error('Values passed to JoyEnum are not unique. Create an individual class for each value.'); } return class JoyEnumObject extends Enum { public static create(typeKey: TypeKey, value: InstanceType) { return new JoyEnumObject({ [typeKey]: value }); } constructor(value?: any, index?: number) { super(types, value, index); } public isOfType(typeKey: keyof Types) { return this.value instanceof types[typeKey]; } public asType(typeKey: TypeKey) { if (!(this.value instanceof types[typeKey])) { throw new Error(`Enum.asType(${typeKey}) - value is not of type ${typeKey}`); } return this.value as InstanceType; } } }