Skip to content

cycosoft/Eyas / .agents/skills/typescript-gotchas

typescript-gotchas

Verified

Advanced TypeScript engineering standards and edge-case mechanics covering type leaks, event listener mocking narrowing, and API migration test awareness.

Reusable Skill: Advanced TypeScript & Test Mocking Gotchas

Overview

This skill documents advanced TypeScript constraints and test mocking workarounds required to maintain strict type safety and prevent narrowing errors across the Eyas codebase.

When to Use

  • Trigger when encountering TypeScript narrowing errors (never) during unit test mocking.
  • Trigger when refactoring complex type structures or migrating internal APIs.

Instructions

1. Preventing Type Leaks

Avoid anonymous object structures even within casting syntax (e.g., as { default: T }). These are considered "Type Leaks." Always use a named meta-type from src/types/ (e.g., ModuleWithDefault<T>) to wrap these structures.

2. Test Narrowing Safety

When mocking event-driven callbacks (e.g., IPC listeners) that are assigned inside the mock, TypeScript may narrow the callback variable to never at the call site. Use explicit type assertions (e.g., (callback as any)(...)) or // eslint-disable-line to bypass these narrowing errors in test flows.

3. API Migration Awareness

When migrating to newer internal or external APIs, prioritize updating all dependent unit tests immediately. Common failures occur when tests mock specific method names that have been renamed or relocated (e.g., webContents.goBack to webContents.navigationHistory.goBack).

4. Module-level Constant Mocking

When a module exports constants derived from the environment at import time (e.g. export const isMac = process.platform === 'darwin'), simply stubbing the environment variable after the import will fail.

  • Solution: Use vi.mock with a factory and vi.hoisted to provide reactive mock values that can be updated between tests.
  • Example:
const platformMocks = vi.hoisted(() => ({ isMac: false }));
vi.mock('@scripts/platform-utils.js', () => platformMocks);
// in test
platformMocks.isMac = true;
finishing-a-development-branchobra/superpowersUse when implementation is complete, all tests pass, and you need to decide how to integrate the workStars: 319kUpdated Claude CodeCodexVerifiedInstall
systematic-debuggingobra/superpowersUse when encountering any bug, test failure, or unexpected behavior, before proposing fixesStars: 319kUpdated Claude CodeCodexVerifiedInstall
test-driven-developmentobra/superpowersUse when implementing any feature or bugfix, before writing implementation codeStars: 319kUpdated Claude CodeCodexVerifiedInstall
skill-creatoranthropics/skillsCreate new skills, modify and improve existing skills, and measure skill performance. Use when users want to create a skill from scratch, edit, or optimize an existing skill, run evals to test a skill, benchmark skill performance with variance analysis, or optimize a skill's description for better triggering accuracy.Stars: 196kUpdated Claude CodeCodexVerifiedInstall