Skip to content

Commit

Permalink
Version and Static Tests
Browse files Browse the repository at this point in the history
  • Loading branch information
sinclairzx81 committed Nov 2, 2023
1 parent 2bba92a commit dc70fa9
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 3 deletions.
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@sinclair/typebox",
"version": "0.31.21",
"version": "0.31.22",
"description": "JSONSchema Type Builder with Static Type Resolution for TypeScript",
"keywords": [
"typescript",
Expand Down
1 change: 1 addition & 0 deletions src/typebox.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2915,6 +2915,7 @@ export class JsonTypeBuilder extends TypeBuilder {
}
/** `[Json]` Creates a Enum type */
public Enum<V extends TEnumValue, T extends Record<TEnumKey, V>>(item: T, options: SchemaOptions = {}): TEnum<T> {
if (ValueGuard.IsUndefined(item)) return this.Throw('Enum undefined or empty')
// prettier-ignore
const values1 = Object.getOwnPropertyNames(item).filter((key) => isNaN(key as any)).map((key) => item[key]) as T[keyof T][]
const values2 = [...new Set(values1)]
Expand Down
48 changes: 48 additions & 0 deletions test/static/record.ts
Original file line number Diff line number Diff line change
Expand Up @@ -128,3 +128,51 @@ import { Type, Static } from '@sinclair/typebox'
const I = Type.Intersect([R, T])
Expect(I).ToStatic<Record<`key${number}`, number> & { x: number; y: number }>()
}
{
// expect T as Object
enum E {
A,
B,
C,
}
const T = Type.Record(Type.Enum(E), Type.Number())
Expect(T).ToStatic<{
0: number
1: number
2: number
}>
}
{
// expect T as Partial Object
enum E {
A,
B,
C,
}
const T = Type.Partial(Type.Record(Type.Enum(E), Type.Number()))
Expect(T).ToStatic<{
0?: number
1?: number
2?: number
}>
}
{
// expect T to support named properties
enum E {
A = 'A',
B = 'B',
C = 'C',
}
const T = Type.Record(Type.Enum(E), Type.Number())
Expect(T).ToStatic<{
A: number
B: number
C: number
}>
}
{
// expect T to support named properties
enum E {}
const T = Type.Record(Type.Enum(E), Type.Number())
Expect(T).ToStatic<{}>
}

0 comments on commit dc70fa9

Please sign in to comment.