| name | typescript-api-typing |
| description | null |
API Typing
Category: typescript · Status: 🟢 Active
When to use
Khi type request/response của API và muốn an toàn kiểu từ network đến UI.
Steps
- Định nghĩa schema bằng zod cho dữ liệu từ ngoài (response, form, env).
- Suy type từ schema bằng
z.infer, không khai báo interface trùng tay.
parse/safeParse response tại ranh giới I/O để validate runtime, không tin as.
- Type request payload riêng; tách DTO server với model dùng trong UI nếu khác.
- Không dùng
any cho response; dùng generic cho client request<T>().
Template
import { z } from "zod";
const userSchema = z.object({ id: z.string(), name: z.string(), email: z.string().email() });
type User = z.infer<typeof userSchema>;
async function getUser(id: string): Promise<User> {
const res = await api.get(`/users/${id}`);
return userSchema.parse(res.data);
}
Example
Good: z.infer cho type, schema.parse validate response trước khi trả về.
Avoid: return res.data as User bỏ qua validate; response để any rồi truy cập field bừa.
Checklist