|
| 1 | +import { Assert } from 'test' |
| 2 | +import * as Type from 'typebox' |
| 3 | + |
| 4 | +const Test = Assert.Context('Type.Base.Evaluate') |
| 5 | + |
| 6 | +// ------------------------------------------------------------------ |
| 7 | +// Base Type Intersection with Evaluate |
| 8 | +// ------------------------------------------------------------------ |
| 9 | +class TId extends Type.Base<string> { |
| 10 | + public override Check(value: unknown): value is string { |
| 11 | + return typeof value === 'string' |
| 12 | + } |
| 13 | + public override Clone(): TId { |
| 14 | + return new TId() |
| 15 | + } |
| 16 | +} |
| 17 | + |
| 18 | +Test('Should evaluate intersect of same Base class instances as equal 1', () => { |
| 19 | + const schemaNonEvaluated = Type.Intersect([ |
| 20 | + new TId(), |
| 21 | + new TId(), |
| 22 | + ]) |
| 23 | + const schemaEvaluated = Type.Evaluate(schemaNonEvaluated) |
| 24 | + |
| 25 | + // Should not be never |
| 26 | + Assert.IsFalse(Type.IsNever(schemaEvaluated)) |
| 27 | + // Should be TId (Base type) |
| 28 | + Assert.IsTrue(Type.IsBase(schemaEvaluated)) |
| 29 | +}) |
| 30 | + |
| 31 | +Test('Should evaluate intersect of same Base class instances as equal 2', () => { |
| 32 | + const T1 = new TId() |
| 33 | + const T2 = new TId() |
| 34 | + const schemaNonEvaluated = Type.Intersect([T1, T2]) |
| 35 | + const schemaEvaluated = Type.Evaluate(schemaNonEvaluated) |
| 36 | + |
| 37 | + // Should not be never |
| 38 | + Assert.IsFalse(Type.IsNever(schemaEvaluated)) |
| 39 | + // Should be TId (Base type) |
| 40 | + Assert.IsTrue(Type.IsBase(schemaEvaluated)) |
| 41 | + // Should be instance of TId |
| 42 | + Assert.IsTrue(schemaEvaluated instanceof TId) |
| 43 | +}) |
| 44 | + |
| 45 | +Test('Should evaluate intersect of different Base class instances as never', () => { |
| 46 | + class TId2 extends Type.Base<number> { |
| 47 | + public override Check(value: unknown): value is number { |
| 48 | + return typeof value === 'number' |
| 49 | + } |
| 50 | + public override Clone(): TId2 { |
| 51 | + return new TId2() |
| 52 | + } |
| 53 | + } |
| 54 | + |
| 55 | + const schemaNonEvaluated = Type.Intersect([ |
| 56 | + new TId(), |
| 57 | + new TId2(), |
| 58 | + ]) |
| 59 | + const schemaEvaluated = Type.Evaluate(schemaNonEvaluated) |
| 60 | + |
| 61 | + // Should be never |
| 62 | + Assert.IsTrue(Type.IsNever(schemaEvaluated)) |
| 63 | +}) |
| 64 | + |
| 65 | +Test('Should evaluate single Base type in intersect', () => { |
| 66 | + const schemaNonEvaluated = Type.Intersect([ |
| 67 | + new TId(), |
| 68 | + ]) |
| 69 | + const schemaEvaluated = Type.Evaluate(schemaNonEvaluated) |
| 70 | + |
| 71 | + // Should not be never |
| 72 | + Assert.IsFalse(Type.IsNever(schemaEvaluated)) |
| 73 | + // Should be TId (Base type) |
| 74 | + Assert.IsTrue(Type.IsBase(schemaEvaluated)) |
| 75 | + // Should be instance of TId |
| 76 | + Assert.IsTrue(schemaEvaluated instanceof TId) |
| 77 | +}) |
| 78 | + |
| 79 | +Test('Should evaluate intersect of Base type with Object', () => { |
| 80 | + const schemaNonEvaluated = Type.Intersect([ |
| 81 | + new TId(), |
| 82 | + Type.Object({ foo: Type.String() }), |
| 83 | + ]) |
| 84 | + const schemaEvaluated = Type.Evaluate(schemaNonEvaluated) |
| 85 | + |
| 86 | + // Should be an object with foo property |
| 87 | + Assert.IsTrue(Type.IsObject(schemaEvaluated)) |
| 88 | + Assert.IsTrue('foo' in schemaEvaluated.properties) |
| 89 | + Assert.IsTrue(Type.IsString(schemaEvaluated.properties.foo)) |
| 90 | +}) |
| 91 | + |
| 92 | +Test('Should evaluate triple intersect of same Base class instances', () => { |
| 93 | + const schemaNonEvaluated = Type.Intersect([ |
| 94 | + new TId(), |
| 95 | + new TId(), |
| 96 | + new TId(), |
| 97 | + ]) |
| 98 | + const schemaEvaluated = Type.Evaluate(schemaNonEvaluated) |
| 99 | + |
| 100 | + // Should not be never |
| 101 | + Assert.IsFalse(Type.IsNever(schemaEvaluated)) |
| 102 | + // Should be TId (Base type) |
| 103 | + Assert.IsTrue(Type.IsBase(schemaEvaluated)) |
| 104 | + // Should be instance of TId |
| 105 | + Assert.IsTrue(schemaEvaluated instanceof TId) |
| 106 | +}) |
| 107 | + |
| 108 | +// Type-level check (from the original issue) |
| 109 | +Test('Should preserve type-level types correctly', () => { |
| 110 | + const schemaNonEvaluated = Type.Intersect([ |
| 111 | + new TId(), |
| 112 | + new TId(), |
| 113 | + ]) |
| 114 | + type TNonEvaluated = Type.StaticDecode<typeof schemaNonEvaluated> |
| 115 | + |
| 116 | + const schemaEvaluated = Type.Evaluate(schemaNonEvaluated) |
| 117 | + type TEvaluated = Type.StaticDecode<typeof schemaEvaluated> |
| 118 | + |
| 119 | + // Runtime checks |
| 120 | + Assert.IsFalse(Type.IsNever(schemaEvaluated)) |
| 121 | + Assert.IsTrue(Type.IsBase(schemaEvaluated)) |
| 122 | + |
| 123 | + // Type-level assertions (compile-time checks) |
| 124 | + const _nonEval: TNonEvaluated = 'test' // should be string |
| 125 | + const _eval: TEvaluated = 'test' // should be string, not never |
| 126 | + |
| 127 | + Assert.IsTrue(true) // If compiles, test passes |
| 128 | +}) |
0 commit comments