A comprehensive interactive application demonstrating advanced TypeScript union types, discriminated unions, and conditional types through an engaging RPG-style interface.
- ๐ Discriminated Union Types - Character types with type-safe narrowing
- ๐ก๏ธ Type Guards - Runtime type checking with compile-time safety
- ๐ฏ Conditional Types - Dynamic type generation based on conditions
- ๐ Template Literal Types - Dynamic string type creation
- ๐บ๏ธ Mapped Types - Type transformations and utility types
- โก Type Narrowing - Automatic type inference and narrowing
- Character Selection - Choose from 4 unique character classes
- Dynamic Actions - Class-specific abilities with type safety
- Status Effects - Add/remove effects with union type validation
- Live Code Examples - Real-time TypeScript code generation
- Educational Showcase - Interactive type demonstrations
- Node.js 18+
- npm or yarn
# Clone the repository
git clone <repository-url>
cd TypeScript-Union-Types-RPG
# Install dependencies
npm install
# Start development server
npm run dev
# Build for production
npm run buildsrc/
โโโ components/ # React components
โ โโโ CharacterCard.tsx # Individual character display
โ โโโ CharacterSelection.tsx # Character grid & controls
โ โโโ GameActions.tsx # Class-specific actions
โ โโโ Header.tsx # App header
โ โโโ TypeShowcase.tsx # Live TypeScript examples
โ โโโ EducationalNotes.tsx # Learning content
โโโ hooks/
โ โโโ useGameState.ts # Game state management
โโโ types/
โ โโโ game.ts # Core TypeScript definitions
โโโ utils/
โ โโโ typeGuards.ts # Type guard utilities
โโโ App.tsx # Main application
โโโ main.tsx # Application entry point
type Warrior = {
type: 'warrior';
strength: number;
armor: number;
weaponType: 'sword' | 'axe' | 'hammer';
battleCry: string;
}type Mage = {
type: 'mage';
intelligence: number;
mana: number;
element: Element;
spells: SpellName<Element>[];
}type Archer = {
type: 'archer';
dexterity: number;
accuracy: number;
arrowType: 'normal' | 'fire' | 'ice' | 'poison';
rangeBonus: number;
}type Rogue = {
type: 'rogue';
stealth: number;
criticalChance: number;
poisonDamage: number;
backstabMultiplier: number;
}type Character = Warrior | Mage | Archer | Rogue;
// Type narrowing with discriminant property
function getCharacterInfo(character: Character) {
switch (character.type) {
case 'warrior':
return `Strength: ${character.strength}`; // โ
TypeScript knows it's a Warrior
case 'mage':
return `Mana: ${character.mana}`; // โ
TypeScript knows it's a Mage
// ...
}
}type Element = 'fire' | 'ice' | 'lightning' | 'dark';
type SpellName<T extends Element> = `${T}_spell`;
// Usage: 'fire_spell' | 'ice_spell' | 'lightning_spell' | 'dark_spell'type AbilityForClass<T extends CharacterClass> =
T extends 'warrior' ? { type: 'charge' | 'defend' | 'berserk' } :
T extends 'mage' ? { type: 'fireball' | 'heal' | 'teleport' } :
T extends 'archer' ? { type: 'multishot' | 'aimshot' | 'trap' } :
T extends 'rogue' ? { type: 'backstab' | 'vanish' | 'poison_dart' } :
never;const isWarrior = (character: Character): character is Warrior => {
return character.type === 'warrior';
};
// Usage with type narrowing
if (isWarrior(character)) {
console.log(character.strength); // โ
TypeScript knows it's a Warrior
}- Add Random Effects - Poisoned, Burning, Frozen, Blessed, Cursed
- Remove Effects - Type-safe removal with validation
- Visual Indicators - Icons and descriptions for each effect
- Class-Specific - Each character class has unique abilities
- Type-Safe - Actions are validated at compile time
- Interactive - Click to perform actions with visual feedback
- Real-Time Updates - Code examples update based on selected character
- Syntax Highlighting - Proper TypeScript syntax display
- Educational - Shows actual implementation patterns
This project teaches:
- Union Type Design - Creating robust type systems
- Type Safety - Preventing runtime errors through types
- Code Organization - Structuring TypeScript projects
- React Integration - Using TypeScript with React hooks
- Advanced Patterns - Template literals, conditional types, mapped types
npm run dev # Start development server
npm run build # Build for production
npm run preview # Preview production build
npm run lint # Run ESLint- React 19 - UI library with latest features
- TypeScript 5.6 - Advanced type system
- Tailwind CSS 3.4 - Utility-first styling
- Vite 5.4 - Fast build tool
- Lucide React - Beautiful icons
- Fork the repository
- Create a feature branch (
git checkout -b feature/amazing-feature) - Commit your changes (
git commit -m 'Add amazing feature') - Push to the branch (
git push origin feature/amazing-feature) - Open a Pull Request
This project is licensed under the MIT License - see the LICENSE file for details.
- TypeScript team for amazing type system features
- React team for excellent developer experience
- Tailwind CSS for beautiful styling utilities
- Lucide for clean, modern icons
Built with โค๏ธ and advanced TypeScript patterns