Skip to content

Commit 3bd71f8

Browse files
committed
fix(macros): return VNode for generic defineComponent
close #21
1 parent fb8416b commit 3bd71f8

File tree

3 files changed

+17
-10
lines changed

3 files changed

+17
-10
lines changed

packages/macros/src/volar/transform.ts

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -75,17 +75,14 @@ export function transformJsxMacros(
7575
}
7676
}
7777

78+
const isDefineComponent =
79+
macros.defineComponent?.expression.getText(ast) === 'defineComponent'
7880
codes.replaceRange(
7981
node.getStart(ast),
8082
node.expression.getStart(ast),
8183
'const ',
8284
[`__render`, node.getStart(ast) - 1, { verification: true }],
83-
macros.defineComponent
84-
? macros.defineComponent?.expression.getText(ast) ===
85-
'defineVaporComponent'
86-
? ''
87-
: ': () => JSX.Element'
88-
: '',
85+
isDefineComponent ? ': () => JSX.Element' : '',
8986
' = ',
9087
)
9188
codes.replaceRange(
@@ -101,7 +98,7 @@ return {} as {
10198
expose: (exposed: ${macros.defineExpose ?? 'Record<string, any>'}) => void,
10299
attrs: Record<string, any>
103100
},
104-
render: typeof __render
101+
render: ${isDefineComponent ? `ReturnType<` : ''}typeof __render${isDefineComponent ? '>' : ''}
105102
}`,
106103
)
107104
}

packages/macros/tests/fixtures/define-component.tsx

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { defineComponent, defineVaporComponent, nextTick, unref } from 'vue'
1+
import { defineComponent, defineVaporComponent, nextTick, unref, VaporComponentInstance, VNode } from 'vue'
22

33
const $ = unref
44

@@ -62,4 +62,14 @@ defineVaporComponent(({
6262
{i}
6363
</>
6464
)
65-
})
65+
})
66+
67+
// #21
68+
const Comp3 = defineComponent(<T,>()=>{
69+
return () => <div>123</div>
70+
})
71+
;<Comp3 /> == ({} as VNode)
72+
const Comp4 = defineVaporComponent(<T,>() => {
73+
return <div>123</div>
74+
})
75+
;<Comp4 /> == ({} as VaporComponentInstance)

packages/vue-jsx-vapor/src/volar/jsx-element.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ type __InferJsxElement<T> = T extends keyof HTMLElementTagNameMap
5959
? SVGElementTagNameMap[T]
6060
: T extends (props: infer Props, ctx: { slots: infer Slots, expose: (exposed: infer Exposed) => void, attrs: any, emit: any }) => infer TypeBlock
6161
// @ts-ignore
62-
? import('vue-jsx-vapor').VaporComponentInstance<Props, {}, Slots, Exposed, TypeBlock>
62+
? TypeBlock extends import('vue').VNode ? TypeBlock : import('vue-jsx-vapor').VaporComponentInstance<Props, {}, Slots, Exposed, TypeBlock>
6363
: T extends { new (...args: any[]): infer Instance }
6464
? Instance extends { $: any }
6565
? import('vue').VNode

0 commit comments

Comments
 (0)