Skip to content

Commit 084f30f

Browse files
authored
fix(language-core): keep union props when using withDefaults (#6180) (#6183)
1 parent c0e5d2e commit 084f30f

5 files changed

Lines changed: 54 additions & 2 deletions

File tree

‎packages/language-core/lib/codegen/localTypes.ts‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ export function getLocalTypesGenerator(vueCompilerOptions: VueCompilerOptions, l
1111
isTs
1212
? `
1313
type __VLS_WithDefaults<P, D> = {
14-
[K in keyof Pick<P, keyof P>]: K extends keyof D
14+
[K in keyof P & string]: K extends keyof D
1515
? ${PrettifyLocal.name}<P[K] & { default: D[K] }>
1616
: P[K]
1717
};
@@ -75,7 +75,7 @@ type __VLS_PropsChildren<S> = {
7575
isTs
7676
? `
7777
type __VLS_TypePropsToOption<T> = {
78-
[K in keyof T]-?: {} extends Pick<T, K>
78+
[K in keyof T & string]-?: {} extends Pick<T, K>
7979
? { type: import('${vueCompilerOptions.lib}').PropType<Required<T>[K]> }
8080
: { type: import('${vueCompilerOptions.lib}').PropType<T[K]>, required: true }
8181
};
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
<script setup lang="ts">
2+
interface SharedProps {
3+
size?: 'sm' | 'md' | 'lg';
4+
}
5+
6+
interface NativeProps extends SharedProps {
7+
as?: 'button';
8+
type?: 'button' | 'submit';
9+
href?: never;
10+
}
11+
12+
interface LinkProps extends SharedProps {
13+
as: 'a';
14+
href: string;
15+
type?: never;
16+
}
17+
18+
type Props = NativeProps | LinkProps;
19+
20+
withDefaults(defineProps<Props>(), {
21+
as: 'button',
22+
type: 'button',
23+
size: 'md',
24+
});
25+
</script>
26+
27+
<template>
28+
<slot />
29+
</template>

‎test-workspace/tsc/#6180/main.vue‎

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 { exactType } from '../shared';
3+
import Button from './Button.vue';
4+
5+
type Props = InstanceType<typeof Button>['$props'];
6+
7+
// #6180: withDefaults + discriminated union must not collapse to Record<string, any>
8+
exactType({} as string extends keyof Props ? true : false, false);
9+
</script>
10+
11+
<template>
12+
<Button as="a" href="/" />
13+
</template>
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
{
2+
"extends": "../../tsconfig.base.json",
3+
"vueCompilerOptions": {
4+
"target": 3.5
5+
},
6+
"include": ["**/*"]
7+
}

‎test-workspace/tsc/tsconfig.json‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -526,6 +526,9 @@
526526
{
527527
"path": "./#6094/tsconfig.json"
528528
},
529+
{
530+
"path": "./#6180/tsconfig.json"
531+
},
529532
{
530533
"path": "./#625/tsconfig.json"
531534
},

0 commit comments

Comments
 (0)