Skip to content

Commit b738637

Browse files
committed
test(defineProps): add intersection type test
1 parent 3127c41 commit b738637

File tree

3 files changed

+202
-1
lines changed

3 files changed

+202
-1
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
@@ -153,6 +153,87 @@ export default /*#__PURE__*/_defineComponent({
153153
154154
155155
156+
return { }
157+
}
158+
159+
})"
160+
`;
161+
162+
exports[`defineProps > w/ extends interface 2`] = `
163+
"import { defineComponent as _defineComponent } from 'vue'
164+
interface Bar extends Foo { y?: number }
165+
interface Props extends Bar {
166+
z: number
167+
y: string
168+
}
169+
170+
interface Foo { x?: number }
171+
172+
export default /*#__PURE__*/_defineComponent({
173+
props: {
174+
z: { type: Number, required: true },
175+
y: { type: String, required: true },
176+
x: { type: Number, required: false }
177+
},
178+
setup(__props: any, { expose: __expose }) {
179+
__expose();
180+
181+
182+
183+
return { }
184+
}
185+
186+
})"
187+
`;
188+
189+
exports[`defineProps > w/ extends intersection Type 1`] = `
190+
"import { defineComponent as _defineComponent } from 'vue'
191+
type Foo = {
192+
x?: number;
193+
};
194+
interface Props extends Foo {
195+
z: number
196+
y: string
197+
}
198+
199+
export default /*#__PURE__*/_defineComponent({
200+
props: {
201+
z: { type: Number, required: true },
202+
y: { type: String, required: true },
203+
x: { type: Number, required: false }
204+
},
205+
setup(__props: any, { expose: __expose }) {
206+
__expose();
207+
208+
209+
210+
return { }
211+
}
212+
213+
})"
214+
`;
215+
216+
exports[`defineProps > w/ extends intersection type 1`] = `
217+
"import { defineComponent as _defineComponent } from 'vue'
218+
type Foo = {
219+
x?: number;
220+
};
221+
interface Props extends Foo {
222+
z: number
223+
y: string
224+
}
225+
226+
export default /*#__PURE__*/_defineComponent({
227+
props: {
228+
z: { type: Number, required: true },
229+
y: { type: String, required: true },
230+
x: { type: Number, required: false }
231+
},
232+
setup(__props: any, { expose: __expose }) {
233+
__expose();
234+
235+
236+
156237
return { }
157238
}
158239
@@ -190,6 +271,56 @@ export default /*#__PURE__*/_defineComponent({
190271
191272
192273
274+
return { }
275+
}
276+
277+
})"
278+
`;
279+
280+
exports[`defineProps > w/ intersection Type 1`] = `
281+
"import { defineComponent as _defineComponent } from 'vue'
282+
type Foo = {
283+
x?: number;
284+
};
285+
type Bar = {
286+
y: string;
287+
};
288+
289+
export default /*#__PURE__*/_defineComponent({
290+
props: {
291+
x: { type: Number, required: false },
292+
y: { type: String, required: true }
293+
},
294+
setup(__props: any, { expose: __expose }) {
295+
__expose();
296+
297+
298+
299+
return { }
300+
}
301+
302+
})"
303+
`;
304+
305+
exports[`defineProps > w/ intersection type 1`] = `
306+
"import { defineComponent as _defineComponent } from 'vue'
307+
type Foo = {
308+
x?: number;
309+
};
310+
type Bar = {
311+
y: string;
312+
};
313+
314+
export default /*#__PURE__*/_defineComponent({
315+
props: {
316+
x: { type: Number, required: false },
317+
y: { type: String, required: true }
318+
},
319+
setup(__props: any, { expose: __expose }) {
320+
__expose();
321+
322+
323+
193324
return { }
194325
}
195326

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">

packages/dts-test/defineComponent.test-d.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1363,13 +1363,13 @@ describe('function syntax w/ runtime props', () => {
13631363
}
13641364
)
13651365

1366-
// @ts-expect-error prop type mismatch
13671366
defineComponent(
13681367
(_props: { msg: string }) => {
13691368
return () => {}
13701369
},
13711370
{
13721371
props: {
1372+
// @ts-expect-error prop type mismatch
13731373
msg: Number
13741374
}
13751375
}

0 commit comments

Comments
 (0)