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/reactConfigure 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
| Feature | Description |
|---|---|
| Complete type coverage | Every API surface is fully typed |
| IDE autocompletion | IntelliSense shows methods, parameters, return types |
| Compile-time validation | Catch errors before runtime |
| JSDoc documentation | Usage examples appear in your editor |
| Cardano primitives | UTxO, Asset, Address, Datum, Redeemer types |
| Framework agnostic | Works 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
| Feature | Benefit |
|---|---|
| Hover documentation | See type definitions inline |
| Autocompletion | Discover methods as you type |
| Parameter hints | Know required arguments |
| Go to Definition | Jump to type sources |
| Find All References | Track usage across codebase |
| Safe refactoring | Rename symbols with confidence |
Recommended configuration
{
"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
| Benefit | Details |
|---|---|
| Prevent costly mistakes | Catch errors before transactions reach mainnet |
| Faster development | IDE guidance instead of documentation lookup |
| Team collaboration | Types serve as living documentation |
| Manage complexity | Encode Cardano's rules in the type system |
Related resources
| Resource | Link |
|---|---|
| Transaction builder | /resources/solutions/transaction-builder |
| React components | /resources/solutions/react-components |
| Wallet integration | /resources/solutions/wallet-integration |
| Learning curve | /resources/challenges/learning-curve |
| Getting started | /getting-started |