-
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
4e85569
commit b8ea289
Showing
4 changed files
with
73 additions
and
47 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,43 +1,25 @@ | ||
import Type, { type Static } from '@sinclair/typebox' | ||
import Type, { FormatRegistry, type Static } from '@sinclair/typebox' | ||
import { Value } from '@sinclair/typebox/value' | ||
import { TypeCompiler } from '@sinclair/typebox/compiler' | ||
|
||
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() }) | ||
const A = Type.Transform(Type.String()) | ||
.Decode((value: string) => new Date(value)) | ||
.Encode((value: Date) => value.toISOString()) | ||
|
||
const B = Type.Union([Type.Object({ date: A }), Type.Number()]) | ||
const T1 = Type.Transform(B) | ||
.Decode((value) => { | ||
// expect date | ||
return value | ||
}) | ||
.Encode((value) => value) | ||
const T2 = Type.Transform(B) | ||
.Decode((value) => { | ||
// expect number | ||
console.log(value) | ||
return value | ||
}) | ||
.Encode((value) => value) | ||
|
||
const R1 = Value.Decode(T1, { date: new Date().toISOString() }) | ||
const R2 = Value.Decode(T2, 1) |
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
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
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