Skip to main content
The Paystack TypeScript SDK provides comprehensive type safety through a combination of TypeScript’s compile-time checking and Zod’s runtime validation. This dual-layer approach ensures your code is both type-safe and validates data at runtime.

Why Type Safety Matters

Type safety prevents entire classes of bugs before they reach production:

Catch Errors Early

TypeScript catches type mismatches during development, not in production.

Better IntelliSense

Your IDE provides accurate autocomplete and inline documentation.

Refactor Safely

Change APIs with confidence knowing TypeScript will flag breaking changes.

Runtime Validation

Zod validates data at runtime, protecting against invalid API responses.

TypeScript Benefits

Full Type Coverage

Every SDK method has complete TypeScript definitions:

IntelliSense Support

Your IDE provides intelligent code completion:
Hover over any method or property in your IDE to see detailed documentation extracted from JSDoc comments.

Compile-Time Error Detection

TypeScript catches mistakes before runtime:

Zod Runtime Validation

While TypeScript provides compile-time safety, Zod validates data at runtime. This is crucial for:
  • API responses that might not match expected schemas
  • User input that bypasses TypeScript
  • Third-party data from webhooks or external sources

How Zod Works

Zod schemas define the shape and validation rules for data. Here’s an example from src/zod/transaction.ts:15-85:
src/zod/transaction.ts

Input Validation

Zod automatically validates all inputs before API requests:

Output Validation

Zod also validates API responses to ensure they match the expected schema:
src/main/transaction.ts
This protects your application from unexpected API changes or malformed responses.

Type Inference

Zod schemas can be converted to TypeScript types using z.infer. From src/zod/transaction.ts:1503-1517:
src/zod/transaction.ts
This ensures TypeScript types and runtime validation stay in sync.

Importing Types

You can import types for use in your application:

Common Types

The SDK exports many reusable types from src/zod/index.ts:228-237:
src/zod/index.ts
Import and use these in your application:

Validation Schemas

Currency Validation

The SDK validates currency codes from src/zod/index.ts:40-49:
src/zod/index.ts
Usage:

Email Validation

Emails are validated using Zod’s built-in email validator:

URL Validation

Callback URLs are validated as proper URLs:

Custom Object Validation

From src/zod/index.ts:127-147, the SDK validates complex nested objects:
src/zod/index.ts

Advanced Type Patterns

Discriminated Unions

Handle different response types safely:

Generic Helpers

Create type-safe wrappers:

Utility Types

Use TypeScript utility types with SDK types:

Schema Composition

Zod schemas can be composed and extended:

Safe Parsing

Manually validate data using Zod schemas:

Best Practices

Trust the Types

Let TypeScript guide you. If something doesn’t type-check, there’s usually a good reason.

Import Types

Import types for reusability and consistency across your codebase.

Handle Validation Errors

Always check for validation errors in the result object.

Use Strict Mode

Enable strict TypeScript settings in tsconfig.json:

Platform Support

The SDK is fully typed for all JavaScript runtimes:

TypeScript Configuration

Recommended tsconfig.json for best experience:
tsconfig.json

Next Steps

API Reference

Explore detailed API documentation with type information

Error Handling

Learn how to handle validation and runtime errors