Skip to content

Commit 2a44b10

Browse files
authored
fix(types): infer discriminated unions in child component props (#3672)
1 parent 006ed12 commit 2a44b10

File tree

4 files changed

+34
-1
lines changed

4 files changed

+34
-1
lines changed

packages/vue-language-core/src/generators/script.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -377,7 +377,7 @@ export function generate(
377377
//#endregion
378378

379379
codes.push('return {} as {\n');
380-
codes.push(`props: __VLS_Prettify<Omit<typeof __VLS_fnPropsDefineComponent & typeof __VLS_fnPropsTypeOnly, keyof typeof __VLS_defaultProps>> & typeof __VLS_fnPropsSlots & typeof __VLS_defaultProps,\n`);
380+
codes.push(`props: __VLS_Prettify<__VLS_OmitKeepDiscriminatedUnion<typeof __VLS_fnPropsDefineComponent & typeof __VLS_fnPropsTypeOnly, keyof typeof __VLS_defaultProps>> & typeof __VLS_fnPropsSlots & typeof __VLS_defaultProps,\n`);
381381
codes.push(`expose(exposed: import('${vueCompilerOptions.lib}').ShallowUnwrapRef<${scriptSetupRanges.expose.define ? 'typeof __VLS_exposed' : '{}'}>): void,\n`);
382382
codes.push('attrs: any,\n');
383383
codes.push('slots: ReturnType<typeof __VLS_template>,\n');

packages/vue-language-core/src/utils/globalTypes.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,11 @@ type __VLS_PickNotAny<A, B> = __VLS_IsAny<A> extends true ? B : A;
1515
1616
type __VLS_Prettify<T> = { [K in keyof T]: T[K]; } & {};
1717
18+
type __VLS_OmitKeepDiscriminatedUnion<T, K extends keyof any> =
19+
T extends any
20+
? Pick<T, Exclude<keyof T, K>>
21+
: never;
22+
1823
type __VLS_GlobalComponents =
1924
__VLS_PickNotAny<import('vue').GlobalComponents, {}>
2025
& __VLS_PickNotAny<import('@vue/runtime-core').GlobalComponents, {}>
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<script setup lang="ts" generic="T extends string | number">
2+
interface InputProps {
3+
type?: 'input';
4+
value: (k: T) => void;
5+
typeDefinition: T;
6+
}
7+
8+
interface SelectProps {
9+
type: 'select';
10+
value: (k: T[]) => void;
11+
typeDefinition: T;
12+
}
13+
14+
defineProps<InputProps | SelectProps>()
15+
</script>
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<script setup lang="ts">
2+
import child from './child.vue';
3+
import { exactType } from '../../shared';
4+
import { ref } from 'vue';
5+
6+
const input = ref(3);
7+
const select = ref('asdf');
8+
</script>
9+
10+
<template>
11+
<child type="select" :typeDefinition="select" :value="k => exactType(k, {} as string[])"></child>
12+
<child type="input" :typeDefinition="input" :value="k => exactType(k, {} as number)"></child>
13+
</template>

0 commit comments

Comments
 (0)