TypeScript Support
TypeScript support and type definitions
TypeScript Support
The Cilantro Smart SDK is written in TypeScript and provides comprehensive type definitions for all functions, parameters, and return types.
Installation
TypeScript types are included with the SDK package. No additional installation is required.
npm install cilantro-sdk
Type Definitions
Importing Types
import type {
DeviceKeyPair,
DeviceKeyStorage,
SignerInfo,
Keypair,
EmailSignerOptions,
PhoneSignerOptions,
Wallet,
TransactionResult,
MintResult
} from 'cilantro-sdk/helpers';
Using Types
import { create } from 'cilantro-sdk/wallet';
import type { Wallet } from 'cilantro-sdk/wallet';
async function createWallet(): Promise<Wallet> {
const wallet = await create({
label: 'my-wallet',
tags: ['production']
});
return wallet;
}
Type Safety Examples
Function Parameters
import { sendSOL } from 'cilantro-sdk/wallet';
// TypeScript will enforce correct parameter types
await sendSOL('wallet-id', {
recipientAddress: 'DYw8jCTfwHNRJhhmFcbXvVDTqWMEVFBX6ZKUmG5CNSKK',
amountLamports: 1000000000
});
// TypeScript error: missing required parameter
// await sendSOL('wallet-id', {}); // Error!
// TypeScript error: wrong type
// await sendSOL('wallet-id', {
// recipientAddress: 123, // Error: expected string
// amountLamports: '1000' // Error: expected number
// });
Return Types
import { findAll } from 'cilantro-sdk/wallet';
// TypeScript knows the return type
const wallets: Wallet[] = await findAll();
// TypeScript error: wrong type
// const wallets: string[] = await findAll(); // Error!
Generic Types
import { getAssets } from 'cilantro-sdk/wallet';
// TypeScript infers the return type based on assetType
const solAssets = await getAssets('wallet-id', {
assetType: 'SOL'
}); // Type: SOLAsset[]
const tokenAssets = await getAssets('wallet-id', {
assetType: 'SPL_TOKEN'
}); // Type: SPLTokenAsset[]
Type Guards
import type { Wallet } from 'cilantro-sdk/wallet';
function isActiveWallet(wallet: Wallet): wallet is Wallet & { status: 'active' } {
return wallet.status === 'active';
}
const wallet = await findOne('wallet-id');
if (isActiveWallet(wallet)) {
// TypeScript knows wallet.status is 'active'
console.log('Wallet is active');
}
Custom Type Definitions
You can extend SDK types for your application:
import type { Wallet } from 'cilantro-sdk/wallet';
interface MyWallet extends Wallet {
customField: string;
}
function processWallet(wallet: Wallet): MyWallet {
return {
...wallet,
customField: 'custom-value'
};
}
Type Utilities
Partial Types
import type { Wallet } from 'cilantro-sdk/wallet';
type PartialWallet = Partial<Wallet>;
function updateWallet(id: string, updates: PartialWallet) {
// updates can have any subset of Wallet properties
}
Pick Types
import type { Wallet } from 'cilantro-sdk/wallet';
type WalletSummary = Pick<Wallet, 'id' | 'address' | 'label'>;
function getWalletSummary(wallet: Wallet): WalletSummary {
return {
id: wallet.id,
address: wallet.address,
label: wallet.label
};
}