Skip to content

fix(types): infer discriminated unions in child component props #3672

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
Oct 22, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion packages/vue-language-core/src/generators/script.ts
Original file line number Diff line number Diff line change
Expand Up @@ -377,7 +377,7 @@ export function generate(
//#endregion

codes.push('return {} as {\n');
codes.push(`props: __VLS_Prettify<Omit<typeof __VLS_fnPropsDefineComponent & typeof __VLS_fnPropsTypeOnly, keyof typeof __VLS_defaultProps>> & typeof __VLS_fnPropsSlots & typeof __VLS_defaultProps,\n`);
codes.push(`props: __VLS_Prettify<__VLS_OmitKeepDiscriminatedUnion<typeof __VLS_fnPropsDefineComponent & typeof __VLS_fnPropsTypeOnly, keyof typeof __VLS_defaultProps>> & typeof __VLS_fnPropsSlots & typeof __VLS_defaultProps,\n`);
codes.push(`expose(exposed: import('${vueCompilerOptions.lib}').ShallowUnwrapRef<${scriptSetupRanges.expose.define ? 'typeof __VLS_exposed' : '{}'}>): void,\n`);
codes.push('attrs: any,\n');
codes.push('slots: ReturnType<typeof __VLS_template>,\n');
Expand Down
5 changes: 5 additions & 0 deletions packages/vue-language-core/src/utils/globalTypes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,11 @@ type __VLS_PickNotAny<A, B> = __VLS_IsAny<A> extends true ? B : A;

type __VLS_Prettify<T> = { [K in keyof T]: T[K]; } & {};

type __VLS_OmitKeepDiscriminatedUnion<T, K extends keyof any> =
T extends any
? Pick<T, Exclude<keyof T, K>>
: never;

type __VLS_GlobalComponents =
__VLS_PickNotAny<import('vue').GlobalComponents, {}>
& __VLS_PickNotAny<import('@vue/runtime-core').GlobalComponents, {}>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<script setup lang="ts" generic="T extends string | number">
interface InputProps {
type?: 'input';
value: (k: T) => void;
typeDefinition: T;
}

interface SelectProps {
type: 'select';
value: (k: T[]) => void;
typeDefinition: T;
}

defineProps<InputProps | SelectProps>()
</script>
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<script setup lang="ts">
import child from './child.vue';
import { exactType } from '../../shared';
import { ref } from 'vue';

const input = ref(3);
const select = ref('asdf');
</script>

<template>
<child type="select" :typeDefinition="select" :value="k => exactType(k, {} as string[])"></child>
<child type="input" :typeDefinition="input" :value="k => exactType(k, {} as number)"></child>
</template>