TypeScript Setup

Tourista is built with TypeScript and provides comprehensive type definitions. This guide shows how to leverage TypeScript for better developer experience and type safety.

Basic Type Usage

Importing Types

import {
  TourConfig,
  TourStep,
  CardProps,
  TourContext,
  BaseTourEvent,
  OverlayStyles,
  CardPositioning,
} from 'Tourista';

Typing Tour Configurations

import { TourConfig } from 'Tourista';

// Method 1: Basic typing (less type inference)
const basicTour: TourConfig = {
  id: 'welcome-tour',
  steps: [
    {
      id: 'step1',
      page: '/',
      targetElement: '#button',
      title: 'Welcome',
      content: 'Get started here',
    },
  ],
  allowPageNavigation: true,
  allowSkip: true,
};

// Method 2: Using 'as const satisfies' (recommended)
// Provides both type checking AND literal type inference
const advancedTour = {
  id: 'welcome-tour',
  steps: [
    {
      id: 'step1',
      page: '/',
      targetElement: '#button',
      title: 'Welcome',
      content: 'Get started here',
    },
  ],
  allowPageNavigation: true,
  allowSkip: true,
} as const satisfies TourConfig;

// With 'as const satisfies', TypeScript knows:
// - The exact tour ID is 'welcome-tour' (not just string)
// - The exact step IDs are literals
// - Better type inference for ExtractStates and other utilities

Typing Tour Steps

Real-World Usage Pattern

Here's how to set up a fully type-safe tour with helpers and exports:

Using the Typed Tour

Advanced Type Patterns

Custom Card Component Types

Typing Configuration Options

Type-Safe Event Handling

Type Inference

Extracting State Types

Extracting Tour IDs

Best Practices

  1. Use as const satisfies TourConfig: This pattern gives you both type checking and literal type inference

  2. Export typed hooks: Create tour-specific versions of useTour for better DX

  3. Export state types: Make ExtractStates<typeof tourConfig> available for components

  4. Organize tours in separate files: Keep each tour configuration in its own module

  5. Use step-level options: Configure canPrev, canSkip at the step level for fine control

Common Patterns

Creating a Tour Module

Next Steps

Last updated