@@ -50,10 +50,14 @@ export function encodeToGrc20Json<T extends object, E>(schema: Schema.Schema<T,
50
50
const out : Record < string , unknown > = { } ;
51
51
52
52
for ( const prop of ast . propertySignatures ) {
53
- // TODO: what about optional properties here? usually we use prop.type.types[0] but we don't here?
54
- const result = SchemaAST . getAnnotation < string > ( PropertyIdSymbol ) ( prop . type ) ;
55
- if ( Option . isSome ( result ) ) {
56
- out [ result . value ] = ( value as any ) [ prop . name ] ;
53
+ const propType =
54
+ prop . isOptional && SchemaAST . isUnion ( prop . type )
55
+ ? ( prop . type . types . find ( ( member ) => ! SchemaAST . isUndefinedKeyword ( member ) ) ?? prop . type )
56
+ : prop . type ;
57
+ const result = SchemaAST . getAnnotation < string > ( PropertyIdSymbol ) ( propType ) ;
58
+ const propertyValue : any = ( value as any ) [ prop . name ] ;
59
+ if ( Option . isSome ( result ) && propertyValue !== undefined ) {
60
+ out [ result . value ] = propertyValue ;
57
61
}
58
62
}
59
63
@@ -71,7 +75,11 @@ export function decodeFromGrc20Json<T extends object, E>(
71
75
const out : Record < string , unknown > = { } ;
72
76
73
77
for ( const prop of ast . propertySignatures ) {
74
- const result = SchemaAST . getAnnotation < string > ( PropertyIdSymbol ) ( prop . type ) ;
78
+ const propType =
79
+ prop . isOptional && SchemaAST . isUnion ( prop . type )
80
+ ? ( prop . type . types . find ( ( member ) => ! SchemaAST . isUndefinedKeyword ( member ) ) ?? prop . type )
81
+ : prop . type ;
82
+ const result = SchemaAST . getAnnotation < string > ( PropertyIdSymbol ) ( propType ) ;
75
83
if ( Option . isSome ( result ) ) {
76
84
const grc20Key = result . value ;
77
85
if ( grc20Key in grc20Data && typeof prop . name === 'string' ) {
0 commit comments