Writing hooks without type safety means you're guessing at property names, relying on documentation for payload structure, and hoping your response format is correct. Every hook becomes an exercise in trial and error, with no editor assistance to guide you.
This lesson introduces the cursor-hooks package, which provides complete TypeScript definitions for all hook payloads and responses. Combined with Bun's elegant Bun.stdin.json() method for reading input, you'll write hooks with full autocomplete support, compile-time error checking, and absolute confidence that your code is correct.
The Power of Types
- Autocomplete Everything: See available properties as you type, with no need to reference documentation.
 - Catch Errors Early: TypeScript catches typos and invalid property access before you run your code.
 - Self-Documenting Code: Type definitions serve as inline documentation, making hooks easier to understand and maintain.
 - Elegant Input Handling: Bun's 
Bun.stdin.json()reads and parses hook input in a single, clean line of code. 
Building a Type-Safe Hook
The workflow is simple and produces reliable, maintainable code:
- Install Dependencies: Add the 
cursor-hookspackage to your Bun project for complete type definitions. - Read Typed Input: Use 
await Bun.stdin.json()and apply the appropriate payload type (e.g.,BeforeSubmitPromptPayload). - Build Typed Output: Create your response object with the correct response type (e.g., 
BeforeSubmitPromptResponse). - Implement Logic: Write conditional logic based on the typed input—your editor will guide you with autocomplete at every step.
 - Test Immediately: Test the hook directly in Cursor's chat interface to see your logic in action.
 
Commands
Install the package providing TypeScript types for Cursor hooks.
Prompts
A test prompt that will be blocked by the hook's logic.
A test prompt that includes the required keyword to be allowed by the hook.
Example Implementation
This before-submit-prompt.ts script shows the elegance of type-safe hooks. Notice how the types guide the entire implementation—from reading the payload to building the response.
With types in place, your editor becomes a powerful assistant, showing you exactly what properties are available on the input and what shape your output must take. This transforms hook development from guesswork into a guided, confident process.
