Mesh LogoMesh
ResourcesSolutions

TypeScript-First Development

Build Cardano dApps with complete type safety using Mesh SDK's comprehensive TypeScript definitions.

Mesh SDK provides complete TypeScript definitions for all Cardano APIs. Catch errors at compile time, explore APIs through autocompletion, and build with confidence.

Quick start

npm install @meshsdk/core @meshsdk/react

Configure TypeScript

{
  "compilerOptions": {
    "strict": true,
    "target": "ES2020",
    "module": "ESNext",
    "moduleResolution": "bundler"
  }
}

Build with types

import { MeshTxBuilder } from "@meshsdk/core";
import { MeshCardanoBrowserWallet } from "@meshsdk/wallet";
import type { UTxO, Asset } from "@meshsdk/core";

const wallet = await MeshCardanoBrowserWallet.enable("eternl");
const utxos: UTxO[] = await wallet.getUtxosMesh();
const changeAddress: string = await wallet.getChangeAddressBech32();

const txBuilder = new MeshTxBuilder();
const unsignedTx = await txBuilder
  .txOut("addr_test1...", [{ unit: "lovelace", quantity: "5000000" }])
  .changeAddress(changeAddress)
  .selectUtxosFrom(utxos)
  .complete();

const signedTx = await wallet.signTx(unsignedTx);

Features

FeatureDescription
Complete type coverageEvery API surface is fully typed
IDE autocompletionIntelliSense shows methods, parameters, return types
Compile-time validationCatch errors before runtime
JSDoc documentationUsage examples appear in your editor
Cardano primitivesUTxO, Asset, Address, Datum, Redeemer types
Framework agnosticWorks with React, Vue, Svelte, Node.js

Type-safe patterns

Transaction building

import { MeshTxBuilder } from "@meshsdk/core";
import { MeshCardanoBrowserWallet } from "@meshsdk/wallet";
import type { UTxO, Asset } from "@meshsdk/core";

const wallet = await MeshCardanoBrowserWallet.enable("eternl");
const utxos: UTxO[] = await wallet.getUtxosMesh();
const changeAddress: string = await wallet.getChangeAddressBech32();

const txBuilder = new MeshTxBuilder();

// TypeScript enforces string for address, Asset[] for amount
txBuilder.txOut(
  "addr_test1qz...",
  [{ unit: "lovelace", quantity: "5000000" }]
);

// Send native tokens
txBuilder.txOut("addr_test1qz...", [
  {
    unit: "policy_id_hex" + "asset_name_hex",
    quantity: "100"
  }
]);

const unsignedTx: string = await txBuilder
  .changeAddress(changeAddress)
  .selectUtxosFrom(utxos)
  .complete();

Wallet APIs

import { MeshCardanoBrowserWallet } from "@meshsdk/wallet";
import type { UTxO, Asset } from "@meshsdk/core";

const wallet = await MeshCardanoBrowserWallet.enable("eternl");

// Typed return values
const address: string = await wallet.getChangeAddressBech32();
const networkId: number = await wallet.getNetworkId();
const utxos: UTxO[] = await wallet.getUtxosMesh();
const balance: Asset[] = await wallet.getBalanceMesh();

// UTxO type includes all fields
utxos.forEach(utxo => {
  console.log(utxo.input.txHash);      // string
  console.log(utxo.input.outputIndex); // number
  console.log(utxo.output.address);    // string
  console.log(utxo.output.amount);     // Asset[]
});

Cardano data types

import type {
  UTxO,
  Asset,
  PlutusScript,
  NativeScript,
  Data,
  Recipient
} from "@meshsdk/core";

// Asset type
const asset: Asset = {
  unit: "lovelace",
  quantity: "5000000"
};

// UTxO type
const utxo: UTxO = {
  input: {
    txHash: "abc123...",
    outputIndex: 0
  },
  output: {
    address: "addr_test1...",
    amount: [asset],
    dataHash: undefined,
    plutusData: undefined,
    scriptRef: undefined
  }
};

// Plutus script type
const script: PlutusScript = {
  version: "V2",
  code: "590abc..."
};

Smart contracts

import { MeshTxBuilder, mConStr0, mConStr1 } from "@meshsdk/core";
import type { Data } from "@meshsdk/core";

// Type-safe datum construction
const datum: Data = mConStr0([
  "owner_address",
  1000000n,
  mConStr1(["metadata"])
]);

// Typed redeemer
const redeemer: Data = mConStr0([]);

// Transaction with typed contract interaction
const txBuilder = new MeshTxBuilder();
txBuilder
  .txOut(scriptAddress, [{ unit: "lovelace", quantity: "10000000" }])
  .txOutInlineDatumValue(datum);

Compile-time error catching

TypeScript catches mistakes before you run code:

import { MeshTxBuilder } from "@meshsdk/core";

const txBuilder = new MeshTxBuilder();

// Error: Argument of type 'number' is not assignable to parameter of type 'string'
txBuilder.txOut("addr_test1...", [{ unit: "lovelace", quantity: 5000000 }]);

// Correct: quantity must be a string
txBuilder.txOut("addr_test1...", [{ unit: "lovelace", quantity: "5000000" }]);

IDE features

FeatureBenefit
Hover documentationSee type definitions inline
AutocompletionDiscover methods as you type
Parameter hintsKnow required arguments
Go to DefinitionJump to type sources
Find All ReferencesTrack usage across codebase
Safe refactoringRename symbols with confidence

{
  "compilerOptions": {
    "strict": true,
    "noUncheckedIndexedAccess": true,
    "exactOptionalPropertyTypes": true,
    "target": "ES2020",
    "module": "ESNext",
    "moduleResolution": "bundler",
    "esModuleInterop": true,
    "skipLibCheck": true
  }
}

Working with BigInt

// BigInt for large values
const lovelace = 5000000n;
const quantity = BigInt("1000000000000");

// Convert to string for transaction methods
txBuilder.txOut(address, [{ unit: "lovelace", quantity: lovelace.toString() }]);

Why TypeScript for Cardano

BenefitDetails
Prevent costly mistakesCatch errors before transactions reach mainnet
Faster developmentIDE guidance instead of documentation lookup
Team collaborationTypes serve as living documentation
Manage complexityEncode Cardano's rules in the type system

On this page