Skip to content

Commit 6de8f68

Browse files
authored
feat(runtime-vapor): implement defineVaporCustomElement type inference (#14183)
1 parent 158e706 commit 6de8f68

File tree

6 files changed

+490
-113
lines changed

6 files changed

+490
-113
lines changed
Lines changed: 248 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,248 @@
1+
import {
2+
type VaporElementConstructor,
3+
defineVaporComponent,
4+
defineVaporCustomElement,
5+
} from 'vue'
6+
import { describe, expectType, test } from '../utils'
7+
8+
describe('defineVaporCustomElement using defineVaporComponent return type', () => {
9+
test('with object emits', () => {
10+
const Comp1Vapor = defineVaporComponent({
11+
props: {
12+
a: String,
13+
},
14+
emits: {
15+
click: () => true,
16+
},
17+
})
18+
const Comp = defineVaporCustomElement(Comp1Vapor)
19+
expectType<VaporElementConstructor>(Comp)
20+
21+
const instance = new Comp()
22+
expectType<string | undefined>(instance.a)
23+
instance.a = ''
24+
})
25+
26+
test('with array emits', () => {
27+
const Comp1Vapor = defineVaporComponent({
28+
props: {
29+
a: Number,
30+
},
31+
emits: ['click'],
32+
})
33+
const Comp = defineVaporCustomElement(Comp1Vapor)
34+
expectType<VaporElementConstructor>(Comp)
35+
36+
const instance = new Comp()
37+
expectType<number | undefined>(instance.a)
38+
instance.a = 42
39+
})
40+
41+
test('with required props', () => {
42+
const Comp1Vapor = defineVaporComponent({
43+
props: {
44+
a: { type: Number, required: true },
45+
},
46+
})
47+
const Comp = defineVaporCustomElement(Comp1Vapor)
48+
expectType<VaporElementConstructor>(Comp)
49+
50+
const instance = new Comp()
51+
expectType<number>(instance.a)
52+
instance.a = 42
53+
})
54+
55+
test('with default props', () => {
56+
const Comp1Vapor = defineVaporComponent({
57+
props: {
58+
a: {
59+
type: Number,
60+
default: 1,
61+
validator: () => true,
62+
},
63+
},
64+
emits: ['click'],
65+
})
66+
const Comp = defineVaporCustomElement(Comp1Vapor)
67+
expectType<VaporElementConstructor>(Comp)
68+
69+
const instance = new Comp()
70+
expectType<number>(instance.a)
71+
instance.a = 42
72+
})
73+
74+
test('with extra options', () => {
75+
const Comp1Vapor = defineVaporComponent({
76+
props: {
77+
a: {
78+
type: Number,
79+
default: 1,
80+
validator: () => true,
81+
},
82+
},
83+
emits: ['click'],
84+
})
85+
const Comp = defineVaporCustomElement(Comp1Vapor, {
86+
shadowRoot: false,
87+
styles: [`div { color: red; }`],
88+
nonce: 'xxx',
89+
shadowRootOptions: {
90+
delegatesFocus: false,
91+
},
92+
configureApp: app => {
93+
app.provide('a', 1)
94+
},
95+
})
96+
expectType<VaporElementConstructor>(Comp)
97+
98+
const instance = new Comp()
99+
expectType<number>(instance.a)
100+
instance.a = 42
101+
})
102+
})
103+
104+
describe('defineVaporCustomElement with direct setup function', () => {
105+
test('basic setup function', () => {
106+
const Comp = defineVaporCustomElement((props: { msg: string }) => {
107+
expectType<string>(props.msg)
108+
return []
109+
})
110+
expectType<VaporElementConstructor<{ msg: string }>>(Comp)
111+
112+
const instance = new Comp()
113+
expectType<string>(instance.msg)
114+
})
115+
116+
test('setup function with emits', () => {
117+
const Comp = defineVaporCustomElement(
118+
(props: { msg: string }, ctx) => {
119+
ctx.emit('foo')
120+
return []
121+
},
122+
{
123+
emits: ['foo'],
124+
},
125+
)
126+
expectType<VaporElementConstructor<{ msg: string }>>(Comp)
127+
128+
const instance = new Comp()
129+
expectType<string>(instance.msg)
130+
})
131+
132+
test('setup function with extra options', () => {
133+
const Comp = defineVaporCustomElement(
134+
(props: { msg: string }, ctx) => {
135+
ctx.emit('foo')
136+
return []
137+
},
138+
{
139+
name: 'Foo',
140+
emits: ['foo'],
141+
inheritAttrs: false,
142+
shadowRoot: false,
143+
styles: [`div { color: red; }`],
144+
nonce: 'xxx',
145+
shadowRootOptions: {
146+
delegatesFocus: false,
147+
},
148+
configureApp: app => {
149+
app.provide('a', 1)
150+
},
151+
},
152+
)
153+
expectType<VaporElementConstructor<{ msg: string }>>(Comp)
154+
155+
const instance = new Comp()
156+
expectType<string>(instance.msg)
157+
})
158+
})
159+
160+
describe('defineVaporCustomElement with options object', () => {
161+
test('with object props', () => {
162+
const Comp = defineVaporCustomElement({
163+
props: {
164+
foo: String,
165+
bar: {
166+
type: Number,
167+
required: true,
168+
},
169+
},
170+
setup(props) {
171+
expectType<string | undefined>(props.foo)
172+
expectType<number>(props.bar)
173+
},
174+
})
175+
expectType<VaporElementConstructor>(Comp)
176+
177+
const instance = new Comp()
178+
expectType<string | undefined>(instance.foo)
179+
expectType<number>(instance.bar)
180+
})
181+
182+
test('with array props', () => {
183+
const Comp = defineVaporCustomElement({
184+
props: ['foo', 'bar'],
185+
setup(props) {
186+
expectType<any>(props.foo)
187+
expectType<any>(props.bar)
188+
},
189+
})
190+
expectType<VaporElementConstructor>(Comp)
191+
192+
const instance = new Comp()
193+
expectType<any>(instance.foo)
194+
expectType<any>(instance.bar)
195+
})
196+
197+
test('with emits', () => {
198+
const Comp = defineVaporCustomElement({
199+
props: {
200+
value: String,
201+
},
202+
emits: {
203+
change: (val: string) => true,
204+
},
205+
setup(props, { emit }) {
206+
emit('change', 'test')
207+
// @ts-expect-error
208+
emit('change', 123)
209+
// @ts-expect-error
210+
emit('unknown')
211+
},
212+
})
213+
expectType<VaporElementConstructor>(Comp)
214+
215+
const instance = new Comp()
216+
expectType<string | undefined>(instance.value)
217+
})
218+
219+
test('with extra options', () => {
220+
const Comp = defineVaporCustomElement(
221+
{
222+
props: {
223+
value: String,
224+
},
225+
emits: {
226+
change: (val: string) => true,
227+
},
228+
setup(props, { emit }) {
229+
emit('change', 'test')
230+
// @ts-expect-error
231+
emit('change', 123)
232+
// @ts-expect-error
233+
emit('unknown')
234+
},
235+
},
236+
{
237+
shadowRoot: false,
238+
configureApp: app => {
239+
app.provide('a', 1)
240+
},
241+
},
242+
)
243+
expectType<VaporElementConstructor>(Comp)
244+
245+
const instance = new Comp()
246+
expectType<string | undefined>(instance.value)
247+
})
248+
})

0 commit comments

Comments
 (0)