Library › Coding AI › Software developers
Turn a rate-limit spec into a typed TypeScript stub
Practice: Setting limits and rules. About 5 minutes.
About this exercise
You're splitting the rate-limiter ticket: you write the types and signature today, and Ines implements it tomorrow. Your team's names are fixed: an exported type LimitDecision with exactly allowed: boolean and retryAfterMs: number | null, and an exported function checkLimit(userId: string, now: Date): LimitDecision whose body is only throw new Error("not implemented"). Reply with exactly 1 code block of at most 10 lines, no imports or classes, and under 20 words of text.
Common mistakes when you ask AI for this
- Getting a full implementation. Ask for 'this function' and the AI builds the whole limiter with a Map and timestamps, which is tomorrow's ticket for someone else. Try: Say: "Stub only: the body is just throw new Error(\"not implemented\")."
- Letting the AI pick names. The AI invents names like RateLimitResult or retryAfter, and the route handler won't compile against them. Try: Give them: "type LimitDecision { allowed: boolean; retryAfterMs: number | null }".
- A different signature. The AI may read the time itself or take a request object, which breaks Gil's no-fake-timers plan. Try: Add: "Signature exactly checkLimit(userId: string, now: Date): LimitDecision."
- Extras around the code. The AI adds JSDoc, usage examples and a design note. Try: Add: "One code block, no imports, no comments, no text."
What a strong prompt includes
- Asks for a stub only
- Names the return type
- Gives the fields and types
- Gives the exact signature
- Limits the extras
Related exercises
- Add a cron line that matches the others in a crontab (Coding AI · Product managers)
- Add dark mode by overriding your existing CSS variables (Coding AI · Software developers)
- Add input checks to your number guessing game (Coding AI · High school students)
- Add input validation to a Java method with exact messages (Coding AI · Software developers)
- Add labels to a signup form without rewriting the page (Coding AI · High school students)