diff --git a/packages/type-compiler/src/compiler.ts b/packages/type-compiler/src/compiler.ts index 1d78359fe..9847d2a0f 100644 --- a/packages/type-compiler/src/compiler.ts +++ b/packages/type-compiler/src/compiler.ts @@ -167,7 +167,8 @@ const OPs: { [op in ReflectionOp]?: { params: number } } = { [ReflectionOp.propertySignature]: { params: 1 }, [ReflectionOp.property]: { params: 1 }, [ReflectionOp.jump]: { params: 1 }, - [ReflectionOp.enum]: { params: 1 }, + [ReflectionOp.enum]: { params: 0 }, + [ReflectionOp.enumMember]: { params: 1 }, [ReflectionOp.typeParameter]: { params: 1 }, [ReflectionOp.typeParameterDefault]: { params: 1 }, [ReflectionOp.mappedType]: { params: 2 }, diff --git a/packages/type-compiler/tests/transpile.spec.ts b/packages/type-compiler/tests/transpile.spec.ts index 401e4b87e..6b1b9d9bd 100644 --- a/packages/type-compiler/tests/transpile.spec.ts +++ b/packages/type-compiler/tests/transpile.spec.ts @@ -282,3 +282,23 @@ test('readonly array', () => { console.log(res); }); + +test('enum union', () => { + const res = transpileAndRun({ + 'app': ` + enum StatEnginePowerUnit { + Hp, + } + + enum StatWeightUnit { + Lbs, + Kg, + } + + type StatMeasurementUnit = StatEnginePowerUnit | StatWeightUnit; + typeOf() + ` + }); + + console.log(res); +}); diff --git a/packages/type/src/reflection/processor.ts b/packages/type/src/reflection/processor.ts index 309c7ff9f..e7dd68978 100644 --- a/packages/type/src/reflection/processor.ts +++ b/packages/type/src/reflection/processor.ts @@ -407,7 +407,7 @@ export class Processor { for (; program.program < program.end; program.program++) { const op = program.ops[program.program]; - // process.stdout.write(`[${program.depth}:${program.frame.index}] step ${program.program} ${RuntimeReflectionOp[op]}\n`); + // process.stdout.write(`[${program.depth}:${program.frame.index}] step ${program.program} ${ReflectionOp[op]}\n`); switch (op) { case ReflectionOp.string: this.pushType({ kind: ReflectionKind.string }); diff --git a/packages/type/src/reflection/type.ts b/packages/type/src/reflection/type.ts index d746c8316..4dcc6f714 100644 --- a/packages/type/src/reflection/type.ts +++ b/packages/type/src/reflection/type.ts @@ -694,6 +694,17 @@ export function isSameType(a: Type, b: Type, stack: StackEntry[] = []): boolean return isSameType(a.return, b.return, stack); } + if (a.kind === ReflectionKind.enum) { + if (b.kind !== ReflectionKind.enum) return false; + if (a.values.length !== b.values.length) return false; + + for (let i = 0; i < a.values.length; i++) { + if (a.values[i] !== b.values[i]) return false; + } + + return true; + } + if (a.kind === ReflectionKind.union) { if (b.kind !== ReflectionKind.union) return false; if (a.types.length !== b.types.length) return false; diff --git a/packages/type/tests/serializer.spec.ts b/packages/type/tests/serializer.spec.ts index 7645c9cea..d7e5c459f 100644 --- a/packages/type/tests/serializer.spec.ts +++ b/packages/type/tests/serializer.spec.ts @@ -10,6 +10,7 @@ import { expect, test } from '@jest/globals'; import { reflect, ReflectionClass, typeOf } from '../src/reflection/reflection'; import { + assertType, AutoIncrement, BackReference, BinaryBigInt, @@ -30,7 +31,7 @@ import { createSerializeFunction, getSerializeFunction, NamingStrategy, serializ import { cast, deserialize, serialize } from '../src/serializer-facade'; import { getClassName } from '@deepkit/core'; import { entity, t } from '../src/decorator'; -import { Alphanumeric, MaxLength, MinLength, validate, ValidationError } from '../src/validator'; +import { Alphanumeric, MaxLength, MinLength, ValidationError } from '../src/validator'; test('deserializer', () => { class User { @@ -974,3 +975,23 @@ test('naming strategy camel case', () => { expect(res).toEqual({id: 2, posts: [{id: 3, likesCount: 1}, {id: 4, likesCount: 2}]}); } }); + +test('enum union', () => { + enum StatEnginePowerUnit { + Hp = 'hp', + } + + enum StatWeightUnit { + Lbs = 'lbs', + Kg = 'kg', + } + + type StatMeasurementUnit = StatEnginePowerUnit | StatWeightUnit; + const type = typeOf(); + assertType(type, ReflectionKind.union); + expect(type.types.length).toBe(2); + + expect(deserialize(StatWeightUnit.Kg)).toBe(StatWeightUnit.Kg); + expect(deserialize(StatWeightUnit.Lbs)).toBe(StatWeightUnit.Lbs); + expect(deserialize(StatEnginePowerUnit.Hp)).toBe(StatEnginePowerUnit.Hp); +});