How to use isolatedDeclarations with defineComponent in typescript #14434
Replies: 4 comments 2 replies
|
下面是在启用 ✅ 核心思路
✅ 推荐做法 1:显式导出 Props/Emits 类型export interface FadeInExpandTransitionProps {
group?: boolean
appear?: boolean
width?: boolean
mode?: 'default' | 'in-out' | 'out-in'
onLeave?: () => void
onAfterLeave?: () => void
onAfterEnter?: () => void
}然后: export default defineComponent({
props: {
// ...使用 PropType 显式标注
} as PropType<FadeInExpandTransitionProps>,
})好处: ✅ 推荐做法 2:
|
| 问题 | 规避 |
|---|---|
通过 PropType<typeof something> 推断链很长 |
改用显式 interface |
| 条件类型 + infer 组合 | 拆成明确类型别名 |
| 导出 default defineComponent 直接推断 | 用 const X = defineComponent(...); export type Props = ...; export default X |
✅ 结论(给你一个最实用的答案)
想“优雅声明组件”,而不是手写长类型:
✅ 把 props/emits 类型显式抽到 interface
✅ 用 satisfies 或 defineProps<T>() 固定推断
✅ 让每个文件声明可独立生成
|
Hello @Mister-Hope, Working with Vue reactivity system and compilers can sometimes present interesting edge cases. A few quick things to consider regarding this topic:
I highly recommend checking out the Vue DevTools extension to inspect the component tree and verify if the state is updating as expected. Let me know if you want to dig deeper into the specific component logic. Hope this points you in the right direction! Let me know how it goes. Happy coding! |
|
With import { defineComponent, h, Transition, type DefineComponent, type PropType } from 'vue'
export interface FadeInExpandTransitionProps {
group?: boolean
appear?: boolean
mode?: 'default' | 'in-out' | 'out-in'
onLeave?: () => void
}
const FadeInExpandTransition: DefineComponent<FadeInExpandTransitionProps> = defineComponent({
name: 'FadeInExpandTransition',
props: {
group: Boolean,
appear: Boolean,
mode: String as PropType<'default' | 'in-out' | 'out-in'>,
onLeave: Function as PropType<() => void>,
},
setup(props, { slots }) {
return () => h(Transition, { appear: props.appear }, slots)
},
})
export default FadeInExpandTransitionChecked with vue 3.5.46 and declare const FadeInExpandTransition: DefineComponent<FadeInExpandTransitionProps>;
export default FadeInExpandTransition;and consumers are still type-checked ( You do have to write the props interface by hand. Deriving it with |
|
Younghwan91's approach is the one that worked for me too. Annotating the variable that holds the component, instead of trying to annotate inside the defineComponent call, is the key trick. Once the variable has an explicit type, isolatedDeclarations stops complaining, and the props stay inferred for the implementation itself. I would add one thing: keep the runtime props object and the interface in sync by hand, since TypeScript will not check that for you with DefineComponent. It is a little extra care, but it is much less work than writing every type by hand from scratch. |
Uh oh!
There was an error while loading. Please reload this page.
When I am trying to speed up with oxc, I need to enable isolatedDeclarations for dts generation.
However, I do not see any possibility to write a detailed type by hand. Is there anyway to "declare these components" gracefully?
All reactions