fix(layout): stabilize declaration types and simplify slot presence handling#374
Conversation
WalkthroughLayout.vue's slot-emptiness computation was replaced with simple slot-presence checks (Boolean(slots[...])), removing the hasNonEmptySlotContent import; the corresponding utility function was deleted from slots.ts. LayoutSurfaceProps was relocated from an inline interface in LayoutSurface.vue into a new exported interface in internal.type.ts. ChangesLayout refactor
Estimated code review effort: 2 (Simple) | ~10 minutes Possibly related PRs
Suggested reviewers: Poem
🚥 Pre-merge checks | ✅ 5✅ Passed checks (5 passed)
✨ Finishing Touches🧪 Generate unit tests (beta)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
📦 Package Previewpnpm add https://pkg.pr.new/@opentiny/tiny-robot@73d9b62 pnpm add https://pkg.pr.new/@opentiny/tiny-robot-kit@73d9b62 pnpm add https://pkg.pr.new/@opentiny/tiny-robot-svgs@73d9b62 commit: 73d9b62 |
🧹 Preview Cleaned UpThe preview deployment has been removed. |

背景
本次 PR 主要处理了
layout组件的两个相关问题:LayoutSurface的 props 类型定义在.vue内部,导致声明生成阶段出现TS4023 / TS4058,类型名无法稳定导出。Layout通过执行 slot、解析 VNode 来反推 header / aside / footer 是否“真的有内容”,这会让布局结构依赖 slot 的内部实现细节,不符合“显式输入决定视图结构”的组件设计路线。(注:当前 PR 是 layout 组件 e2e 测试 - #372 的前置提交)
问题修改
1. 收敛
LayoutSurface的类型出口LayoutSurfaceProps从 LayoutSurface.vue 内部移动到 internal.type.ts。LayoutSurface.vue改为直接从internal.type.ts引入该类型。原因分析:
组件构建过程中,出现了类型问题(如下图)
Layout.vue模板引用LayoutSurface后,声明生成工具会把子组件 props 类型间接带入父组件导出类型。.vue私有上下文内,声明打包时就可能无法稳定命名,进而触发:TS4058 Return type of exported function has or is using name ... but cannot be namedTS4023 Exported variable ... has or is using name ... but cannot be named相关参考:
https://raw.githubusercontent.com/microsoft/TypeScript/main/src/compiler/diagnosticMessages.json
<script setup>/defineProps官方文档:https://vuejs.org/api/sfc-script-setup.html
vite-plugin-dtsREADME:https://raw.githubusercontent.com/qmhc/vite-plugin-dts/master/README.md
结论是:
defineProps不能引用外部类型;.vue内部私有类型名;.ts模块,是更稳定的做法。2. 删除 slot 内容反向推断逻辑
Comment/Text/Fragment/ VNode 来判断区域是否“有内容”。原因分析:
参考 #370 讨论,可以得出结论:
Layout本来应该根据显式输入组织布局;预期实现
Layout的结构语义收敛为:测试调整
后续 #372 中:
由于
Layout的 slot 语义已经改为“slot presence 决定结构”,对应结构测试也做了同步调整:验证
已验证:
通过。
已验证:
pnpm.cmd -F tiny-robot-test test src/layout/specs/structure.spec.ts通过。
影响说明
行为变化
v-if="false"时,Layout可能尝试移除 header / aside shellLayout就认为该区域存在兼容性
layout的结构判断语义更直接、更稳定。总结
.vue私有类型进入导出链路最终效果是:
layout的类型导出更稳定;layout的结构语义更简单、更可预测;layout的使用方式更符合“显式输入决定视图结构”的组件设计原则。Summary by CodeRabbit
Bug Fixes
Refactor