<p>Lucid also has a minimal example for typed data <a href="https://github.com/spacebudz/lucid/blob/main/src/examples/typed_data.ts" rel="nofollow noreferrer">here</a>.</p>
<p>The function <code>Data.to</code> has the following signature (as seen from <a href="https://github.com/spacebudz/lucid/blob/main/src/plutus/data.ts#L264" rel="nofollow noreferrer">here</a>):</p>
<pre class="lang-typescript prettyprint-override"><code>/**
 * Convert PlutusData to Cbor encoded data.\
 * Or apply a shape and convert the provided data struct to Cbor encoded data.
 */
function to&lt;T = Data&gt;(data: T, shape?: Json): Datum | Redeemer {
</code></pre>
<p>Where the type <code>Data</code> which is a wrapper around your usual Plutus primitives (see <a href="https://github.com/spacebudz/lucid/blob/main/src/plutus/data.ts#L31" rel="nofollow noreferrer">this</a>).</p>
<p>Lucid defines higher types <a href="https://github.com/spacebudz/lucid/blob/main/src/plutus/data.ts#L39" rel="nofollow noreferrer">here</a> which correspond to their serialized version as one would expect.</p>
<p>Now seeing the definition of function <code>to</code> from Lucid's source code, you'll realize that it first wants to obtain this <code>Data</code> (<a href="https://github.com/spacebudz/lucid/blob/main/src/plutus/data.ts#L305" rel="nofollow noreferrer">this</a> line) and then convert it for corresponding plutus types (the <code>serialize</code> function) in <a href="https://github.com/dcSpark/cardano-multiplatform-lib" rel="nofollow noreferrer">Cardano Multiplatform Library</a>.
Now either you can play with so called raw/untyped datum (like in <a href="https://github.com/spacebudz/lucid/blob/main/src/examples/matching_keyhash.ts#L59" rel="nofollow noreferrer">this</a> example) in which case your <code>T</code> above will simply be that <code>Data</code> or you can play at a bit higher type level (like you would do when writing Plutus contracts) where lucid helps you get this <code>T</code> using <code>Data.Static</code> as seen <a href="https://github.com/spacebudz/lucid/blob/main/src/examples/typed_data.ts#L19" rel="nofollow noreferrer">here</a>. The second parameter as you would understand by now, is not at type level and is for <a href="https://github.com/spacebudz/lucid/blob/main/src/examples/typed_data.ts#L13" rel="nofollow noreferrer">this</a> - the shape of your structure. Also <code>Json</code> is essentially <code>any</code> type.</p>

CC BY-SA 4.0
2023-03-29T07:14:59.047Z
2023-03-29T07:14:59.047Z
Sourabh
<p>At the bottom of this post are lines 52-68 of file <code>lucid-vesting</code> of week 4 (PPP fourth iteration).  I am having a hard time understanding line 60:</p>
<pre class="lang-typescript prettyprint-override"><code>const dtm: Datum = Data.to&lt;VestingDatum&gt;(datum,VestingDatum);
</code></pre>
<p>Why are there two instances of <code>VestingDatum</code>?  It seems that the command <code>to</code> is converting <code>datum</code> to <code>VestingDatum</code>, but <code>datum</code> had already been declared to be of type <code>VestingDatum</code>.</p>
<pre class="lang-typescript prettyprint-override"><code>// Creating a datum with a beneficiary and deadline
const datum: VestingDatum = {
    beneficiary: beneficiaryPKH,
    deadline: deadlinePosIx,
};

// An asynchronous function that sends an amount of Lovelace to the script with the above datum.
async function vestFunds(amount: bigint): Promise&lt;TxHash&gt; {
    const dtm: Datum = Data.to&lt;VestingDatum&gt;(datum,VestingDatum);
    const tx = await lucid
      .newTx()
      .payToContract(vestingAddress, { inline: dtm }, { lovelace: amount })
      .complete();
    const signedTx = await tx.sign().complete();
    const txHash = await signedTx.submit();
    return txHash
}
</code></pre>

<plutus-pioneer-program><typescript>
Understanding a datum declaration in typescript
# Understanding a datum declaration in typescript

(Asked by: username_0 on 2023-03-28)

At the bottom of this post are lines 52-68 of file `lucid-vesting` of week 4 (PPP fourth iteration). I am having a hard time understanding line 60:

```typescript
const dtm: Datum = Data.to<VestingDatum>(datum,VestingDatum);

```

Why are there two instances of `VestingDatum`? It seems that the command `to` is converting `datum` to `VestingDatum`, but `datum` had already been declared to be of type `VestingDatum`.

```typescript
// Creating a datum with a beneficiary and deadline
const datum: VestingDatum = {
    beneficiary: beneficiaryPKH,
    deadline: deadlinePosIx,
};

// An asynchronous function that sends an amount of Lovelace to the script with the above datum.
async function vestFunds(amount: bigint): Promise<TxHash> {
    const dtm: Datum = Data.to<VestingDatum>(datum,VestingDatum);
    const tx = await lucid
      .newTx()
      .payToContract(vestingAddress, { inline: dtm }, { lovelace: amount })
      .complete();
    const signedTx = await tx.sign().complete();
    const txHash = await signedTx.submit();
    return txHash
}

```

**Tags:** plutus-pioneer-program, typescript


---

## 1 answers

### Answer by username_1 on 2023-03-29. Score: 4 | Accepted answer

Lucid also has a minimal example for typed data [here](https://github.com/spacebudz/lucid/blob/main/src/examples/typed_data.ts).

The function `Data.to` has the following signature (as seen from [here](https://github.com/spacebudz/lucid/blob/main/src/plutus/data.ts#L264)):

```typescript
/**
 * Convert PlutusData to Cbor encoded data.\
 * Or apply a shape and convert the provided data struct to Cbor encoded data.
 */
function to<T = Data>(data: T, shape?: Json): Datum | Redeemer {

```

Where the type `Data` which is a wrapper around your usual Plutus primitives (see [this](https://github.com/spacebudz/lucid/blob/main/src/plutus/data.ts#L31)).

Lucid defines higher types [here](https://github.com/spacebudz/lucid/blob/main/src/plutus/data.ts#L39) which correspond to their serialized version as one would expect.

Now seeing the definition of function `to` from Lucid's source code, you'll realize that it first wants to obtain this `Data` ([this](https://github.com/spacebudz/lucid/blob/main/src/plutus/data.ts#L305) line) and then convert it for corresponding plutus types (the `serialize` function) in [Cardano Multiplatform Library](https://github.com/dcSpark/cardano-multiplatform-lib).
Now either you can play with so called raw/untyped datum (like in [this](https://github.com/spacebudz/lucid/blob/main/src/examples/matching_keyhash.ts#L59) example) in which case your `T` above will simply be that `Data` or you can play at a bit higher type level (like you would do when writing Plutus contracts) where lucid helps you get this `T` using `Data.Static` as seen [here](https://github.com/spacebudz/lucid/blob/main/src/examples/typed_data.ts#L19). The second parameter as you would understand by now, is not at type level and is for [this](https://github.com/spacebudz/lucid/blob/main/src/examples/typed_data.ts#L13) - the shape of your structure. Also `Json` is essentially `any` type.


---

