feat(dto): refine DTO class field declarations and schema handling

This commit is contained in:
2026-07-14 11:53:01 +08:00
parent 3a4f24cb06
commit 9612a28b3c
+16 -1
View File
@@ -144,10 +144,26 @@ export type XxxResponseData = z.output<typeof XxxResponseSchema>;
src/data/dto/<module>/*.ts
```
要求:
- DTO class 的 `declare readonly` 只声明前端业务代码会直接访问的字段。
- 不要因为后端响应包含某字段,就机械地为它增加 class 属性、嵌套类型或类型别名。
- 后端返回但前端既不读取、也不透传的字段,可以不加入 schema 和 DTO。
- 仅用于请求序列化、响应兼容或内部透传的字段保留在 schema 中,不添加 class 属性声明。
- `Object.assign(this, data)` 会保留 schema 解析后的字段;即使 class 未声明对应属性,`toJson()` 仍可按 schema 正常序列化。
- 新增 DTO 字段前先搜索实际调用点;没有直接读取方时默认不声明。
DTO 统一模式:
```ts
export const XxxResponseSchema = z.object({
id: z.string(),
// 仅用于内部透传,不需要在 DTO class 中声明。
traceId: z.string(),
});
export class XxxResponse {
// 业务代码直接读取的字段才公开声明。
declare readonly id: string;
private constructor(input: XxxResponseInput) {
@@ -365,4 +381,3 @@ pnpm exec vitest run <related tests>
4. 更新状态机和 UI 调用。
5. 运行 `rg` 确认无残留引用。
6. 运行验证。