typescript-type-safe-api-contracts

Solid

TypeScript patterns for type-safe API contracts, strict typing, Zod integration. Use when designing APIs, creating types, validating data. Keywords: "api design", "schema", "type safety", "interface", "zod", "validation", "type", "strict mode".

API & Backend 364 stars 68 forks Updated today MIT

Install

View on GitHub

Quality Score: 89/100

Stars 20%
85
Recency 20%
100
Frontmatter 20%
70
Documentation 15%
100
Issue Health 10%
80
License 10%
100
Description 5%
100

Skill Content

# TypeScript Type-Safe API Contracts ## Core Principle: Explicit Return Types (Mandatory) ```tsx // ❌ BAD: Implicit return type export function getUser(id: string) { return db.user.findUnique({ where: { id } }); } // ✅ GOOD: Explicit return type export async function getUser(id: string): Promise<User | null> { return db.user.findUnique({ where: { id } }); } ``` ## Interface vs Type **Use `interface` for object shapes**: ```tsx interface User { id: string; name: string; email: string; } interface ApiResponse<T> { success: boolean; data: T; } ``` **Use `type` for unions, intersections, utilities**: ```tsx type Status = 'pending' | 'active' | 'archived'; type ApiResult<T> = | { success: true; data: T } | { success: false; error: string }; ``` ## Generic API Response Wrapper ```tsx // types/api/common.ts export type ApiResponse<T> = | ApiSuccessResponse<T> | ApiErrorResponse; export interface ApiSuccessResponse<T> { success: true; data: T; } export interface ApiErrorResponse { success: false; error: { code: string; message: string; details?: Record<string, string[]>; }; } // Usage export async function createUser(data: CreateUserInput): Promise<ApiResponse<User>> { // ... } ``` ## Zod Integration (Runtime Validation) ```tsx import { z } from 'zod'; // 1. Define Zod schema const userSchema = z.object({ name: z.string().min(1, 'Name required').max(100), email: z.string().email('Invalid email'), age: z.number().in...

Details

Author
majiayu000
Repository
majiayu000/claude-skill-registry
Created
5 months ago
Last Updated
today
Language
HTML
License
MIT

Similar Skills

Semantically similar based on skill content — not just same category