@@ -24,6 +24,7 @@ parent: Modules
2424 - [ NullOr] ( #nullor )
2525 - [ OneLiteral] ( #oneliteral )
2626 - [ Struct] ( #struct )
27+ - [ StructOptions (interface)] ( #structoptions-interface )
2728 - [ Tuple] ( #tuple )
2829 - [ UndefinedOr] ( #undefinedor )
2930 - [ Union] ( #union )
@@ -37,8 +38,8 @@ parent: Modules
3738
3839## ByteArray
3940
40- ByteArray schema that transforms hex string to Data.ByteArray for PlutusData .
41- This enables withSchema compatibility by transforming from hex string to Uint8Array .
41+ ByteArray schema for PlutusData hex strings .
42+ Since Data.ByteArray is now hex string based, this is just an alias to it .
4243
4344** Signature**
4445
@@ -108,11 +109,7 @@ Added in v2.0.0
108109**Signature**
109110
110111` ` ` ts
111- export interface ByteArray
112- extends Schema .transform <
113- Schema .SchemaClass <Uint8Array <ArrayBufferLike >, Uint8Array <ArrayBufferLike >, never >,
114- typeof Schema .String
115- > {}
112+ export interface ByteArray extends Schema .Schema <string , string , never > {}
116113```
117114
118115## Integer (interface)
@@ -180,16 +177,71 @@ export declare const OneLiteral: <Single extends Exclude<SchemaAST.LiteralValue,
180177## Struct
181178
182179Creates a schema for struct types using Plutus Data Constructor
183- Objects are represented as a constructor with index 0 and fields as an array
180+ Objects are represented as a constructor with index (default 0) and fields as an array
184181
185182**Signature**
186183
187184` ` ` ts
188- export declare const Struct: <Fields extends Schema .Struct .Fields >(fields : Fields ) => Struct <Fields >
185+ export declare const Struct: <Fields extends Schema .Struct .Fields >(
186+ fields : Fields ,
187+ options ? : StructOptions
188+ ) => Struct <Fields >
189+ ` ` `
190+
191+ **Example**
192+
193+ ` ` ` typescript
194+ import { TSchema } from " @evolution-sdk/evolution"
195+
196+ // Default: nested in Union, index 0
197+ TSchema .Struct ({ name: TSchema .ByteArray , age: TSchema .Integer })
198+ ```
199+
200+ ** Example**
201+
202+ ``` typescript
203+ import { TSchema } from " @evolution-sdk/evolution"
204+
205+ // Flat union variants with custom indices
206+ TSchema .Struct ({ amount: TSchema .Integer }, { index: 121 , flat: true })
207+ TSchema .Struct ({ amount: TSchema .Integer }, { index: 122 , flat: true })
208+ ```
209+
210+ ** Example**
211+
212+ ``` typescript
213+ import { TSchema } from " @evolution-sdk/evolution"
214+
215+ // Custom index but stay nested (advanced use case)
216+ TSchema .Struct ({ data: TSchema .Integer }, { index: 10 , flat: false })
189217```
190218
191219Added in v2.0.0
192220
221+ ## StructOptions (interface)
222+
223+ Options for Struct schema
224+
225+ ** Signature**
226+
227+ ``` ts
228+ export interface StructOptions {
229+ /**
230+ * Custom Constr index for this struct (default: 0)
231+ * Useful when creating union variants with specific indices
232+ */
233+ index? : number
234+ /**
235+ * When used in a Union, controls whether this Struct should be "flattened" (unwrapped).
236+ * - true: Encodes as Constr(index, [fields]) directly
237+ * - false: Encodes as Constr(unionPos, [Constr(index, [fields])]) (nested)
238+ *
239+ * Default: true when index is specified, false otherwise
240+ */
241+ flat? : boolean
242+ }
243+ ```
244+
193245## Tuple
194246
195247Creates a schema for tuple types - just passes through to Schema.Tuple directly
@@ -221,14 +273,53 @@ Added in v2.0.0
221273## Union
222274
223275Creates a schema for union types using Plutus Data Constructor
224- Unions are represented as a constructor with index 0 and fields as an array
276+ Unions are represented as a constructor with index 0, 1, 2... and fields as an array
277+
278+ Members marked with flat: true will be encoded directly using their index
279+ instead of being wrapped in an additional Constr layer.
225280
226281**Signature**
227282
228283` ` ` ts
229284export declare const Union: <Members extends ReadonlyArray <Schema .Schema .Any >>(... members : Members ) => Union <Members >
230285` ` `
231286
287+ **Example**
288+
289+ ` ` ` typescript
290+ import { TSchema } from " @evolution-sdk/evolution"
291+
292+ // Standard union with auto indices (nested)
293+ TSchema .Union (TSchema .Struct ({ a: TSchema .Integer }), TSchema .Struct ({ b: TSchema .Integer }))
294+ // Encodes to: Constr(0, [Constr(0, [a])]) or Constr(1, [Constr(0, [b])])
295+ ```
296+
297+ ** Example**
298+
299+ ``` typescript
300+ import { TSchema } from " @evolution-sdk/evolution"
301+
302+ // Union with flat Structs (single-level encoding)
303+ TSchema .Union (
304+ TSchema .Struct ({ amount: TSchema .Integer }, { index: 121 , flat: true }),
305+ TSchema .Struct ({ amount: TSchema .Integer }, { index: 122 , flat: true })
306+ )
307+ // Encodes to: Constr(121, [amount]) or Constr(122, [amount]) - single level!
308+ ```
309+
310+ ** Example**
311+
312+ ``` typescript
313+ import { TSchema } from " @evolution-sdk/evolution"
314+
315+ // Mixed union: some nested, some flat
316+ TSchema .Union (
317+ TSchema .Struct ({ a: TSchema .Integer }), // nested, auto index 0
318+ TSchema .Struct ({ b: TSchema .Integer }, { flat: true }), // flat, auto index 1
319+ TSchema .Struct ({ c: TSchema .Integer }, { index: 100 , flat: true }) // flat, custom index 100
320+ )
321+ ```
322+
232323Added in v2.0.0
233324
234325## compose
0 commit comments