-
Notifications
You must be signed in to change notification settings - Fork 163
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
3ef8d3a
commit 4e85569
Showing
2 changed files
with
61 additions
and
41 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,39 +1,43 @@ | ||
import Type from '@sinclair/typebox' | ||
import Type, { type Static } from '@sinclair/typebox' | ||
import { Value } from '@sinclair/typebox/value' | ||
import { TypeCompiler } from '@sinclair/typebox/compiler' | ||
|
||
const X = TypeCompiler.Code( | ||
Type.Object({ | ||
x: Type.String(), | ||
y: Type.BigInt(), | ||
z: Type.Array(Type.Null()), | ||
}), | ||
) | ||
|
||
console.log(X) | ||
|
||
const A = Type.Transform(Type.String()) | ||
.Decode((value: string): Date => new Date(value)) | ||
.Encode((value: Date): string => value.toISOString()) | ||
|
||
const B = Type.Union([Type.Object({ date: A }), Type.Number()]) | ||
|
||
const C = Type.Transform(B) | ||
.Decode((value) => { | ||
console.log(value) | ||
if (typeof value === 'object') { | ||
// date is supposed to be a Date according to type inference, but it's a string when it's logged. | ||
console.log('union', typeof value.date) // string | ||
} | ||
return value | ||
}) | ||
.Encode((o) => o) | ||
|
||
const D = Type.Transform(Type.Object({ date: A })) | ||
.Decode((o) => { | ||
console.log('simple', typeof o.date) // object | ||
return o | ||
}) | ||
.Encode((o) => o) | ||
|
||
const R1 = Value.Decode(C, [], { date: new Date().toISOString() }) | ||
const A = Type.Number({}) | ||
|
||
// console.log(AA) | ||
|
||
// const X = TypeCompiler.Code( | ||
// Type.Object({ | ||
// x: Type.String(), | ||
// y: Type.BigInt(), | ||
// z: Type.Array(Type.Null()), | ||
// }), | ||
// ) | ||
|
||
// console.log(X) | ||
|
||
// const A = Type.Transform(Type.String()) | ||
// .Decode((value: string): Date => new Date(value)) | ||
// .Encode((value: Date): string => value.toISOString()) | ||
|
||
// const B = Type.Union([Type.Object({ date: A }), Type.Number()]) | ||
|
||
// const C = Type.Transform(B) | ||
// .Decode((value) => { | ||
// console.log(value) | ||
// if (typeof value === 'object') { | ||
// // date is supposed to be a Date according to type inference, but it's a string when it's logged. | ||
// console.log('union', typeof value.date) // string | ||
// } | ||
// return value | ||
// }) | ||
// .Encode((o) => o) | ||
|
||
// const D = Type.Transform(Type.Object({ date: A })) | ||
// .Decode((o) => { | ||
// console.log('simple', typeof o.date) // object | ||
// return o | ||
// }) | ||
// .Encode((o) => o) | ||
|
||
// const R1 = Value.Decode(C, [], { date: new Date().toISOString() }) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters