Skip to content

Commit 85c5ec4

Browse files
so11ysxzz
authored andcommitted
test(defineProps): add intersection type test
1 parent 623ba51 commit 85c5ec4

File tree

2 files changed

+201
-0
lines changed

2 files changed

+201
-0
lines changed

packages/compiler-sfc/__tests__/compileScript/__snapshots__/defineProps.spec.ts.snap

Lines changed: 131 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,87 @@ export default /*#__PURE__*/_defineComponent({
147147
148148
149149
150+
return { }
151+
}
152+
153+
})"
154+
`;
155+
156+
exports[`defineProps > w/ extends interface 2`] = `
157+
"import { defineComponent as _defineComponent } from 'vue'
158+
interface Bar extends Foo { y?: number }
159+
interface Props extends Bar {
160+
z: number
161+
y: string
162+
}
163+
164+
interface Foo { x?: number }
165+
166+
export default /*#__PURE__*/_defineComponent({
167+
props: {
168+
z: { type: Number, required: true },
169+
y: { type: String, required: true },
170+
x: { type: Number, required: false }
171+
},
172+
setup(__props: any, { expose: __expose }) {
173+
__expose();
174+
175+
176+
177+
return { }
178+
}
179+
180+
})"
181+
`;
182+
183+
exports[`defineProps > w/ extends intersection Type 1`] = `
184+
"import { defineComponent as _defineComponent } from 'vue'
185+
type Foo = {
186+
x?: number;
187+
};
188+
interface Props extends Foo {
189+
z: number
190+
y: string
191+
}
192+
193+
export default /*#__PURE__*/_defineComponent({
194+
props: {
195+
z: { type: Number, required: true },
196+
y: { type: String, required: true },
197+
x: { type: Number, required: false }
198+
},
199+
setup(__props: any, { expose: __expose }) {
200+
__expose();
201+
202+
203+
204+
return { }
205+
}
206+
207+
})"
208+
`;
209+
210+
exports[`defineProps > w/ extends intersection type 1`] = `
211+
"import { defineComponent as _defineComponent } from 'vue'
212+
type Foo = {
213+
x?: number;
214+
};
215+
interface Props extends Foo {
216+
z: number
217+
y: string
218+
}
219+
220+
export default /*#__PURE__*/_defineComponent({
221+
props: {
222+
z: { type: Number, required: true },
223+
y: { type: String, required: true },
224+
x: { type: Number, required: false }
225+
},
226+
setup(__props: any, { expose: __expose }) {
227+
__expose();
228+
229+
230+
150231
return { }
151232
}
152233
@@ -182,6 +263,56 @@ export default /*#__PURE__*/_defineComponent({
182263
183264
184265
266+
return { }
267+
}
268+
269+
})"
270+
`;
271+
272+
exports[`defineProps > w/ intersection Type 1`] = `
273+
"import { defineComponent as _defineComponent } from 'vue'
274+
type Foo = {
275+
x?: number;
276+
};
277+
type Bar = {
278+
y: string;
279+
};
280+
281+
export default /*#__PURE__*/_defineComponent({
282+
props: {
283+
x: { type: Number, required: false },
284+
y: { type: String, required: true }
285+
},
286+
setup(__props: any, { expose: __expose }) {
287+
__expose();
288+
289+
290+
291+
return { }
292+
}
293+
294+
})"
295+
`;
296+
297+
exports[`defineProps > w/ intersection type 1`] = `
298+
"import { defineComponent as _defineComponent } from 'vue'
299+
type Foo = {
300+
x?: number;
301+
};
302+
type Bar = {
303+
y: string;
304+
};
305+
306+
export default /*#__PURE__*/_defineComponent({
307+
props: {
308+
x: { type: Number, required: false },
309+
y: { type: String, required: true }
310+
},
311+
setup(__props: any, { expose: __expose }) {
312+
__expose();
313+
314+
315+
185316
return { }
186317
}
187318

packages/compiler-sfc/__tests__/compileScript/defineProps.spec.ts

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -258,6 +258,76 @@ const props = defineProps({ foo: String })
258258
})
259259
})
260260

261+
test('w/ extends interface', () => {
262+
const { content, bindings } = compile(`
263+
<script lang="ts">
264+
interface Foo { x?: number }
265+
</script>
266+
<script setup lang="ts">
267+
interface Bar extends Foo { y?: number }
268+
interface Props extends Bar {
269+
z: number
270+
y: string
271+
}
272+
defineProps<Props>()
273+
</script>
274+
`)
275+
assertCode(content)
276+
expect(content).toMatch(`z: { type: Number, required: true }`)
277+
expect(content).toMatch(`y: { type: String, required: true }`)
278+
expect(content).toMatch(`x: { type: Number, required: false }`)
279+
expect(bindings).toStrictEqual({
280+
x: BindingTypes.PROPS,
281+
y: BindingTypes.PROPS,
282+
z: BindingTypes.PROPS
283+
})
284+
})
285+
286+
test('w/ extends intersection type', () => {
287+
const { content, bindings } = compile(`
288+
<script setup lang="ts">
289+
type Foo = {
290+
x?: number;
291+
};
292+
interface Props extends Foo {
293+
z: number
294+
y: string
295+
}
296+
defineProps<Props>()
297+
</script>
298+
`)
299+
assertCode(content)
300+
expect(content).toMatch(`z: { type: Number, required: true }`)
301+
expect(content).toMatch(`y: { type: String, required: true }`)
302+
expect(content).toMatch(`x: { type: Number, required: false }`)
303+
expect(bindings).toStrictEqual({
304+
x: BindingTypes.PROPS,
305+
y: BindingTypes.PROPS,
306+
z: BindingTypes.PROPS
307+
})
308+
})
309+
310+
test('w/ intersection type', () => {
311+
const { content, bindings } = compile(`
312+
<script setup lang="ts">
313+
type Foo = {
314+
x?: number;
315+
};
316+
type Bar = {
317+
y: string;
318+
};
319+
defineProps<Foo & Bar>()
320+
</script>
321+
`)
322+
assertCode(content)
323+
expect(content).toMatch(`y: { type: String, required: true }`)
324+
expect(content).toMatch(`x: { type: Number, required: false }`)
325+
expect(bindings).toStrictEqual({
326+
x: BindingTypes.PROPS,
327+
y: BindingTypes.PROPS
328+
})
329+
})
330+
261331
test('w/ exported interface', () => {
262332
const { content, bindings } = compile(`
263333
<script setup lang="ts">

0 commit comments

Comments
 (0)